Matthias
Matthias

Reputation: 471

Show QDialog create in QtDesigner

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();

Upvotes: 1

Views: 1342

Answers (1)

Vazquinhos
Vazquinhos

Reputation: 132

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.

Upvotes: 3

Related Questions