-
Notifications
You must be signed in to change notification settings - Fork 0
Cpp Qt Programming
Welcome to the QT-Tutorial-notes wiki!
- go to File > New and then choose the QT Console Application template.
- Now configure your project.
In the main.cpp file include the QDebug header file.
#include<QDebug>
Inside the main function call the qDebug() function.
qDebug() << "Hello World";
alternatively you can declare a QString and assign "Hello World" to it.
QString mStr = "Hello World";
qDebug() << mStr;
This time create a QT GUI Application from File > New
The main.cpp GUI application is similar to the console application except this one has these extra lines
MainWindow w;
w.show();
this generates the class MainWindow;
inside the namespace Ui
in mainwindow.h file.
- Inside the class definition there is a macro
Q_OBJECT
. - Everything in QT is a QOBJECT
In mainwindow.cpp , the ui->setupUi(this)
creates user interface.
Inside the mainwindow.ui create a pushButton i.e. drag and drop it in the Design panel.
The ui pointer holds a reference to the pushButton and in the mainwindow.cpp you can use it like this
ui->pushButton->setText("Close")
If you hover over you can see F1 and press it to go to the help file for QPushButton
Go to the Design Mode > Edit Signal and Slots and connect the button to the form and configure it to clicked()
and closed()
. Now build and run the GUI app. If you press the button it will close the form.
connect()
is used to connect the signal-slot path. e.g.
connect(ui->horizontalSlider,SIGNAL(valueChanged(int)),
ui->progressBar,SLOT(setValue(int)))
It takes a sender which is a QObject * and generate a signal to send to the receiver which is another QObject * and the SLOT receives the signal.
disconnect()
does the opposite of connect()
i.e. to disconnect the signal slot mechanism.
disconnect(ui->horizontalSlider,SIGNAL(valueChanged(int)),
ui->progressBar,SLOT(setValue(int)))
- A QAction helps you to trigger an event.
- In the design panel, Create a menu Item and make an action. After that drag and drop it into the tool Bar.
- Right click on the action and Go to slot..
- after selecting the signal it will generate the code.
In the design panel drag and drop a Plain Text Editor and inside the mainwindow.cpp
setCentralWidget(ui->plainTextEdit);
It will help to take the majority of space.
right click on Forms select Qt and select Qt Designer Form Class . In the design panel design the form. To show this:
-
Modal : can not interact with other windows
-
void MainWindow::on_actionNew_Window_triggered() { MyDialog mDiaog; mDialog.setModal(true); mDilog.exec(); }
-
-
Modaless : can interact with multiple windows
- In mainwindow.h
#include "mydialog.h"
and create aprivate:
object pointerMyDialog *mDialog
. - In mainwindow.cpp
void MainWindow::on_actionNew_Window_triggered() { mDialog = new MyDialog(this); mDialog->show(); }
- In mainwindow.h