QT 开发学习 一

一 环境的配置

 个人是建议使用一些IDE去写QT的代码,毕竟原生的环境实在是有点…比如CLion就很合适,此外还要去下载Qt creator,将其编译器的路径加入到我们的项目路径中去,这部分大家可以在网上搜教程

二 Qt的项目结构

 在QT中主要是以 *.cpp , *.h文件,在cpp文件中去声明类成员函数,在h文件中去声明我们的结构

1. *.h文件的书写

 首先我们要去define这个类

#ifndef SUBWIN_H
#define SUBWIN_H
#include <QWidget>
#include <QPushButton>

 下面的为基础的模版

class Wide : public QWidget {
Q_OBJECT

public:
    explicit Wide(QWidget *parent = nullptr);
    ~Wide() override;
    void  out();
    void showson();
    void doson();
private:
    QPushButton b1;
    QPushButton *b2;
    QPushButton b3;
    Subwin b4{};
   // Ui::Wide *ui;
};
2. *.cpp文件的书写
#include "wide.h"
Wide::Wide(QWidget *parent) : QWidget(parent)
{
}

三 基本库函数的调用

1. <QWidget>

 在QWidget 中你可以得到这个窗口的相关设定

Wide w; // declare a win
w.hide(); // hide it 
w.resize(100,100); // resize the win
w.show(); // show the win
2. <QPushBottom>
QPushBottom b; // declare a bottom object
b.setText("^_^"); // show the text
b.setParent(&w); // give it a parent so that it can show with the parent window
b.resize(100, 100) // resize the bottom
QPushButton b1(&w); // you can also give parent in this way
b1.move(100,100); // move this bottom to a pos
b.show()

四 信号的传递

 在这里我们需要去使用connect函数,在connect中有四个参数

connect(the sender, the action, the receiver, the function);

 以下面的函数为例,发出者是b1这个按钮,发出的动作是按压按钮(其实也有别的函数在QpushButtom),信号的接收者是这个对象本身,执行的命令是关闭窗口

connect(&b1,&QPushButton::pressed, this, &Wide::close);
  b4.show();
   connect(&b1,&QPushButton::pressed, this, &Wide::close);
   // connect(bottom,the press function, this, the close function in the class)
   connect(b2, &QPushButton::released, this, &Wide::out);
   // released means when the pressed is released, the function will work
   connect(b2, &QPushButton::released,&b1,&QPushButton::hide);
   // when you released the b2, the b1 will be hidden
  // connect(b2, &QPushButton::released, &b1, &QPushButton::show);
   connect(&b3,&QPushButton::released, this,&Wide::showson);

   // solve the sign of the son win
   connect(&b4, &Subwin::mysignal, this,&Wide::doson);

五 信号的传递

当一个类为另外一个类的成员时,我们如何通过这个成员类对它的本身进行操作,在这里我们可以使用signal

the declare of the elem class

#ifndef SUBWIN_H
#define SUBWIN_H
#include <QWidget>
#include <QPushButton>

class Subwin :public QWidget{
    Q_OBJECT
public:
    explicit Subwin(QWidget *parent = 0);
    void sendm();
signals:
    // the sign do not have the return type
    void  mysignal();
public slots:
private:
    QPushButton b;
};

#endif

 接收到信号之后去进行发送信号

void Subwin::sendm() {
    emit mysignal();
}

接收到信号之后进行链接

 connect(&b4, &Subwin::mysignal, this,&Wide::doson);

thats all, I will continue to learn it in order to finish my c++ work

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值