1. Hello,QT
#include <QApplication>
#include <QLabel>
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
QLabel *label = new QLabel("Hello,World");
label->show();
return app.exec();
}
2. QT信号槽链接
槽函数四要素:
( s e n d e r , s i g n a l , s l o t , s l o t _ f u n c ) (sender,signal,slot,slot\_func) (sender,signal,slot,slot_func)
#include <QApplication>
#include <QLabel>
#include <QPushButton>
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
QPushButton *button = new QPushButton("click to quit");
QObject::connect(button,SIGNAL(clicked()),&app,SLOT(quit()));
button->show();
return app.exec();
}
3. QT布局
下面的程序将滑条和复选框关联起来
三种布局方式:
- 水平
- 竖直
- 网格
QHBoxLayout
QVBoxLayout
QGridLayout
#include <QApplication>
#include <QLabel>
#include <QPushButton>
#include <QHBoxLayout>
#include <QSlider>
#include <QSpinBox>
#include <QWidget>
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
QWidget *mainWindow = new QWidget();
mainWindow->setWindowTitle("Enter your age");
QSpinBox *spinBox = new QSpinBox();
QSlider *slider = new QSlider(Qt::Horizontal);
spinBox->setRange(0, 130);
slider->setRange(0, 130);
QObject::connect(slider, SIGNAL(valueChanged(int)),spinBox, SLOT(setValue(int)));
QObject::connect(spinBox, SIGNAL(valueChanged(int)), slider, SLOT(setValue(int)));
spinBox->setValue(25);
QHBoxLayout *layout = new QHBoxLayout();
layout->addWidget(spinBox);
layout->addWidget(slider);
mainWindow->setLayout(layout);
mainWindow->show();
return app.exec();
}
4. QT助手
可以通过 Q T A s s i s t a n t QT Assistant QTAssistant来查询 a p i api api和对应的类说明。