QT——第一个GUI应用程序

  1. QLineEdit组件
  • 用于接收用户输入

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

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

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

    #include "Widget.h"
    #include <QtGui>
    #include <QApplication>
    #include <QLabel>
    #include <QLineEdit>

    int main(int argc, char *argv[])
    {
    QApplication a(argc, argv);
    Widget w;
    QLineEdit le(&w);
    le.setAlignment(Qt::AlignRight); //设置显示的字符串向右边看齐,如果不写则默认向左对齐
    le.move(10,10); //移动到坐标(10,10)
    le.resize(240,30); //设置宽和高

    复制代码
      return QCoreApplication::exec();

    }

2.程序设计与实现

界面设计

  • 定义组件间的间隔 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);
    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); //指定这个按钮的父窗口是 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]->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(); //调用 exec() 后,程序会进入一个无限循环,等待用户操作,只要窗口不关闭,循环就一直跑。
      delete w;
      return ret; //把退出码返回给操作系统。

    }

3.计算器界面代码重构

重构:以改善代码质量为目的代码重写,使其软件的设计和架构更加合理,提高软件的扩展性和维护性

将上述代码进行重构:

QCalculator类继承了QWidget、QLineEdit和QPushButton

QCalculatorui.h

复制代码
#ifndef QCALCULATORUI_H_
#define QCALCULATORUI_H_

#include <QWidget>
#include <QLineEdit>
#include <QPushButton>

class QCalculatorUI : public QWidget
{
private:
    QLineEdit* m_edit;
    QPushButton* m_buttons[20];
    QCalculatorUI(); //使用二阶构造法
    bool construct();
public:
    static QCalculatorUI* NewInstance();
    void show();
    ~QCalculatorUI();
};

#endif // QCALCULATORUI_H

QCalculatorui.cpp

复制代码
#include "QCalculatorui.h"

QCalculatorUI::QCalculatorUI() : QWidget(NULL, Qt::WindowCloseButtonHint) //QWidget没有父类,所以为NULL
{

}
bool QCalculatorUI::construct(){
    bool ret = true;
    const char* btnText[20] =
    {
            "7", "8", "9", "+", "(",
            "4", "5", "6", "-", ")",
            "1", "2", "3", "*", "<-",
            "0", ".", "=", "/", "C",
    };
    m_edit = new QLineEdit(this);

    if( m_edit != NULL){
        m_edit->move(10, 10);
        m_edit->resize(240, 30);
        m_edit->setReadOnly(true); //设置文本框属性为只读,不可编辑
    }
    else{
        ret = false;
    }
    for(int i = 0; (i<4) && ret; i++) {
        for(int j = 0; (j < 5) && ret; j++){
            m_buttons[i*5 + j] = new QPushButton(this); //指定这个按钮的父窗口
            if( m_buttons[i*5 + j] != NULL ){
                m_buttons[i*5 + j]->resize(40, 40);
                m_buttons[i*5 + j]->move(10 + (10 + 40)*j, 50 + (10 + 40)*i);
                m_buttons[i*5 + j]->setText(btnText[i*5 + j]);
            }
            else{
                ret = false;
            }
        }
    }
    return ret;
}

QCalculatorUI* QCalculatorUI::NewInstance(){
    QCalculatorUI* ret = new QCalculatorUI();
    if( (ret== NULL) || !ret->construct() )
    {
        delete ret;
        ret = NULL;
    }
    return ret;
}
void QCalculatorUI::show(){
    QWidget::show();
    setFixedSize(width(), height());
}
QCalculatorUI::~QCalculatorUI(){

}

main.cpp

复制代码
#include <QApplication>
#include <QWidget>
#include <QLineEdit>
#include <QPushButton>
#include "QCalculatorui.h"

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    QCalculatorUI* cal = QCalculatorUI::NewInstance();

    int ret = 0;

    if(cal != NULL){
        cal->show();
        ret = a.exec();
        delete cal;
    }
    return ret; //把退出码返回给操作系统。
}
相关推荐
sycmancia2 小时前
Qt——窗口部件及窗口类型、坐标系统
开发语言·qt
火山上的企鹅2 小时前
QGC 二次开发实战:Android 单机离线授权怎么落地
android·qt·qgroundconrol·离线授权
不被定义的~wolf2 小时前
qt小游戏——坦克大作战
开发语言·qt
冉佳驹2 小时前
Qt 开发【第四篇】——— 常用基础、显示及输入控件核心特性概述
qt·qwidget·table widget·tree widget·text edit·boxlayout·radio button
问水っ3 小时前
Qt Creator快速入门 第三版 第7章 Qt对象模型与容器类
开发语言·qt
郝学胜-神的一滴3 小时前
图形学基础:OpenGL、图形引擎与IG的核心认知及核心模式解析
开发语言·c++·qt·程序人生·图形渲染
yunn_3 小时前
Qt智能指针
c++·qt
我在人间贩卖青春21 小时前
Qt 项目发布
qt·项目发布
不才小强1 天前
Qt开发实战:屏幕录制项目中学习到的知识与遇到的难题
qt·音视频