第8课 - 启航!第一个应用实例

1、计算器程序界面分析 


2、QLineEdit组件

            QLineEdit用于接受用户输入 

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

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

            QLineEdit能够在父组件中进行定位 

         

3、设计与实现 

        界面设计

            -定义组件间的间隔 

                Space = 10px 

            -定义按钮组件的大小 

                Width = 40px, Height = 40px 

            -定义文本框组件的大小 

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

        

        考虑的问题

            -计算器程序不需要最大化和最小化按钮 

            -计算器程序的窗口最好是固定大小 

             - 文本框不能直接输入字符 

        解决方案

                在Qt帮助文档里寻到属性和函数等解决问题


4、编程实验 

计算机器界面实现 
#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;
}

                                            

                                                        将会在后续一步步完善

5、小结 

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

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

                -界面决定最终用户的体验 

                -界面细节是GUI应用程序品质的重要体现 

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

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


猜你喜欢

转载自blog.csdn.net/qq_39654127/article/details/81016377