1. QLineEdit组件
(1)QLineEdit用于接受用户输入
(2)QLineEdit能够获取用户输入的字符串
(3)QLineEdit是功能性组件,需要父组件作为容器
(4)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
2. 设计与实现
(1)界面设计
①定义组件间的间隔: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);//le的生命期由父组件来管理 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 + j * 50, 50 + i * 50); button[i * 5 + j]->setText(btnText[i * 5 + j]); } } w->show(); w->setFixedSize(w->width(), w->height()); //固定窗口大小 ret = a.exec(); delete w; return ret; }
(2)存在问题及解决
①不需要最大化和最小化按钮:窗口风格Qt::WindowCloseButtonHint
②程序窗口应该是固定大小的:QWidget类的setFixedSize
③文本框不能直接输入字符:QLineEdit的setReadOnly(true);
3. 小结
(1)GUI应用程序开发应该前必须先进行界面设计
(2)GUI应用程序界面需要考虑各个细节
(3)Qt库有能力实现各种GUI应用程序需求
(4)Qt帮助文档的使用对于开发是非常重要的
原文链接: https://www.cnblogs.com/5iedu/p/5432943.html
欢迎关注
微信关注下方公众号,第一时间获取干货硬货;公众号内回复【pdf】免费获取数百本计算机经典书籍;
也有高质量的技术群,里面有嵌入式、搜广推等BAT大佬
原创文章受到原创版权保护。转载请注明出处:https://www.ccppcoding.com/archives/394383
非原创文章文中已经注明原地址,如有侵权,联系删除
关注公众号【高性能架构探索】,第一时间获取最新文章
转载文章受原作者版权保护。转载请注明原作者出处!