Tag: QT
In the interface design, QT and WPF (C #) are mostly used, and the previous MFC has become aging. I recently learned QT and found it very useful. I decided to record my learning process. I may feel the pleasure of coming later.
I. Hello QT
#include "try_qt.h"#include <QtGui/QApplication>#include <QLabel>int main(int argc, char *argv[]){QApplication app(argc, argv);QLabel *label = new QLabel("Hello Qt!");label->show();return app.exec();}
QApplication app(argc, argv);
Create a qapplication object app to manage program resources.
QLabel *label = new QLabel("Hello Qt!");
Create a qlabel widget to display the content of the brackets.
label->show();
Display the label content.
return app.exec();
Exit QT and the operating system re-allocates the resources.
Ii. qpushbutton
#include "try_qt.h"#include <QtGui/QApplication>#include <QPushButton>int main(int argc, char *argv[]){QApplication app(argc, argv);QPushButton *button = new QPushButton("Quit");QObject::connect(button, SIGNAL(clicked()),&app, SLOT(quit()));button->show();return app.exec();}
The widgets of QT sends a signal indicating that the user's behavior changes the status of the latter. When you click a button, qpushbutton sends a clicked () signal. A signal can be associated with a function. When a signal is sent, its corresponding slot is automatically executed.
QObject::connect(button, SIGNAL(clicked()),&app, SLOT(quit()));
When a user clicks a button, qpushbutton sends a clicked () signal, causing the associated quit () function to be automatically executed.
3. widgets Layout
#include "try_qt.h"#include <QtGui/QApplication>#include <QHBoxLayout>#include <qslider>#include <qspinbox>int main(int argc, char *argv[]){QApplication app(argc, argv);QWidget *window = new QWidget;window->setWindowTitle("Enter your age");QSpinBox *spinBox = new QSpinBox;QSlider *slider = new QSlider(Qt::Horizontal);spinBox->setRange(0,130);slider->setRange(0,130);QObject::connect(spinBox,SIGNAL(valueChanged(int)),slider,SLOT(setValue(int)));QObject::connect(slider,SIGNAL(valueChanged(int)),spinBox,SLOT(setValue(int)));spinBox->setValue(35);QHBoxLayout *layout = new QHBoxLayout;layout->addWidget(spinBox);layout->addWidget(slider);window->setLayout(layout);window->show();return app.exec();}
This program is composed of three widgets, namely qspinbox, qslider, and qwidget. Qwidget is the main window of the program, and the subwindow is qspinbox and qslider.
<span style="white-space:pre"></span>QWidget *window = new QWidget;window->setWindowTitle("Enter your age");
Create a qwidget as the main window of the program and call setwindowtitle () to set the name of the main window.
QSpinBox *spinBox = new QSpinBox;QSlider *slider = new QSlider(Qt::Horizontal);spinBox->setRange(0,130);slider->setRange(0,130);
Create qspinbox and qslider and set their valid range
<pre name="code" class="cpp">QObject::connect(spinBox,SIGNAL(valueChanged(int)),slider,SLOT(setValue(int)));QObject::connect(slider,SIGNAL(valueChanged(int)),spinBox,SLOT(setValue(int)));
Make sure that spin-box and slider are synchronized. The slot values are the same whether the signal valuechanged (INT) is sent or the setvalue (INT) is set.
spinBox->setValue(35);
Set the value of spinbox to 35.
QHBoxLayout *layout = new QHBoxLayout;layout->addWidget(spinBox);layout->addWidget(slider);window->setLayout(layout);
Use the layout manager layout to layout the spinbox and slider. Call qwidget: setlayout to install the layout manager in the window.
Learn QT, getting started