【学习笔记】C++ GUI Qt4 第一章

开发环境:
Windows10
Qt5.6.3
IDE:
Qt Createor
VS2013

第一章 Qt入门

1.1 Hello Qt

#include <QApplication>
#include <QLabel>

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);//用于管理整个应用程序所用到的资源,argc和argv为命令行参数
    QLabel *label = new QLabel("Hello Qt!");//新建一个标签控件,内容为Hello Qt!
    label->show();//标签通常默认为隐藏,可以先对其进行设置再进行显示,从而避免了窗口部件闪烁现象
    return a.exec();//将应用程序的控制权交给Qt,进行事件循环状态,等待用户操作
}
//这里未对QLabel进行delete操作,这样一点内存泄漏问题无关大局,这部分内容是可以由操作系统重新回收的

运行结果

QLabel *label = new QLabel("<h2><i>Hello</i>""<font color=red>Qt!</font></h2>");
//把上面第8行代码换成这行,说明Qt中同样支持一些简单的HTML样式格式

运行结果

1.2 建立连接

#include <QApplication>
#include <QPushButton>

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    QPushButton *button = new QPushButton("Quit");
    /* 1. QT 4 信号连接
     *(1) 槽函数必须有slots关键字
     *(2) SIGNAL SLOT 将函数转为字符串,不进行错误检查
     *(3) 槽函数和信号一致(参数,返回值),没有返回值
     */
    QObject::connect(button,SIGNAL(clicked()),&a,SLOT(quit()));
    /* 2. QT 5 信号连接
     *(1) SIGNAL SLOT 会进行错误检查
     *(2) 槽可以是任意的成员函数,普通全局函数和静态函数
     *(3) 槽函数和信号一致(参数,返回值),没有返回值
     */
    QObject::connect(button,&QPushButton::clicked,&a,QApplication::quit);
    button->show();
    return a.exec();
}
/* Qt Creator中编译会正常显示页面,但同时会有下面一段提示
 * QWindowsWindow::setGeometry: Unable to set geometry 93x28+640+275 on QWidgetWindow/'QPushButtonClassWindow'. Resulting geometry:  152x28+640+275 (frame: 9, 38, 9, 9, custom margin: 0, 0, 0, 0, minimum size: 0x0, maximum size: 16777215x16777215).
 * 错误原因是父窗口无法容纳对话框,因此需要重新对父窗体进行构造。
 * 后面我们创建控件的时候会给控制指定父对象就不会出现这种问题了
 */

运行结果

1.3 窗口部件的布局

#include <QApplication>
#include <QHBoxLayout>//水平布局
#include <QSlider>//滑动条控件
#include <QSpinBox>//步长调节器控件,用于整数的显示和输入

int main(int argc, char *argv[])
{
    QApplication a(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);//从底层将QSpinBox和QSlider自动重定义父对象

    window->show();
    return a.exec();
}

运行结果

1.4 使用参考文档

关于参考文档的使用我这里推荐另一篇文章介绍的更加的详细:

Qt Assistant(Qt助手)查阅帮助文档(非常详细)http://c.biancheng.net/view/4003.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
书有每章书签。 -------- C++ GUI Programming with Qt 4 (second edition) by Jasmin Blanchette and Mark Summerfield. ISBN 0-13-235416-0 The root of the examples directory contains examples.pro. If you execute qmake examples.pro make (nmake if you use Visual C++), the examples for all chapters with complete cross-platform examples will be built. Note that chapters 11, 17, and 18 use code snippets rather than complete examples, so are not included here. The appendixC directory contains Qt Jambi examples. 1. Getting Started chap01/age chap01/hello chap01/quit 2. Creating Dialogs chap02/find chap02/gotocell1 chap02/gotocell2 chap02/gotocell3 chap02/sort 3. Creating Main Windows chap03/spreadsheet 4. Implementing Application Functionality chap04/spreadsheet 5. Creating Custom Widgets chap05/hexspinbox chap05/iconeditor chap05/iconeditorplugin chap05/plotter 6. Layout Management chap06/findfile1 chap06/findfile2 chap06/findfile3 chap06/mailclient chap06/mdieditor chap06/preferences chap06/splitter 7. Event Processing chap07/ticker 8. 2D Graphics chap08/cityscape chap08/diagram chap08/oventimer 9. Drag and Drop chap09/projectchooser 10. Item View Classes chap10/booleanparser chap10/cities chap10/colornames chap10/coordinatesetter chap10/currencies chap10/directoryviewer chap10/flowchartsymbolpicker chap10/settingsviewer chap10/teamleaders chap10/trackeditor 12. Input/Output chap12/imageconverter chap12/imagespace chap12/tidy 13. Databases chap13/scooters chap13/staffmanager 14. Multithreading chap14/imagepro chap14/semaphores chap14/threads chap14/waitconditions 15. Networking chap15/ftpget chap15
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值