一 环境的配置
个人是建议使用一些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