(C++) QDialog
January 10, 2018 ยท View on GitHub
QDialog is a custom Qt dialog class for a dialog.
The code below, Qt example 11: creating a QDialog with QLayout on the fly shows how to create and show a QDialog on the fly (that is: without using the GUI designer). When using the Qt Creator GUI designer, QDialog can be selected as the base class of the custom GUI.
#include <memory>
#include <QtGui/QApplication>
#include <QDialog>
#include <QPushButton>
#include <QVBoxLayout>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
std::unique_ptr<QDialog> dialog(new QDialog);
std::unique_ptr<QVBoxLayout> layout(new QVBoxLayout);
std::unique_ptr<QPushButton> button1(new QPushButton);
std::unique_ptr<QPushButton> button2(new QPushButton);
//Cannot let button1 do anything fancy,
//without creating a derived class from QDialog
button1->setText("Nothing");
button2->setText("Quit");
button2->connect(
button2.get(),SIGNAL(clicked()),
dialog.get(),SLOT(close()));
layout->addWidget(button1.get());
layout->addWidget(button2.get());
dialog->setGeometry(0,0,200,100);
dialog->setWindowTitle("CppQtExample11");
dialog->setLayout(layout.get());
dialog->show();
return a.exec();
}