zoukankan      html  css  js  c++  java
  • 第8课 启航!第一个应用实例

    1. QLineEdit组件

    (1)QLineEdit用于接受用户输入

    (2)QLineEdit能够获取用户输入的字符串

    (3)QLineEdit是功能性组件,需要父组件作为容器

    (4)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

    2. 设计与实现

    (1)界面设计

      ①定义组件间的间隔:Space = 10px

      ②定义按钮组件的大小:Width = 40px, Height = 40px

      ③定义文本框组件的大小:Width = 5 * 40px + 4 * 10px,Height = 30px

     

    【编程实验】计算机器界面实现

    #include <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);//le的生命期由父组件来管理
        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 + j * 50, 50 + i * 50);
                button[i * 5 + j]->setText(btnText[i * 5 + j]);
            }
        }
    
        w->show();
        w->setFixedSize(w->width(), w->height()); //固定窗口大小
    
        ret = a.exec();
    
        delete w;
    
        return ret;
    }

    (2)存在问题及解决

      ①不需要最大化最小化按钮:窗口风格Qt::WindowCloseButtonHint

      ②程序窗口应该是固定大小的:QWidget类的setFixedSize

      ③文本框不能直接输入字符:QLineEdit的setReadOnly(true);

    3. 小结

    (1)GUI应用程序开发应该前必须先进行界面设计

    (2)GUI应用程序界面需要考虑各个细节

    (3)Qt库有能力实现各种GUI应用程序需求

    (4)Qt帮助文档的使用对于开发是非常重要的

  • 相关阅读:
    Chapter 12 homework
    copy construction note
    Chapter 11 homework
    数组排序最大元素
    temporary Object and destructor
    strcpy() 函数注意的地方
    结对项目第二次作业
    结队项目——第一次作业
    软件工程实践2017第二次作业
    软件工程实践2017第一次作业
  • 原文地址:https://www.cnblogs.com/5iedu/p/5432943.html
Copyright © 2011-2022 走看看