QT实验分析教程 计算器程序界面设计(6)
计算器程序界面
定义组件间的间隔:Space = 10px
定义按钮组件的大小:Width = 40px,Height = 40px
定义文本框组件的大小:Width = 5 * 40px + 4 * 10px, Height = 30px
计算器程序不需要最大化和最小化按钮。
计算器程序的窗口应该是固定大小。
文本框不能直接输入字符。
示例程序:
main.cpp
#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;
}
结果如下:
小结
GUI应用程序开发应该必须先进性界面设计
GUI应用程序界面需要考虑各个细节
- 界面决定最终用户的体验
- 界面细节是GUI应用程序品质的重要体现
Qt库有能力实现各种GUI应用程序需求
Qt帮助文档的使用对于开发是非常重要的