Qt entry routine (1), qt entry routine
The following uses "Hello Qt" as an example to briefly introduce how to build a Qt project.
1 QLabel routine
1.1 Hello Qt
#1 and #2 indicate the contained header file; #6. Create an Instance Object of the QApplication class, and enable the event loop for the entire program with #11;
#8 create a QLabel Object label and assign the initial value "Hello Qt !", Then #9 shows the label object.
1 #include <QApplication> 2 #include <QLabel> 3 4 int main(int argc, char *argv[]) 5 { 6 QApplication app(argc, argv); 7 8 QLabel label("Hello Qt!"); 9 label.show();10 11 return app.exec();12 }
The project configuration file. pro is as follows:
QT += coregreaterThan(QT_MAJOR_VERSION, 4): QT += widgetsCONFIG += c++11TARGET = HelloQtTEMPLATE = appSOURCES += main.cpp
HTML style supported
QLabel label("
The output is as follows:
1.2 smart pointer
The example in <C ++ GUI Programming with Qt4_2nd> is
8 QLabel *label = new QLabel("Hello Qt!");9 label->show();
Because the program is small, the operating system will be responsible for memory recovery after it is closed, but this new method is not recommended for deleting.
1.2.1 smart pointer of Qt
To use pointers, consider the smart pointer QScopedPointer in Qt.
QScopedPointer<QLabel> label(new QLabel("Hello Qt!"));
1.2.2 smart pointer of c ++
Another solution is the smart pointer std: unique_ptr in c ++.
std::unique_ptr<QLabel> label(new QLabel("Hello Qt!"));
Note that the header file is included.
#include <memory>
2 QPushButton routine
The following routine uses the QPushButton class and connects the signal clicked () and the slot function quit () through the connect function.
When the user clicks the button, the clicked () signal is sent, and the slot function is automatically executed, so the program exits.
1 #include <QApplication> 2 #include <QPushButton> 3 4 int main(int argc, char *argv[]) 5 { 6 QApplication app(argc, argv); 7 8 QPushButton btn("Quit"); 9 QObject::connect(&btn, SIGNAL(clicked()), &app, SLOT(quit()));10 btn.show();11 12 return app.exec();13 }
The program output is as follows:
3 QSpinBox and QSlider
The following interface is implemented, including the spinbox and slider controls, and their values are correlated.
#3 and #4 contain the required header file, #10 and #11 create a widget, #13 ~ #16 create a new control object for the spinbox and slider, and set the range;
#18 and #19 connect the two so that the values of the spinbox and slider are synchronized in real time;
#23 and #24 add the two controls to the layout manager layout. #26 set the form part manager to layout and #27 to display the entire form part.
1 #include <QApplication> 2 #include <QHBoxLayout> 3 #include <QSpinBox> 4 #include <QSlider> 5 6 int main(int argc, char *argv[]) 7 { 8 QApplication app(argc, argv); 9 10 QWidget window;11 window.setWindowTitle("Enter Your Age");12 13 QSpinBox spin;14 QSlider slider(Qt::Horizontal);15 spin.setRange(0,130);16 slider.setRange(0,130);17 18 QObject::connect(&spin, SIGNAL(valueChanged(int)), &slider, SLOT(setValue(int)));19 QObject::connect(&slider, SIGNAL(valueChanged(int)), &spin, SLOT(setValue(int)));20 spin.setValue(35);21 22 QHBoxLayout layout;23 layout.addWidget(&spin);24 layout.addWidget(&slider);25 26 window.setLayout(&layout);27 window.show();28 29 return app.exec();30 }
There are three layout manager classes in Qt: QHBoxLayout, QVBoxLayout, and QGridLayout)
These layout managers can automatically allocate positions and sizes for the controls to be added, saving the hassle of manual layout and drawing.
References:
<C ++ GUI Programming with Qt4_2nd> chapter 1
<Qt learning path 2> bean https://www.devbean.net/2012/08/qt-study-road-2-hello-world/