Qt Study Note(1)

xiaoxiao2025-09-08  148

Introduce Qt

Hello Qt

#include <QApplication> #include <QLabel> int main(int argc, char *argv[]) { QApplication app(argc, argv); /* create an QApplication object, manage all sources in program, need two arguments */ QLabel *label = new QLabel("Hello Qt!"); /* create a Qlabel widget displaying "Hello Qt!" */ label->show(); // make Qlabel's label visiable( default invisible ) return app.exec(); // give ownership to Qt, waiting for user's motion }

Launch Connection

FUNCTION : user click the button can exit

#include <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())); /* 宏SIGNAL()和SLOT()是Qt语法中的一部分,负责将函数名转换成字符串 */ button->show(); return app.exec(); }

Widget’s Layout(窗口部件的布局)

FUNCTION : user can operate spin box(微调框) or slider(滑块) to input age

#include <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))); /* synchronize spinBox and slider */ spinBox->setValue(35); QHBoxLayout *layout = new QHBoxLayout; layout->addWidget(spinBox); layout->addWidget(slider); window->setLayout(layout); window->show(); return app.exec(); }

Three layout manager classes in Qt

QHBoxLayout : arrange widgets in horizontal( left to right)QVBoxLayout : arrange widgets in vertical( top to bottom)QGridLayout : arrange all widgets in one grid
转载请注明原文地址: https://www.6miu.com/read-5035939.html

最新回复(0)