Show QDialog create in QtDesigner - c++

I created next to my MainWindow a second form as QDialog with the Qt Designer.
My problem is how to show this Dialog by click a Button in the MainWindow.
If I use following code it creates a newDialog but I want to use the form I created in Qt Designer. How I can embed it?
QDialog *myDialog = new QDialog;
myDialog->show();

First you need to create in QtDesigner a new dialog. In that dialog add all the stuff that you need and then save all the changes. The file that you will create will be a .ui. Add this file to your project.
After this create a header file and cpp file with the name that you want( it would be better if you use the same name as de ui file ). Then in the .h:
namespace Ui {
class QDialogExample;
}
class QDialogExample : public QDialog
{
Q_OBJECT
public:
explicit QDialogExample(QWidget *parent = 0);
~QDialogExample();
private:
Ui::QDialogExample *ui; // This will be the acces to the widgets defined in .ui
};
And then in the .cpp file:
QDialogExample::QDialogExample(QWidget *parent) :
QDialog(parent),
ui(new Ui::QDialogExample)
{
ui->setupUi(this); // This will init all the widgets
}
Then in the slot of the QAction or your custom button add the call to
QDialogExample pDialogExample = new pDialogExample( this );
pDialogExample->show();
Anyway, to learn how it works, an all the process it would be advisable to use QtCreator, this is very helpfull in the creation of the dialogs and all the widgets that you need.

Related

QT5 custom widget with .ui file - calling Ui::myWidget.setupUi required when displaying it

I created QT custom widget MyWidget with a .ui file. But when I place QWidget to MainWindow in QT designer and promote it to MyWidget, it just renders an empty widget.
I figured out why it doesn't work. In generated file ui_mainwindow.h in function setupUi is code for displaying my widget following:
widget = new MyWidget(centralWidget);
widget->setObjectName(QString::fromUtf8("widget"));
verticalLayout->addWidget(widget);
It works, when I call Ui::MyWidget.setupUi (replace the code above with following):
widget = new MyWidget(centralWidget);
Ui::MyWidget uiOfWidget;
uiOfWidget.setupUi(widget);
widget->setObjectName(QString::fromUtf8("widget"));
verticalLayout->addWidget(widget);
So my question is, how it's possible to reach one of following:
Generate code like this automatically when compiling MainWindow.ui file
Remove the need for calling Ui::MyWidget.setupUi
Or other fix...?
I use QT5. Thank you!
As #drescherjm pointed out, the solution is to call Ui::MyWidget.setupUi in constructor of MyWidget:
MyWidget::MyWidget(QWidget* parent, Qt::WindowFlags f): QWidget(parent, f) {
Ui::MyWidget ui;
ui.setupUi(this);
}

Adapt Qt window form to prebuilt QWindow

I'm testing a project I've downloaded that doesn't have a "mainwindow.ui" but the main window is created programmatically inheriting QWindow instead.
OpenGLWindow::OpenGLWindow(QScreen* screen ): QWindow(screen)
It works properly but of course adding new elements on this window is not as quick as it would be using the Qt designer. Therefore I was wondering if there is a way, after creating a new form with the Qt designer, to promote the new window to this existing OpenGLWindow so that I can view its content in the "newwindow.ui". I hope this makes sense.
I'm not completely sure, but this general idea should work, assuming the window class you created is named NewWindow:
class OpenGLWindow : public QWindow {
Ui::NewWindow *ui; // Add this variable
/* rest of class */
}
#include <ui_newwindow.h>
OpenGLWindow::OpenGLWindow(QScreen* screen): QWindow(screen), ui(new Ui::NewWindow) {
ui->setupUi(this);
/* rest of constructor */
}
And then don't forget to delete ui in the destructor.

Qt C++ Creating toolbar

