Gt 4.1

Qt 4.1 Simple dialog.


Source Download (2.5kB)

The dialog contains
Text Label
and buttons
Simple dialog

During initialization displays text "Simple dialog correctly initialized"
The button Reset clear text
The button OK displays text "OK Button clicked ..."
The button Cancel close dialog

1. Via Qt designer create new form
File->New Form...
and select Dialog with Buttons Bottom
New Form...

2. We create Text Label so move Label icon to form
Label Text Label

3. In Property editor rename label name to m_pLabel Text Label

4. In the same way create button Reset Push button Push button 02

5. Rename button name to m_pResetButton and button text to Reset m_pResetButton Reset

In the same way rename button OK
objectName = m_pOkButton
button Close
objectName = m_pCancelButton
and dialog
objectName = CSimpleDialog

6. Set signal and slot for buttons Reset a Cancel. We will need achieve clear Text Label after to press button Reset and close dialog after to press button Close. Therefore change designer to signal/slot mod.
Edit signal slots

7. Now press left mouse button Reset and move mouse cursor to Text Label. It`s automatically shows Configure connection dialog. We set signal clicked() and slot clear() Configure connection

8. Delete signal and slot for button OK in Signal/Slot Editor by press button minus. Signal/Slot editor

We will write signal and slot for this button later in CSimpleDialog class.

9. In Signal/Slot Editor change Close button slot reject() to slot close() Close

10. Now dialog looks like this: Dialog

These arrows represent signal influence to slot. These arrows can be manipulated visually, and provide the user with an overview of the form's connection logic.

11. Created dialog save to file simpledialog.ui

12. Create own class CSimpleDialog

#pragma once

#include "ui_simpledialog.h"

class CSimpleDialog : public QDialog, private Ui::CSimpleDialog
	private slots:

		void OkButtonClicked();
		CSimpleDialog(QWidget *parent = 0);

This class is inherit from Ui simple dialog Ui::CSimpleDialog. The important thing is makro Q_OBJECT , which allow acces to simple dialog Ui data.
This class contains function
void OkButtonClicked();
which is declaret as slot
private slots:

void CSimpleDialog::OkButtonClicked()
	m_pLabel->setText("OK Button clicked ...");

This funkce write to Text Label
"OK Button clicked ..."

13. In class constructor

CSimpleDialog::CSimpleDialog(QWidget *parent): QDialog(parent)
	// Set label text
	m_pLabel->setText("Simple dialog correctly initialized");
	// Set signal and slot for "OK Button"
	connect(m_pOkButton, SIGNAL(clicked()), this, SLOT(OkButtonClicked()));

set as Ui this class
set to Text Labelu initialization text
m_pLabel->setText("Simple dialog correctly initialized");
and creage signal and slot for button OK
connect(m_pOkButton, SIGNAL(clicked()), this, SLOT(OkButtonClicked()));

14. The file main.cpp looks like this:

#include "ui_simpledialog.h"
#include "simpledialog.h"
#include <QApplication>

int main(int argc, char *argv[])
	QApplication app(argc, argv);
	QDialog *window = new CSimpleDialog;
	return app.exec();

15. Create project file

TEMPLATE    = app
FORMS       = simpledialog.ui
HEADERS     = simpledialog.h
SOURCES     += main.cpp \ 

16. Command
create file Makefile and file ui_simpledialog.h

17. Finally compile projekt by command

home / qt

Valid XHTML 1.0 Transitional