QT基础

    xiaoxiao2021-03-26  15

    传智课程学习笔记。

    qt creator下创建项目,

    选择空项目,

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

    注意工程文件中要修改,

    添加

    QT += widgets gui

    /* 应用程序抽象类 */ #include <QApplication> /*窗口类*/ #include <QWidget> /* 按钮类 */ #include <QPushButton> 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 <QApplication> #include <QWidget> #include <QPushButton> #include <QLineEdit> #include <QCompleter> 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 <QApplication> #include <QWidget> #include <QLabel> #include <QPushButton> #include <QLineEdit> #include <QVBoxLayout> #include <QHBoxLayout> #include <QGridLayout> 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(); }

    转载请注明原文地址: https://ju.6miu.com/read-650000.html

    最新回复(0)