QT 开发学习 一

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

大家好,欢迎来到IT知识分享网。

一 环境的配置

 个人是建议使用一些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

免责声明:本站所有文章内容,图片,视频等均是来源于用户投稿和互联网及文摘转载整编而成,不代表本站观点,不承担相关法律责任。其著作权各归其原作者或其出版社所有。如发现本站有涉嫌抄袭侵权/违法违规的内容,侵犯到您的权益,请在线联系站长,一经查实,本站将立刻删除。 本文来自网络,若有侵权,请联系删除,如若转载,请注明出处:https://haidsoft.com/147598.html

(0)
上一篇 2025-04-06 15:15
下一篇 2025-04-06 15:26

相关推荐

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注微信