1.下载QT5
2.导入VS(添加QT的头文件路径,以及库文件路径)
3.
#include "testqt.h"
#include <QPushButton>
#include <QtWidgets/QApplication>
#include <QlineEdit>
#include <QCompleter>
#include <QVBoxLayout>
#include <QHBoxLayout>
#include <QGridLayout>
#include <QLabel>
//#define BTN_1
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QWidget w;
/*************QT的消息槽机制,类似与MFC的消息映射**********/
#ifdef BTN_1
// 按钮的处理消息
QPushButton btn;
btn.setText("");
btn.setParent(&w);
btn.show();
// QT对c++的扩展(消息槽,信号和槽绑定)
QObject::connect(&btn, SIGNAL(clicked()), &w, SLOT(close()));
#endif
#ifdef EDIT_1
// 编辑框的处理
QLineEdit edit;
edit.setEchoMode(QLineEdit::PasswordEchoOnEdit);/*NoEcho,Password,PasswordEchoOnEdit*/;
//edit.text(); /*获取文字*/
//edit.setPlaceholderText("Please input text");
edit.show();
edit.setParent(&w);
QStringList list;
list.append("abb");
list.append("ccc");
list.append("112");
QCompleter completer(list);
completer.setFilterMode(Qt::MatchContains);
edit.setCompleter(&completer);
#endif
/******************************************************/
// 分层处理(QLayout的派生类)
/*
QVBoxLayout Layout;
Layout.addStretch(0);
Layout.addWidget(new QPushButton("button"), 0);
Layout.addSpacing(20);
Layout.addWidget(new QLineEdit(), 0);
Layout.addStretch(0);
*/
QGridLayout GridLayout;
QLabel label1(QStringLiteral("用户名"));/*QStringLiteral用于将字符转换成UTF-8编码*/
QLabel label2(QStringLiteral("密码"));
QPushButton btn(QStringLiteral("确定"));
QPushButton btn1(QStringLiteral("取消"));
QLineEdit edit_psd;
edit_psd.setEchoMode(QLineEdit::Password);
edit_psd.setPlaceholderText("Please input text");
QLineEdit edit_user;
edit_user.setPlaceholderText("Please input text");
GridLayout.setColumnStretch(0,1); /*注意行和宽要同步设置弹簧*/
GridLayout.setRowStretch(0, 1);
GridLayout.setColumnStretch(4, 1);
GridLayout.setRowStretch(4, 1);
GridLayout.addWidget(&label1, 1, 1);
GridLayout.addWidget(&label2, 2, 1);
GridLayout.addWidget(&edit_user, 1, 2);
GridLayout.addWidget(&edit_psd, 2, 2);
QHBoxLayout layout;
GridLayout.addLayout(&layout, 3, 2); /*层中层*/
layout.addStretch(1);
layout.addWidget(&btn);
layout.addWidget(&btn1);
// 设置窗体大小
w.setGeometry(300, 300, 800, 600);
w.setLayout(&GridLayout);
w.setWindowTitle("hello world");
w.show();
return a.exec(); //此处类似win32以及mfc的消息循环机制,必须加上
QT学习日记(一)
最新推荐文章于 2024-01-15 01:49:35 发布