• Qt学习08 启航!第一个应用实例


    Qt学习08 启航!第一个应用实例

    计算器程序界面分析

    在这里插入图片描述

    QLineEdit组件

    • QLineEdit用于接受用户输入
    • QLineEdit能够获取用户输入的字符串
    • QLineEdit是功能性组件,需要父组件作为容器
    • QLineEdit能够在父组件中进行定位
    QWidget w;							// 生成QWidget对象,顶级组件
    QLineEdit le(&w);					// 生成QLineEdit对象,其父组件为QWidget
    le.setAlignment(Qt::AlignRight);	// 设置显示字符串向右边对其
    le.move(10, 10);					// 移动到坐标(10, 10)
    le.resize(240, 30);					// 设置大小width=240, height=30
    
    • 1
    • 2
    • 3
    • 4
    • 5

    设计与实现

    • 界面设计

      • 定义组件间的间隔

        Space = 10px

      • 定义按钮组件的大小

        Width = 40px,Height = 40px

      • 定义文本框组件的大小

        Width = 5 * 40px + 4 * 10px,Height = 30px

    在这里插入图片描述

    • 存在的问题
      • 计算器程序不需要最大化和最小化按钮
      • 计算器程序的窗口应该是固定大小
      • 文本框不能直接输入字符
    #include <QtGui/QApplication>
    #include <QWidget>
    #include <QLineEdit>
    #include <QPushButton>
    
    int main(int argc, char *argv[])
    {
        QApplication a(argc, argv);
        QWidget* w = new QWidget(NULL, Qt::WindowCloseButtonHint);
        QLineEdit* le = new QLineEdit(w);
        QPushButton* button[20] = {0};
        const char* btnText[20] =
        {
            "7", "8", "9", "+", "(",
            "4", "5", "6", "-", ")",
            "1", "2", "3", "*", "<-",
            "0", ".", "=", "/", "C",
        };
        int ret = 0;
    
        le->move(10, 10);
        le->resize(240, 30);
        le->setReadOnly(true);
    
        for(int i=0; i<4; i++)
        {
            for(int j=0; j<5; j++)
            {
                button[i*5 + j] = new QPushButton(w);
                button[i*5 + j]->resize(40, 40);
                button[i*5 + j]->move(10 + (10 + 40)*j, 50 + (10 + 40)*i);
                button[i*5 + j]->setText(btnText[i*5 + j]);
            }
        }
    
        w->show();
        w->setFixedSize(w->width(), w->height());
        
        ret = a.exec();
    
        delete w;
    
        return ret;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44

    小结

    • GUI应用程序开发前应前必须先进行界面设计
    • GUI应用程序界面需要考虑各个细节
      • 界面决定最终用户的体验
      • 界面细节是GUI应用程序品质的重要体现
    • Qt库有能力实现各种GUI应用程序需求
      必须先进行界面设计**
    • GUI应用程序界面需要考虑各个细节
      • 界面决定最终用户的体验
      • 界面细节是GUI应用程序品质的重要体现
    • Qt库有能力实现各种GUI应用程序需求
    • Qt帮助文档的使用对于开发是非常重要
  • 相关阅读:
    node.js基础学习
    C++ vector容器 常用 API 操作
    Unity 热更--AssetBundle学习笔记 0.8
    设计模式-创建型-抽象工厂模式-Abstract Factory
    计算连续性状的PRS得分
    UI自动化测试如何走出困境?价值又如何体现?
    2024 RubyMine 激活,分享几个RubyMine 激活的方案
    vulnhub之cereal
    【计算机网络】 RTT和RTO
    eclipse写xsd报错:s4s-elt-schema-ns: The namespace of element...且没有自动补全类提示 问题的解决方法
  • 原文地址:https://blog.csdn.net/weixin_40743639/article/details/125468307