一日磨不出两手茧
计算机程序界面分析
- QLineEdit
- QWIdget
- QPushButton
QLineEdit 组件(文本框)
QLineEdit 用于接受用户的输入
QLineEdit 能够获取用户输入的字符串
QLineEdit 是功能性组件,需要父组件作为容器
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;
界面设计
- 定义组件间的间隔
- - Space = 10px
- 定义组件的按钮大小
- - Width = 40px, Heigth = 40px
- 定义文本框组件的大小
- - Width = 5 *40px + 4 * 10px, Height = 30px
编程实验
计算器界面的额实现
/*
* 先创建主窗口
* 生成一个文本框
* 生成按钮
* 按钮上填写数字
* 完工,检查问题
*/
#include <QtGui/QApplication>
#include <QWidget>
#include <QLineEdit>
#include <QPushButton>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QWidget* w = new QWidget();
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);
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();
ret = a.exec();
return ret;
}
存在的问题
- 计算器程序不需要最大化和最小化按钮
- 计算器程序的窗口应该是固定大小
- 文本框不能直接输入字符
编程实验
计算器界面优化
/*
存在的问题
- 计算器程序不需要最大化和最小化按钮 (思路 : 可以设置顶层窗口的式样)
- 计算器程序的窗口应该是固定大小(思路 : 文档中找QWidget, 输入fix关键词查找)
- 文本框不能直接输入字符(思路 : 就在QLineEdit中找一找 set)
查找相关文档, 多试一试
*/
#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();
return ret;
}
总结:
GUI 应用程序开发前应该必须先进性界面设计
GUI 应用程序界面需要考虑各个细节
- 界面决定最终用户的体验
- 界面细节是GUI应用程序品质的重要体现
Qt 库有能力实现各种 GUI 应用程序需求
Qt 帮助文档的使用对于开发是非常重要的
感谢关注,文章持续高速更新中……