以 “Hello Qt” 为例,介绍如何建立一个 Qt 工程

1  QLabel 例程

  QLabel 用来显示文本和图片,它继承自 QFrame (QFrame 继承自 QWidget)

1.1  Hello Qt

  #1 和 #2 标明头文件,也可用一个总的来代替: #include <QtWidgets>

  #6 创建 QApplication 类对象,配合 #11 使整个程序进入事件循环状态,等待用户的动作

  #8 创建 QLabel 对象 label 并赋初值 “Hello Qt!”, 接着 #9 显示出该对象。

#include <QApplication>
#include <QLabel>

int main(int argc, char *argv[])
{
    QApplication app(argc, argv);
    QLabel label("Hello Qt!");
    label.show();

    return app.exec();
} 

  自动生成的工程配置文件 .pro 如下:

QT += core gui

greaterThan(QT_MAJOR_VERSION, 4): QT += widgets

TARGET = HelloQt
TEMPLATE = app

SOURCES += main.cpp  

  支持 HTML 风格

QLabel label("<h2><i>Hello</i>" "<font color=red>Qt!</font></h2>");  

 

1.2  智能指针

  下面程序因为简短,关闭后,操作系统会负责回收内存,但是这种 new 了之后不 delete 的方式是不推荐的 

QLabel *label = new QLabel("Hello Qt!");
label->show();    

1)  Qt 的智能指针

  如果使用指针,可以考虑 Qt 中的智能指针 QScopedPointer 

 QScopedPointer<QLabel> label(new QLabel("Hello Qt!"));   

2)  c++ 的智能指针

  也可使用 c++ 中的智能指针 std::unique_ptr,注意包含头文件 #include <memory>

std::unique_ptr<QLabel> label = std::make_unique<QLabel>("Hello Qt!");  

  

2  QPushButton 例程

  使用 QPushButton 类,新建一个按钮指针 btn,设置其父窗口为 &window,这样当 window 被销毁,就会自动删除 btn,这其实是 Qt 中特有的“拥有权”问题,它可使编程中 new 了不用 delete

  connect 将信号 clicked() 和槽函数 quit() 连接了起来,当点击按钮时,clicked() 信号被发出,接着槽函数被执行,于是程序退出。这称为 "信号槽" 机制

#include <QtWidgets>

int main(int argc, char *argv[])
{
    QApplication app(argc, argv);

    QWidget window;
    window.resize(200, 150);
    window.setWindowTitle("Button");
    window.show();

    QPushButton *btn = new QPushButton("Quit", &window);
    QObject::connect(btn, SIGNAL(clicked()), &app, SLOT(quit()));
    btn->move(50,50);
    btn->show();

    return app.exec();
}  

  界面如下:

 

 

3  QSpinBox 和 QSlider

  实现如下界面,包含 spinbox 和 slider 两个控件,且二者的数值互相关联。
   

    #1 ~ #4 包含所需头文件,#10 和 #11 新建窗口部件,作为顶层窗口 (top-level), #13 ~ #16 新建 spinbox 和 slider 控件指针,取值范围 0~130;

    #18 和 #19 将二者连接起来, 使得 spinbox 和 slider 的数值保持实时同步; #22 新建 layout 布局管理器指针;

    #23 和 #24 将两个控件加入布局管理器 layout 中, #26 在窗体部件上安装布局管理器 layout,此时 QHBoxLayout 以及它包含的 QSpinBox 和 QSlider 会自动 "重新定义" 父窗口,QWidget 会取得它们的所有权,这也是它们在创建时没有设置父窗口的原因; #27 显示出整个窗体部件。

#include <QApplication>    // #include <QtWidgets>
#include <QHBoxLayout>
#include <QSpinBox>
#include <QSlider>

int main(int argc, char *argv[])
{
    QApplication app(argc, argv);

    QWidget window;
    window.setWindowTitle("Enter Your Age");

    QSpinBox *spin = new QSpinBox;
    QSlider *slider = new QSlider(Qt::Horizontal);
    spin->setRange(0,130);
    slider->setRange(0,130);

    QObject::connect(spin, SIGNAL(valueChanged(int)), slider, SLOT(setValue(int)));
    QObject::connect(slider, SIGNAL(valueChanged(int)), spin, SLOT(setValue(int)));
    spin->setValue(35);

    QHBoxLayout *layout = new QHBoxLayout;
    layout->addWidget(spin);
    layout->addWidget(slider);

    window.setLayout(layout);
    window.show();

    return app.exec();
}  

  Qt 中有三个布局管理器类,分别是水平布局管理器 (QHBoxLayout), 垂直布局管理器 (QVBoxLayout), 以及网格布局管理器 (QGridLayout)

  这些布局管理器,可以为加入其中的控件自动分配位置和尺寸大小,省却了手动布局画图的繁琐。

 

参考资料:

  《C++ GUI Programming with Qt4》 2nd ch 1

    <Qt 5.9 | All Qt Examples> Widgets Tutorial - Child Widgets

    <Qt 学习之路2>  豆子   https://www.devbean.net/2012/08/qt-study-road-2-hello-world/

posted on 2016-10-17 17:21  飞鸢逐浪  阅读(10357)  评论(0编辑  收藏  举报