您的位置:首页 > 编程语言 > Qt开发

qt学习笔记001 2015/6/15

2015-06-15 16:25 337 查看

一、 QT入门

1.图形化用户界面

(1)GUI -Graphical User Interface
(2)Hello Qt


#include <QApplication>
#include <QLabel>

int main(int argc,char *argv[])
{
QApplication app(argc,argv);
QLabel *label = new QLabel("Hello Qt!");
label->show();
return app.exec();
}


1.Qt中任意窗口部件都可以用作窗口,在本例中,用窗口部件QLabel作为应程序的窗口。
2.在创建窗口部件的时候,标签通常都是隐藏的,这就允许我们可以先对其进行设置然后在显示它们,从而避免了窗口部件的闪烁现象。
3.return app.exec()使程序进入事件循环状态,等待用户的动作,如单击,用户的动作会让可以产生响应的程序生成一些事件(event,也称消息)。
4.本例存在内存泄漏,不过很小,操作系统可以自动回收。


2.建立连接(信号和槽)

#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()));
button->show();
return app.exec();
}


1.connect 将信号发出者button的单击信号连接到信号接收者app的退出槽函数上,所以单击Quit按钮程序会退出。

3.窗口部件的布局

#include <QApplication>
#include <QHBoxLayout>//布局管理器类
#include <QSlider>
#include <QSpinBox>

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(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);
mainWindow->setLayout(layout);
mainWindow->show();
return app.exec();
}


布局管理器类(设置其所负责的窗口部件的尺寸大小和位置):

QHBoxLayout 在水平方向上排列窗口部件

QVBoxLayout 在竖直方向上排列窗口部件

QGridLayout 把各个窗口部件排列在一个网格中

mainWindow->setLayout(layout);函数会在窗口上安装该布局管理器。

通常顺序:先声明所需的窗口部件,然后再设置它所应具备的属性,再把这些窗口部件添加到布局中,布局会自动设置它们的大小和位置。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: