zoukankan      html  css  js  c++  java
  • 第6课 窗口部件及窗口类型

    1. 窗口组件

    (1)图形用户界面由不同的窗口和窗口组件构成

    (2)<QtGui>头文件包含窗口组件,对应Qt中的GUI模块

    (3)Qt以组件对象的方式构建图形用户界面

    (4)组件类型

      ①容器类(父组件):用于包含其他的界面组件。Qt中没有父组件的顶级组件窗口

      ②功能类(子组件):用于实现特定的交互功能

     

    2. 窗口组件的类继承图

    (1)QWidget继承图

     

      ①QObject是所有支持Qt对象模型的基类

      ②QWidget类继承自QObject类和QPaintDevice类

      ③QPaintDevice是Qt中所有可绘制组件的基类

    (2)QWidget组件

      ①QWidget能够绘制自己处理用户的输入

      ②QWidget是Qt中所有窗口组件类的父类(super class)

      ③QWidget是所有窗口组件的抽象

      ④Qt中每个窗口组件都是一个QWidget

      ⑤QWidget类对象常作为父组件顶级组件使用

    【编程实验】QWidget初探

        注意:Qt→创建项目→选择“Application模板” →“Qt Widgets Application” →……→“基类”中选择“QWidget”并且去掉创建界面勾选

    //Widget.h

    #ifndef WIDGET_H
    #define WIDGET_H
    
    #include <QWidget>
    
    class Widget : public QWidget
    {
        Q_OBJECT
    
    public:
        Widget(QWidget *parent = 0);
        ~Widget();
    };
    
    #endif // WIDGET_H
    View Code

    //Widget.cpp

    #include "Widget.h"
    
    Widget::Widget(QWidget *parent)
        : QWidget(parent)
    {
    }
    
    Widget::~Widget()
    {
    
    }
    View Code

    //main.cpp

    #include "Widget.h"
    #include <QApplication>
    
    int main(int argc, char *argv[])
    {
        QApplication a(argc, argv);
        Widget w;
        w.setWindowTitle("Hello World");
        w.show();
    
        return a.exec();
    }

    3. QLabel组件

    (1)QLabel用于显示一个提示性的字符串

    (2)QLabel是功能性组件,一般需要父组件作为容器

    (3)QLabel可以作为窗口存在,但没有什么意义。

    QWidget w;   //生成QWidget对象,顶级组件
    
    QLabel l(&w);//生成QLabel对象,其父组件为QWidget
    
    //设置QLabel组件的显示字符串
    
    l.setText("This is a label control");

    【编程实验】QLabel组件的使用

    #include "Widget.h"
    #include <QApplication>
    #include <QLabel>
    
    int main(int argc, char *argv[])
    {
        QApplication a(argc, argv);
        Widget w;
        QLabel l(&w);
    
        l.setText("I'm a label control.");
        w.setWindowTitle("Hello World");
        w.show();
    
        return a.exec();
    }

    4. 窗口式样

    (1)窗口类型

      ①Qt::Dialog——对话框类型

      ②Qt::Window——主窗口类型

      ③Qt::SplashScreen——启动画面类型

      ④……

    (2)窗口标志

      ①Qt::WindowStaysOnTopHint——置顶

      ②Qt::WindowContextHelpButtonHint——添加问号和关闭按钮,像对话框一样

    【编程实验】窗口类型与窗口标志

    #include "Widget.h"
    #include <QApplication>
    #include <QLabel>
    
    int main(int argc, char *argv[])
    {
        QApplication a(argc, argv);
    
        //注意是QWidget类,置顶窗口并添加问号和关闭按钮,像对话框一样
        QWidget w(NULL,Qt::Window | Qt::WindowStaysOnTopHint | Qt::WindowContextHelpButtonHint);
        QLabel l(&w);
    
        l.setText("I'm a label control.");
        w.setWindowTitle("Hello World");
        w.resize(400, 300);  //窗口大小
        w.show();
    
        return a.exec();
    }

    5. 小结

    (1)Qt以组件对象的方式构建图形用户界面

    (2)QWidget类是所有用户界面组件的父类

    (3)QWidget类对象常作为父组件或顶级组件使用

    (4)Qt中可以根据需要定制窗口式样

    (5)QLabel用于显示一个提示性的字符串

  • 相关阅读:
    hdu6229 Wandering Robots 2017沈阳区域赛M题 思维加map
    hdu6223 Infinite Fraction Path 2017沈阳区域赛G题 bfs加剪枝(好题)
    hdu6438 Buy and Resell 买卖物品 ccpc网络赛 贪心
    hdu6441 Find Integer 求勾股数 费马大定理
    bzoj 1176 Mokia
    luogu 3415 祭坛
    bzoj 1010 玩具装箱
    bzoj 3312 No Change
    luogu 3383【模板】线性筛素数
    bzoj 1067 降雨量
  • 原文地址:https://www.cnblogs.com/5iedu/p/5428704.html
Copyright © 2011-2022 走看看