Qt学习

8_第一个应用实例

2018-01-18  本文已影响14人  编程半岛

声明:此文章仅是本人在学习狄泰QT实验分析课程所做的笔记,文章中包含狄泰软件资料内容,一切版权归狄泰软件所有!
实验环境:ubuntu10 + Qt Creator2.4.1 + Qt SDK 4.7.4

1. 计算器程序界面分析

2. QLineEdit组件

#include <QtGui/QApplication>
#include <QWidget>
#include <QLineEdit>
#include "mainwindow.h"

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    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

    w.show();
    
    return a.exec();
}

输出结果:


3. 设计实现

编程说明:计算器界面实现

#include <QtGui/QApplication>
#include <QLineEdit>
#include <QPushButton>
#include <QWidget>

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    QWidget* w = new QWidget(NULL, Qt::Dialog);
    QLineEdit* le = new QLineEdit(w);
    QPushButton* button[20] = {0};
    const char* butText[20] =
    { "7", "8", "9", "+", "(",
      "4", "5", "6", "-", ")",
      "1", "2", "3", "*", "<-",
      "0", ".", "=", "/", "C",
    };


    int ret = 0;

    w->resize(260, 250);
    w->move(10, 10);

    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]->move(10+50*j, 50+50*i);
            button[i*5+j]->resize(40, 40);
            button[i*5+j]->setText(butText[i*5+j]);
        }
    }

    w->show();
    w->setFixedSize(260, 250);

    ret = a.exec();

    delete w;
    delete le;
    delete* button;

    return ret;
}

输出结果i:


4. 小结

上一篇 下一篇

猜你喜欢

热点阅读