zoukankan      html  css  js  c++  java
  • Qt学习(一)

                                                                                                         QT学习(一)基础

    一、hello world和QPushbutton

    1.创建一个空的QT

    2.增加一个空的源文件main

    3.在.pro文件中加入

    QT +=widgets gui

    4.在main中编写代码

    /*应用程序抽象类*/
    #include<QApplication>
    /*窗口类*/
    #include<QWidget>
    //按钮
    #include<QPushButton>
    int main(int argc,char *argv[])
    {
        QApplication app(argc,argv);
        /*构造一窗口*/
        QWidget w;
        /*设置窗口的名称*/
        w.setWindowTitle("Hello World");
    
        /*按钮也是一个窗口*/
        QPushButton button;
        button.setText("Button");
        /*窗口对象的父子关系影响,影响显示位置*/
        /*没有父窗口的窗口,我们称之为主窗口*/
        button.setParent(&w);
    
        /*QT对c++的拓展*/
        //std::bind std::function
        //信号和槽
        QObject::connect(&button,SIGNAL(clicked(bool)),&w,SLOT(close()));
    
        /*显示窗口*/
        w.show();
        /*在exec中有一个消息循环,保持窗口显示后补消失*/
        return app.exec();
    }

    二、QLineEdit

    /*应用程序抽象类*/
    #include<QApplication>
    /*窗口类*/
    #include<QWidget>
    #include<QLineEdit>
    #include<QCompleter>
    int main(int argc,char *argv[])
    {
        QApplication app(argc,argv);
        /*构造一窗口*/
        QWidget w;
        /*设置窗口的名称*/
        w.setWindowTitle("Hello World");
    
        QLineEdit edit;
        edit.setParent(&w);
    
        /*输入密码*/[]
        //edit.setEchoMode(QLineEdit::Password);
        //获取输入的内容
        edit.text();
        edit.setPlaceholderText("Please input number:");//输入提示
        QCompleter completer(QStringList()<<"aav"<<"123"<<"998");
        completer.setFilterMode(Qt::MatchContains);//设置匹配格式
        edit.setCompleter(&completer);//提示自动补全
        /*显示窗口*/
        w.show();
        /*在exec中有一个消息循环,保持窗口显示后补消失*/
        return app.exec();
    }

    三、坐标系统和layout

    /*应用程序抽象类*/
    #include<QApplication>
    /*窗口类*/
    #include<QWidget>
    #include<QLineEdit>
    #include<QCompleter>
    #include<QPushButton>
    #include<QVBoxLayout>
    #include<QHBoxLayout>
    #include<QGridLayout>
    #include<QLabel>
    int main(int argc,char *argv[])
    {
        QApplication app(argc,argv);
        /*构造一窗口*/
        QWidget w;
        /*设置窗口的名称*/
        w.setWindowTitle("Hello World");
    
        QPushButton button;
        button.setText("Button");
       // button.setParent(&w);
        //button.setGeometry(30,30,100,30);//针对于父窗口坐标体系的值
    
        QLineEdit edit;
       // edit.setParent(&w);
    #if 0
        QVBoxLayout layout;
        QHBoxLayout layout;//加入layout后,前面的设置部件的父窗口的部分就可以不要了
        layout.addStretch(1);
        layout.addWidget(&button,1);
        layout.addSpacing(50);
        layout.addWidget(&edit,1);//数字可以控制部件的比重
        layout.addStretch(1);//在后边加了一个弹簧
    #endif
    #if 0
        QGridLayout layout;
        layout.addWidget(&button,1,1);
        layout.addWidget(&edit,1,2);
        layout.addWidget(new QPushButton("1,0"),2,2);
        layout.addWidget(new QPushButton("1,1"),2,1);
        //格子的合并
        layout.addWidget(new QPushButton("aaa"),3,1,1,2);//从3,1的位置开始的1行2列
    
        layout.setColumnStretch(3,1);//列的弹簧
        layout.setRowStretch(4,1);
        layout.setColumnStretch(0,1);//列的弹簧
        layout.setRowStretch(0,1);
    #endif
    //layout的合并
    
        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);
    
        password->setEchoMode(QLineEdit::Password);
    
        QHBoxLayout *hBox;
        layout.addLayout(hBox=new QHBoxLayout,3,2);
        hBox->addStretch(1);
        hBox->addWidget(new QPushButton("login") );
        /*显示窗口*/
        w.setLayout(&layout);
        w.show();
    
        /*在exec中有一个消息循环,保持窗口显示后补消失*/
        return app.exec();
    }



  • 相关阅读:
    Redis配置文件详解
    linux系统配置Apache虚拟主机实例
    nginx File not found 错误分析与解决方法
    svn配置使用
    linux下svn命令使用大全
    Kendo UI For ASP.NET MVC项目资源
    ReSharper 配置及用法
    SQL判断某列中是否包含中文字符、英文字符、纯数字 (转)
    Visual Studio最好用的快捷键
    19个必须知道的Visual Studio快捷键
  • 原文地址:https://www.cnblogs.com/rainbow1122/p/8136713.html
Copyright © 2011-2022 走看看