@1405010312
2017-06-04T14:29:58.000000Z
字数 2664
阅读 586
C++GUIQt4
- 这一章介绍如何把基本的C++知识与Qt所提供的功能组合起来创建一些简单的图形用户界面应用程序.在这一章中,还引入了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();
}
qmake -project
生成一个与平台无关的项目文件hello.proQT += widget
qmake hello.pro
生成一个平台相关的makefile文件make(mingw32-make)
命令就可以构建该程序.qmake -tp vc hello.pro
然后就可以在Visual Studio中编译这个程序.qmake -spec macx-xcode hello.pro
这个要说明如何响应用户的动作.这个应用程序有一个按钮构成,用户可以单击这个按钮退出程序.源代码如下:
#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.4所示,它可以用来询问用户的年龄,而用户可以通过操纵微调框(spin box)或者滑块(slider)来完成年龄的输入.代码如下:
#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(valueChange(int)), slider, SLOT(setValue(int)));
QObject::connect(slider, SIGNAL(valueChange(int)), spinBox, SLOT(setValue(int)));
QHBoxLayout *layout = new QHBoxLayout;
layout->addWidget(spinBox);
layout->addWidget(slider);
window->setLayout(layout);
window->show();
return app.exec();
}
./age -style motif
在Qt的doc/html目录下可以找到HTML格式的参考文档.
在Qt Assistant中可以查看帮助文档