I am learning Qt and trying some examples in the book "Foundations of Qt Development".
In the book, there is a section teaching Single Document Interface with an example creating a simple app like a notepad.
However I am having problem with toolbar creating.
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
setAttribute(Qt::WA_DeleteOnClose);
setWindowTitle(QString("%1[*] - %2").arg("unnamed").arg("SDI"));
connect(ui->docWidget->document(), SIGNAL(modificationChanged(bool)), this, SLOT(setWindowModified(bool)));
createActions();
createMenu();
createToolbars();
statusBar()->showMessage("Done");
}
It is the constructor of the main window.
void MainWindow::createToolbars()
{
QToolBar* toolbar;
toolbar = addToolBar(tr("File"));
toolbar->addAction(anyaction);
}
This is how the book create the toolbar.
However, when I try to run the program, there are two toolbars created.
One is the toolbar created by the code and called "File"
Another is a blank toolbar created by the ui designer ie. *ui.toolbar.
In order to get rid of two toolbars, I tried using only the *ui.toolbar.
It's working. The code is shown below.
void MainWindow::createToolbars()
{
ui->toolBar->addAction(anyaction);
}
But I tried to create the toolbar by code only, ie. not adding a toolbar in the ui designer.
So I write this:
void MainWindow::createToolbars()
{
QToolBar* FileBar = this->addToolBar(tr("File"));
FileBar->addAction(anyaction);
}
However, there is a compile error.
The compiler use this function:
void QMainWindow::addToolBar(QT::ToolBarArea area, QToolBar * toolbar)
instead of what I want:
QToolBar * QMainWindow::addToolBar(const QString & title)
http://doc.qt.io/qt-5/qmainwindow.html#addToolBar-3
What is my mistake here?
When you removed QToolBar from MainWindow QtCreator automatically removed import of QToolBar class.
Just add this to the top of mainwindow.h:
#include <QToolBar>
And it is better to define QToolBar* FileBar in private section of MainWindow in mainwindow.h. Then you will be able to access it from any method of MainWindow class.
void MainWindow::createToolbars()
{
FileBar = this->addToolBar(tr("File"));
FileBar->addAction(anyaction);
}
When you see such message:
must point to class/struct/union/generic type
First of all try to include headers for necessary class.

How to call created .ui design form in Qt with button.clicked() event?

I'm very new in Qt and I would like to ask how to call the created .ui designer form to be connected with the PushButton in MainWindow.
What I've done:
1. Created a new .ui design form (Forms.right click -> Add New -> Qt Designer Form) to the current project.
2. Designed an about dialog in the .ui form created.
3. Make a slot PushButton.clicked() in the MainWindow.
For all who have done programming in Qt, please help me solve this problem to connect the PushButton with the .ui form, so the PushButton can call/show the form created in the .ui.
Thanks for your attention.
Using .ui files is explained in Qt's documentation:
http://doc.qt.io/archives/qt-4.7/designer-using-a-ui-file.html
Summary:
The easiest way to handle an .ui file is to run it through uic (the User Interface Compiler) at compile time. This is done automatically if the .ui file is included in your project (.pro) file. ("Add New" probably has done this automatically in your case.) Then you only need to include the generated C++ header file in your source file. Its name should be something like "ui_nameoftheoriginaluifile.hpp". Of course, after that you need to instantiate the form defined in the .hpp file.
Edited to add:
There are several issues with your code, starting from its readability. I don't know if you have used an object-oriented language before, but a very basic rule in C++ is to begin class names with capital letters to make them easier to distinguish from objects and other variables. So the class names should be "About", "Parent", etc.
The compilation error is caused by using "about" instead of the name of the class actually used in the "ui_about.hpp" file - "MainWindow". (Which is determined by the name of the form you have used in the .ui file.)
If you are using Qt Creator: hold down the Ctrl key and click on the name of the file "ui_about.h" in the include directive. This will open it for inspection. Try to figure out how it works.
You also haven't defined the opDialog() function as a member of the "parent" class in "parent.cpp", which causes another compile-time error.
You also shouldn't be using a QMainWindow for an about dialog. QMainWindow is supposed to be the main window of your application - there shouldn't be any more instances of it.
So, about.h:
namespace Ui {
class MainWindow;
}
class about : public QMainWindow {
Q_OBJECT
public:
about(QWidget *parent = 0);
~about();
protected:
void changeEvent(QEvent *e);
private:
Ui::MainWindow *ui;
};
And the beginning of about.cpp:
#include "about.h"
#include "ui_about.h"
about::about(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
}

Qt - How to override a widget?

I've created a form using the Qt designer which includes a QToolButton.
I can turn it using ui->favouriteToolButton.
I used the designer because it's much more simple then code all the gui.
Problem is that specifically for this ToolButton i want to code by creating its own class:
class FavouriteMenu : public QToolButton
{
Q_OBJECT
public:
explicit FavouriteMenu(QWidget *parent = 0);
//~FavouriteMenu();
private:
QMenu *menu; //MENU
QAction *addToBookmarkAct;
QAction *editBookmarkAct;
...
I want to be able to override ui->favouriteToolButton with a new instance of FavouriteMenu.
How can i do it?
Right click on the button in designer and select "Promote To..."
Under Promoted class name enter "FavouriteMenu"
Under Header File enter the filename of the .h file
Click "Add"
Click "Promote"