简体   繁体   English

如何使用 Qt C++ 隐藏另一个 class ui?

[英]How to hide another class ui using Qt C++?

I've been trying to hide another class ui, but I can't figure it out.我一直试图隐藏另一个 class ui,但我无法弄清楚。

As an example I have the MainWindow class and other two classes.例如,我有 MainWindow class 和其他两个类。 This is a scheme of the MainWindow ui:这是 MainWindow ui 的方案:

[---------------][---------------] [---------------][---------------]

[ Class A ui ][ Class B ui ] [ Class A 用户界面 ][ Class B 用户界面 ]

[---------------][---------------] [---------------][---------------]

The top & bottom lines are just to outline the window.顶部和底部线只是概述了 window。 You can imagine it as a rectangle containing two rectangles with the classes ui inside.你可以把它想象成一个包含两个矩形的矩形,里面有 ui 类。

When I click on a QPushButton in class AI want to hide class B ui.当我单击 class AI 中的QPushButton时,我想隐藏 class B ui。

This is a code snippet:这是一个代码片段:

ClassA.h类A.h

 #ifndef CLASSA_H #define CLASSA_H #include <QWidget> namespace Ui { class ClassA; } class ClassA: public QWidget { Q_OBJECT public: explicit ClassA(QWidget *parent = nullptr); ~ClassA(); public slots: void on_pushButton_clicked(); private: Ui::ClassA *ui; }; #endif // CLASSA_H
ClassA.cpp A类.cpp
 #include "classa.h" #include "ui_classa.h" ClassA::ClassA(QWidget *parent): QWidget(parent), ui(new Ui::ClassA) { ui->setupUi(this); } ClassA::~ClassA() { delete ui; } void classA::on_pushButton_clicked() { // code to hide ClassB ui }

ClassB & MainWindow are brand new (standard). ClassB 和 MainWindow 是全新的(标准)。 ClassB ui contains some labels, pushButtons,.... MainWindow ui contains two QWidgets that are promoted to ClassA ui & ClassB ui. ClassB ui 包含一些标签,pushButtons,.... MainWindow ui 包含两个 QWidget,它们被提升为 ClassA ui 和 ClassB ui。

What code should I write in the on_pushButton_clicked() slot?我应该在on_pushButton_clicked()插槽中编写什么代码? I've made some tries but nothing seems to work.我做了一些尝试,但似乎没有任何效果。

Any help would be much appreciated.任何帮助将非常感激。

one way is to add a signal in ClassA and emit it when clicking on the considered push button:一种方法是在 ClassA 中添加一个信号并在单击考虑的按钮时发出它:

 #ifndef CLASSA_H #define CLASSA_H #include <QWidget> namespace Ui { class ClassA; } class ClassA: public QWidget { Q_OBJECT public: explicit ClassA(QWidget *parent = nullptr); ~ClassA(); public signals: void notifyButtonClicked(); public slots: void on_pushButton_clicked(); private: Ui::ClassA *ui; }; #endif // CLASSA_H

and in classA.cpp we have:在 classA.cpp 我们有:

 void classA::on_pushButton_clicked() { emit notifyButtonClicked(); }

now you can write this code in mainwindow constructor (assume _A is an object of ClassA):现在您可以在主窗口构造函数中编写此代码(假设 _A 是 ClassA 的 object):

 connect(_A, &ClassA::notifyButtonClicked, this, [] { // some code for hiding ClassB });

I haven't tested this, because I dont have Qt on this machine, but that should work, with possible minor modifications.我没有对此进行测试,因为我在这台机器上没有 Qt,但这应该可以工作,可能需要稍作修改。 If any errors appear, let me know and I will help.如果出现任何错误,请告诉我,我会提供帮助。 Also, read the signals and slots documentation.另外,请阅读信号和插槽文档。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM