当前位置:   article > 正文

Qt2:QtCreator的认识_qt与qtcreator对应的版本

qt与qtcreator对应的版本

Qt2:QtCreator的认识

一:QtCreator的使用

QtCreator:
在这里插入图片描述
Qt Creator是跨平台的 Qt IDE,Qt Creator 是 Qt 被 Nokia 收购后推出的一款新的轻量级集成开发环境(IDE),能够帮助我们快速的进行Qt项目的开发。

双击QtCreator即可以进入:
在这里插入图片描述
示例:
在这里插入图片描述
一个Qt计算器项目:
在这里插入图片描述

二:用QtCreator新建空项目

newProject—>其他项目–>Empty qmake Project–>choose
在这里插入图片描述
选择路径,为项目起名
在这里插入图片描述
下一步:
在这里插入图片描述
完成:
在这里插入图片描述
新建成的空项目
在这里插入图片描述
QtCreator的视图切换
在这里插入图片描述

添加文件:右键–>添加新文件
在这里插入图片描述
下一步:
在这里插入图片描述
完成:
在这里插入图片描述
结果:
在这里插入图片描述

三:Hello程序的运行

//hello.cpp
#include<QApplication>
#include<QLabel>//标签
int main(int argc,char**argv)
{
    QApplication app(argc,argv);
    
    //Qt中有内存回收机制,new只后不需要我们进行delete
    QLabel* label = new QLabel("Hello Qt!!!");
    label->show();

    return app.exec();
}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14

另外在配置文件中加上QT+=widgets
在这里插入图片描述
运行结果:
在这里插入图片描述

Qt的信号槽机制

#include<QApplication>
#include<QLabel>//标签
#include<QPushButton>

int main(int argc,char**argv)
{
    QApplication app(argc,argv);

    //Qt中有内存回收机制,new只后不需要我们进行delete
    //QLabel* label = new QLabel("Hello Qt!!!");
    //label->show();

    QPushButton* pushButton = new QPushButton("测试按钮");
    //Qt的信号槽机制
    //QObject是Qt的一个基类
    //参数:组件 SIGNAL是一个宏
    //点击pushButton的时候,发出一个信号SIGNAL(clicked())(点击信号),信号与槽进行连接。让app执行quit()指令。
    QObject::connect(pushButton,SIGNAL(clicked()),&app,SLOT(quit()));

    pushButton->show();
    

    return app.exec();
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24

利用Qt的信号槽机制当我们点击测试按钮时,Qt会进行退出。

Qt的布局

当我们需要下面这样的窗口时,就牵涉到Qt的布局

标签和按钮水平排布,
二者与滑动控件垂直排布
在这里插入图片描述

//hello.cpp
#include<QApplication>
#include<QLabel>//标签
#include<QPushButton>
#include<QWidget>
#include<QHBoxLayout>//水平布局
#include<QVBoxLayout>//垂直布局
#include<QSlider>//滑动控件

int main(int argc,char**argv)
{
    QApplication app(argc,argv);

    //Qt中有内存回收机制,new只后不需要我们进行delete
    //QLabel* label = new QLabel("Hello Qt!!!");
    //label->show();

    //QPushButton* pushButton = new QPushButton("测试按钮");
    //Qt的信号槽机制
    //QObject是Qt的一个基类
    //参数:组件 SIGNAL是一个宏
    //点击pushButton的时候,发出一个信号SIGNAL(clicked())(点击信号),信号与槽进行连接。让app执行quit()指令。
    //QObject::connect(pushButton,SIGNAL(clicked()),&app,SLOT(quit()));
    //pushButton->show();
    
    //创建窗口
    QWidget* widget = new QWidget;
    widget->setWindowTitle("今天是情人节……");
    //label:标签
    QLabel* label = new QLabel("Hello Qt!!!",widget);
    //button:按钮
    QPushButton* pushButton = new QPushButton("退出",widget);
    //hLayout:水平排布
    QHBoxLayout* hLayout = new QHBoxLayout;
    hLayout->addWidget(label);          //为水平布局加入label标签
    hLayout->addWidget(pushButton);     //为水平布局加入pushButton按钮

    //widget->setLayout(hLayout);         //给widget设置一个布局hLayout
    //slider:滑动控件   Qt::Horizontal表明滑动控件是水平的
    QSlider* slider = new QSlider(Qt::Horizontal);

    QVBoxLayout* vLayout = new QVBoxLayout;
    vLayout->addLayout(hLayout);        //为垂直布局vLayout加入水平布局hLayout
    vLayout->addWidget(slider);         //为垂直布局vLayout加入滑动控件slider
    widget->setLayout(vLayout);

    widget->show();

    return app.exec();
}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/神奇cpp/article/detail/1020399
推荐阅读
相关标签
  

闽ICP备14008679号