I would like to make something like GUI creator which takes an ui file and creates widgets in it, and show in a window.
So I have created a button on click I use QFileDialog to get a file.
And then I would like to use the ui file from QFileDialog to create that gui/widgets and show in a window.
I have tried QFormbuilder, but it always gives me compile error “undefined reference to `QFormBuilder::QFormBuilder()’”
Is there a way to do it in qt5?
Any help appreciated
From documentation of QUiLoader:
QUiLoader loader;
QFile file(":/forms/myform.ui");
file.open(QFile::ReadOnly);
QWidget *myWidget = loader.load(&file, this);
file.close();
I have solved the problem.
Actually I was missing QFormbuilder module in my .pro file. So I have just added them, and it is working fine.
Related
I have two radio buttons. I'd like to replace the controls' text (not the indicator) with images/icons. How can I do this?
With a QLabel I do this by using setPixmap(); the method is not available in QRadioButtons.
I tried
setStyleSheet("background-image: url(./images/img.png); background-repeat: no-repeat;");
but the image is set as the background of the entire control (indicator + label), not just the label.
Any idea?
Why not use Qt resource system to add the desired icons and then use the buttons setIcon method to assign them an icon? Here's an example :
#include "widget.h"
#include <QIcon>
Widget::Widget(QWidget *parent) : QWidget(parent) {
lyt = new QVBoxLayout;
grpBx = new QGroupBox("Radio buttons");
QVBoxLayout* lyt_btns = new QVBoxLayout;
grpBx->setLayout(lyt_btns);
rBtn1 = new QRadioButton("");
rBtn2 = new QRadioButton("");
lyt_btns->addWidget(rBtn1);
lyt_btns->addWidget(rBtn2);
rBtn1->setIcon(QIcon(":/spoon.png"));
rBtn1->setIconSize(QSize(10,10));
rBtn2->setIcon(QIcon(":/fork.png"));
rBtn2->setIconSize(QSize(10,10));
lyt->addWidget(grpBx);
setLayout(lyt);
}
Widget::~Widget() {
delete lyt;
delete rBtn1;
delete rBtn2;
delete grpBx;
}
PS if you don't know what is and how to use the Qt ressource system here some tips:
Choose the Edit view in Qt Creator.
Right-click on the project, then choose Add New.... In the New File dialog that
opens, click on Qt under Files and Classes, and click on Qt Resource file.
Name the file resources.
Add it to the current project.
If resources.qrc isn't already open in the editor, double-click on it in the
solution pane. The resource file editor will appear.
Click on Add, choose Add prefix, and make the prefix /.
Click on Add again, then on Add Files, and choose your icon.
After that you've added ressources to your .qrc file, the application can access those files by simply using the colon symbol ":" + the prefix specified in step 6 and then the ressource name (as shown in the example)
Please see : The Qt Resource System documentation
Class DCGraphicsView is a subclass of QGraphicsView, I put a QGraphicsView Widget on the UI file framework.ui. And the I promote that QGraphicsView Widget to DCGraphicsView. After clean, qmake and rebuild, everything is OK as expected.
But after I moved those code and ui file into a subfolder named ui, and modify the pro as well, use the same procedure as previous (clean, qmake and rebuild). An error message displayed when doing rebuild.
To figure out what causes this error, I cancel the promotion of QGraphicsView Widget, then no error pops out. If I put it back, the same error comes again. Confused.
error message as follows:
No rule to make target 'dcgraphicsview.h', needed by 'ui_framework.h'.Stop.
The "Header File" field in the promote dialog should contain the path of the header file relative to the base directory from which uic, Qt User Interface Compiler, is executed, so it should be "ui/dcgraphicsview.h".
You can change the path by double clicking on the path in the "Promoted Widgets" dialog.
I got the right solution. In pro file, use UI_DIR to specify the location of ui files. In unix like system, use unix:UI_DIR; and win32:UI_DIR for windows.
In QtWebkit, using QWebSettings class, I could enable like the permission to close the window using the JavaScript command window.close();:
setAttribute(QWebSettings::JavascriptCanCloseWindows, true);
But in QtWebEngine, such an attribute doesn't exist: http://doc.qt.io/qt-5/qwebenginesettings.html#WebAttribute-enum
How to allow JavaScript to close any QWebEngineView using window.close()?
Indeed, this attribute doesn't exist anymore in the Qt WebEngine.
However, you can close any views using the signal windowCloseRequested from your QWebEnginePage, and connecting it to a slot where you close the window. There is an example of use in the Demo Browser example, in the file webview.cpp:
connect(page(), &WebPage::windowCloseRequested, this, &QWidget::close);
I am using QFileDialog as
filename = QFileDialog::getExistingDirectory(this,"Select Image File: ",dataDir,0);
I want that I can check files inside folder before selecting it. function getExistingDirectory() is setting QFileDialog::ShowDirsOnly as a default option. I checked in docs there is no any option that do opposite of this. So I set last parameter 0. But now it is not using native dialog. I want to use native dialog with this. I have no clue how to do this cause no flag found in options for UseNativeDialog. Please help.
Try creating the file dialog on your own, something like:
QFileDialog dialog(this);
dialog.setFileMode(QFileDialog::Directory);
dialog.setViewMode(QFileDialog::Detail);
dialog.setDirectory(datadir);
dialog.exec();
The code by Sebastian should create a native dialog, unless you make a line such as:
dialog.setOption(QFileDialog::DontUseNativeDialog, true);
However, I have not been able to get this working under Windows, even though the documentation says that the QFileDialog::Directory option should display files by default. Not only that, but doing:
qDebug() << dir_selector.testOption(QFileDialog::ShowDirsOnly);
displays false on my system, indicating that there is probably a bug somewhere.
I'm working on a QML application for an embedded platform which includes a GridView widget containing images. It's important for me that scrolling through the GridView will be smooth and will not put load on the CPU. Can I expect Qt to use OpenGL to render the GridView?
I faced with the same problem.
QApplication::setGraphicsSystem(QLatin1String("opengl"));
haven`t work for me. So i set the OGWidget as a viewport:
QDeclarativeView mainwindow;
mainwindow.setSource(QUrl::fromLocalFile("./qml/app.qml"));
QGLFormat format = QGLFormat(QGL::DirectRendering); // you can play with other rendering formats like DoubleBuffer or SimpleBuffer
format.setSampleBuffers(false);
QGLWidget *glWidget = new QGLWidget(format);
glWidget->setAutoFillBackground(false);
mainwindow.setViewport(glWidget);
and do not forget to add opengl in *.pro file.
Depending on your platform use
QApplication::setGraphicsSystem(QLatin1String("opengl"));
or (Symbian)
QApplication::setGraphicsSystem(QLatin1String("openvg"));
before you instantiate the QApplication object.
By default Qt does not use the OpenGL render backend. You can enforce it by using a QGlWidget. In your case, as you want to use a stock widget, you can set the render backend as a command line option:
<binary name> -graphicssystem opengl