Sunday, May 8, 2011

Create Custom QDialogs With QLayout Classes

Qt Designer is used to design user interfaces for QT applications. In addition to using QT Designer to generate user interfaces, you can populate your dialogs with widgets on them dynamically at runtime by using layout classes. To achieve this, you need to extend qdialog class and add widgets to your derived qdialog instance dynamically at runtime.
Qt's layout classes are used to place widgets on parent dialogs or widgets. Instead of manually placing widgets on main containers and creating static user interfaces, layout classes help to place widgets easily. There are different QT layout classes but QHBoxLayout and QVBoxLayout are the most popular of them. By using the addWidget() method of layout class, you place items on the layout. After adding widgets to the layout properly, it is required to call setLayout() method of the main container to make layout visible on it.
Sample project started as a QT Gui Application and contains main.cpp, DynamicQDialog.h and DynamicQDialog.cpp files.
DynamicDialog.pro is the project configuration file contains:

TARGET = DynamicDialog
CONFIG += qt gui
TEMPLATE = app
SOURCES += main.cpp \
DynamicQDialog.cpp
HEADERS += \
DynamicQDialog.h

main.cpp file contains the custom QDialog instance and shows it.

#include <QApplication>
#include "DynamicQDialog.h"
int main(int argc, char *argv[]) {
QApplication app(argc, argv);
DynamicQDialog dialog;
dialog.show();
return app.exec();
}

DynamicQDialog.h file is the derived QDialog class header file. Custom QDialog contains signals and slots to be associated with buttons on the dialog.

#ifndef DYNAMICQDIALOG_H
#define DYNAMICQDIALOG_H
#include <QDialog>
class DynamicQDialog : public QDialog
{
Q_OBJECT
public:
DynamicQDialog(QWidget * parent = 0, Qt::WindowFlags f = 0);
~DynamicQDialog();
public slots:
void sl_accept();
void sl_cancel();
signals:
};
#endif // DYNAMICQDIALOG_H

DynamicQDialog.cpp file contains implementation details for derived QDialog class. 2 QPushButtons added to the QHBoxLayout dynamically at runtime and displayed on the DynamicQDialog instance.

#include "DynamicQDialog.h"
#include <QHBoxLayout>
#include <QPushButton>
#include <QTextEdit>
#include <QMessageBox>

DynamicQDialog::DynamicQDialog(QWidget* parent, Qt::WindowFlags flags): QDialog( parent, flags )
{
QGridLayout* mainGrid = new QGridLayout;
QVBoxLayout* topLayout = new QVBoxLayout;
topLayout->addWidget(new QTextEdit);
mainGrid->addLayout(topLayout,0,0);

QHBoxLayout* hLayout = new QHBoxLayout;
QPushButton* btn;
for( int i=0; i<2; i++ ) {
if( i == 0 ) {
btn = new QPushButton("OK");
connect(btn, SIGNAL(clicked()), this, SLOT(sl_accept()));
}
else if( i == 1 ) {
btn = new QPushButton("Cancel");
connect(btn, SIGNAL(clicked()), this, SLOT(sl_cancel()));
}
hLayout->addWidget(btn);
}
mainGrid->addLayout(hLayout,1,0);

setLayout(mainGrid);
}

DynamicQDialog::~DynamicQDialog(){}

void DynamicQDialog::sl_accept(){
QMessageBox msgBox;
msgBox.setText("OK Clicked!");
msgBox.exec();
}

void DynamicQDialog::sl_cancel(){
QMessageBox msgBox;
msgBox.setText("Cancel Clicked!");
msgBox.exec();
}



Application created by QT Creator and uses Qt4.7 .