博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
QT基础
阅读量:3699 次
发布时间:2019-05-21

本文共 3362 字,大约阅读时间需要 11 分钟。

传智课程学习笔记。

qt creator下创建项目,

选择空项目,

选中项目,添加新文件,C++ source file,

注意工程文件中要修改,

添加

QT += widgets gui

/* 应用程序抽象类 */#include 
/*窗口类*/#include
/* 按钮类 */#include
int main(int argc, char* argv[]){ QApplication app(argc, argv); /* 构造一个窗口*/ QWidget w; /*显示窗口*/ w.show(); /* 按钮也是个窗口 */ QPushButton button; button.setText("Button"); /* 窗口对象的父子关系,影响显示位置 */ /* 没有父窗口的窗口,我们称之为主窗口 */ button.setParent(&w);//将按钮放到窗口中,注意我这里的表达方式,而且如果要显示,要放在w.show的前面,如果没有写这句,就单独2个窗口, button.show(); /* QT对C++的拓展,不仅包含,还扩展了C++,这里的信号槽就是, */ //button,w一定要是QObject子类的对象,前面两个叫信号,后面两个叫槽, //信号和槽函数绑定起来,信号会触发后者, QObject::connect(&button, SIGNAL(clicked()), &w, SLOT(close())); w.setWindowTitle("Hello World"); /*在exec中有一个消息循环*/ return app.exec();}

#include 
#include
#include
#include
#include
int main(int argc, char* argv[]){ QApplication app(argc, argv); QWidget w; QLineEdit edit; edit.show(); edit.setParent(&w); //edit.setEchoMode(QLineEdit::Password); //edit.text(); //edit.setPlaceholderText("please input text:"); //就像百度输入时候的提示 QCompleter completer( QStringList()<<"aaa"<<"123"<<"112"); edit.setCompleter( &completer); w.show(); w.setWindowTitle("Hello World"); return app.exec();}

追踪源码到这里,

enum EchoMode {
Normal, NoEcho, Password, PasswordEchoOnEdit };
EchoMode echoMode() const;
void setEchoMode(EchoMode);
Normal:就是普通,

NoEcho:不显示,

Password:密码,

PasswordEchoOnEdit:有点像我们手机输入的样子,

button.setGeometry( 30, 30, 200, 200);
button的位置,位于所在主窗口坐标(30,30)的位置,其长宽为200,200,

上面是通过坐标,

然后还有3中布局的方式,

#include <QVBoxLayout> //竖直布局

#include <QHBoxLayout> //水平布局
#include <QGridLayout> //格子布局

为了将我们的窗口位置能靠左,或者靠右,等等,

我们有两种方法来解决,

加个弹簧,stretch,

添加一些像素,addspacing,

#include 
#include
#include
#include
#include
#include
#include
#include
int main(int argc, char* argv[]){ QApplication app(argc, argv); QWidget w;#if 0 QPushButton button; button.setText("Button"); // button.setParent(&w); button.show(); QLineEdit edit; edit.setParent(&w); QHBoxLayout layout; layout.addStretch(1); layout.addWidget(&button, 1); layout.addSpacing(50); layout.addWidget(&edit, 1); layout.addStretch(1);#endif#if 0 QGridLayout layout; layout.setColumnStretch(3, 1); layout.setRowStretch(4, 1); layout.setColumnStretch(0, 1); layout.setRowStretch(0, 1); layout.addWidget(&button, 1, 1); layout.addWidget(&edit, 1, 2); layout.addWidget(new QPushButton("1, 0"), 2, 1); layout.addWidget(new QPushButton("1, 1"), 2, 2); layout.addWidget(new QPushButton("aaa"), 3, 1, 1, 2);#endif QGridLayout layout; QLineEdit* password; layout.setColumnStretch(3, 1); layout.setRowStretch(4, 1); layout.setColumnStretch(0, 1); layout.setRowStretch(0, 1); layout.addWidget(new QLabel("Username:"), 1, 1); layout.addWidget(new QLineEdit(), 1, 2); layout.addWidget(new QLabel("Password:"), 2, 1); layout.addWidget(password = new QLineEdit(), 2, 2); QHBoxLayout* hBox; layout.addLayout(hBox = new QHBoxLayout, 3, 2); hBox->addStretch(1); hBox->addWidget(new QPushButton("Login")); // layout.addWidget(new QPushButton("登录"), 3, 2); password->setEchoMode(QLineEdit::Password); /*显示窗口*/ w.show(); w.setLayout(&layout); w.setWindowTitle("Hello World"); /*在exec中有一个消息循环*/ return app.exec();}

转载地址:http://glbcn.baihongyu.com/

你可能感兴趣的文章
MySQL性能【索引优化分析】
查看>>
MySQL性能【查询截取分析】
查看>>
MySQL 锁机制
查看>>
3. Shiro 授权
查看>>
Git 版本管理
查看>>
MyBatis Plus 3.X 通俗易懂版教程
查看>>
Java 中一个元素在集合中如何忽略自己循环比对是否存在相同元素
查看>>
JAVA如何计算字符串公式
查看>>
Java 实现 crc modbus 16 位校验算法
查看>>
Java工作中常用方法总结
查看>>
vue 项目修改 title 旁边的 icon 图片
查看>>
Jeecg-Boot前端图片更改(网站 title 图标修改)
查看>>
前端通过 v-html 或 js 都可以替换富文本中的标签
查看>>
自定义注解通过反射实现Excel的导出功能(提供项目源码)
查看>>
SpringBoot + Ant Design Vue 实现 excel 导入功能
查看>>
vscode终端无法执行命令行
查看>>
史上功能最全的Java权限认证框架
查看>>
使用EasyPoi完成复杂一对多excel表格导出功能
查看>>
mybatis 中使用 CASE WHEN 关键字报错问题解决方案
查看>>
VsCode 常用插件
查看>>