C++ GUI Qt4学习笔记(一)

本文介绍如何运用C++与Qt框架结合,创建基本的图形用户界面应用程序,涉及信号与槽、窗口部件及布局管理的概念,并通过实例展示了如何通过连接用户事件与窗口部件,以及使用布局实现界面的合理排列。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

这一章介绍了如何把基本的C++只是与Qt所提供的功能组合起来创建一些简单的图形用户界面应用程序。

引入两个重要概念:一个是“信号和槽”,另一个是“布局”。

窗口部件(widget)是用户界面的一个可视化元素,相当于windows系统中的“控件”和“容器”。任意窗口部件都可以用作窗口。

1.1 Hello Qt
正确安装Qt4开发环境,创建工程目录hello,源代码文件名为hello.cpp,进入hello目录

#include <QApplication>
#include <QLabel>
int main(int argc,char * argv[])
{
  QApplicationapp(argc,argv);  //创建一个QApplication的对象,用来管理应用程序所用到的资源,这个QApplication需要两个参数,分别是argc,argv,因为qt支持它自己的一些命令行参数。
  QLabel*label = new QLabel("helloword,i am ghostyu");  //创建一个label窗口部件,不用多说,c++执行new操作,在堆中创建的label指针,注意程序中的*label最后并为通过delete销毁,程序退出后,由操作系统收回堆内存。
  label->show();   //调用显示方法。
  returnapp.exec();   //将应用程序的控制权传递给qt,此时,程序会进入时间循环状态,这是一种等待模式,等待用户的动作。
}

(1)#qmake -project

生成一个与平台无关的项目文件hello.pro

(2)#qmake hello.pro

生成一个与平台相关的makefile文件

(3)#make

构建改程序

linux下运行该程序

#./hello

2.建立连接,响应用户的动作(将用户事件与窗口部件建立联系,以相应用户动作)     

Qt的窗口部件通过发射信号(signal)来表明一个用户动作已经发生了或者是一个状态已经改变了。信号可以与槽(slot)相连接,以便在发射信号时,槽可以得到自动执行。

SIGNAL()和SLOT()是Qt语法中的一部分。

#include <QApplication>
#include <QLabel>
#include <QPushButton>
int main(int argc,char *argv[])
{
        QApplicationapp(argc,argv);
        QPushButton*button = new QPushButton("quit");   <span style="font-size:16px">//注意一定要有后面的new QPushButton</span>
        QObject::connect(button,SIGNAL(clicked()),&app,SLOT(quit()));    <span style="font-size:16px">//QObject::connect(发送者,什么信号,接受者,怎么办)</span>
        button->show();
        returnapp.exec();
}
注意:QObject::connect()里接受者对象前面加采用引用&app

3.窗口部件的布局

布局(layout)

QHBoxlayout    从左到右水平排列
QVBoxLayout    从上到下竖直排列
QGridLayout    把各个窗口排列在一个网格里

Qt程序员最常使用的构建用户接口的方法是先声明所需的窗口部件,然后再设置它们所对应具备的属性,然后把这些窗口不见添加到布局中,布局会自动设置它们的位置和大小。利用Qt的信号和槽机理,并通过窗口部件之间的连接就可以管理用户的交互行为。

#include <QApplication>
#include <QHBoxLayout>
#include <qslider.h>   //滑动条
#include <qspinbox.h>

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);

    window->show();

    return a.exec();
}

Project 3:控件使用
#include <QApplication>
#include <QSlider>  // 滑动条
#include <QSpinBox> //旋转盒
#include <QHBoxLayout> //水平排列
int main(int argc,char *argv[])
{
QApplication app(argc,argv);
QWidget *par=new QWidget;//声明一个QWidget作窗口
par->setWindowTitle("Enter Your Age");//设置窗口的标题
QSlider *slider=new QSlider;//声明一个滑动条
QSpinBox *spinBox=new QSpinBox;//声明一个旋转盒
slider->setRange(0,100);//设置滑动条的范围
spinBox->setRange(0,100);//设置旋转盒的取值范围
QObject::connect(slider,SIGNAL(valueChanged(int)),spinBox,SLOT(setValue(int)));//滑动条值变化引起旋转盒值的变化
QObject::connect(spinBox,SIGNAL(valueChanged(int)),slider,SLOT(setValue(int)));//旋转盒值的变化引起滑动条值的变化 ,这里的两个就不用加& 因为没有本质改变这两个控件
spinBox->setValue(35);//初始一个spinBox的值
//布置一下控件格局layout
QHBoxLayout *layout=new QHBoxLayout;//声明一个布局

layout->addWidget(spinBox);//把两个控件加到格局中
layout->addWidget(slider);//格局有了,把窗口的格局设置成这个
par->setLayout(layout);
par->show();
return app.exec();
}
//细节总结:QWidget类->setWindowTitle("blabla");QWidget类->setLayout(某layout类成员);
   声明。触发事件的连接QObject::connect();     声明横向的QSlider slider=new slider(Qt::Horizontal)
   新遇到的类:QHBoxLayout横向排列;  QVBoxLayout垂直排列;QGridLayout按矩阵方式排列
虽然我们还没有看见spinBox 和slider 控件的大小和位置,它们已经水平排列好了。
QHBoxLayout 能合理安排它们。我们不用在程序中考虑控件在屏幕上的大小和位置这些头
疼的事情了,交给布局管理器就万事大吉。
在Qt 中建立用户界面就是这样简单灵活。程序员的任务就是实例化所需要的控件,按
照需要设置它们的属性,把它们放到布局管理器中。界面中要完成任务由Qt 的signal 和
slot 完成。

4.Qt参考文档

Qt Assistant

UNIX下在终端输入assistant命令,通过继承得到的函数的文档会显示在它的基类中。

书有每章书签。 -------- 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、付费专栏及课程。

余额充值