QT学习日记(一)

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的消息循环机制,必须加上
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值