使用2类QT之间的信号
我有一个MainWindow类,其中包含Qcombobox和一个来自另一类的小部件。第二类包含QCheckbox和Qcombobox。我想使用信号更改QCHECKBOX的检查状态,当我的Mainwindow中显示的Qcombobox中显示的字符串已更改时,我的QCOMBOBOX中显示的字符串已更改。
但是我不太了解我的信号必须具有哪种形式,以及如何在小部件课程中使用它。
mainwindow.h:
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QWidget>
#include <QComboBox>
#include "devices_left_widget.h"
#define STRING_DEVICE1 "DEVICE1"
#define STRING_DEFAULT ""
class MainWindow : public QMainWindow {
Q_OBJECT
public:
explicit MainWindow(QWidget* parent = nullptr);
signals:
public slots:
void carte_OK();
protected:
QComboBox* carte_type_combo_box;
devices_left_widget* left_widget;
};
#endif // MAINWINDOW_H
device_left_widget.h:
#ifndef DEVICE_LEFT_WIDGET_H
#define DEVICE_LEFT_WIDGET_H
#include <QWidget>
#include <QCheckBox>
#include <QComboBox>
#define STRING_DEVICE1 "DEVICE1"
#define STRING_DEFAULT ""
class device_left_widget : public QWidget {
Q_OBJECT
public:
explicit device_left_widget(QWidget* parent = nullptr);
signals:
public slots:
protected:
QGridLayout* main_grid_layout;
QCheckBox* device_checkbox;
QComboBox* device_type_combo_box;
};
#endif // DEVICES_LEFT_WIDGET_H
I have a MainWindow class which contain a QComboBox and a widget which is from another class. This second class contain a QCheckBox and a QComboBox. I want to use a signal to change the checkState of my QCheckBox and the string displayed in my QComboBox from my widget class when the string displayed in my QComboBox from my MainWindow has changed.
But I don't really understand which form my signal must have and how I can use it in my widget class.
MainWindow.h :
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QWidget>
#include <QComboBox>
#include "devices_left_widget.h"
#define STRING_DEVICE1 "DEVICE1"
#define STRING_DEFAULT ""
class MainWindow : public QMainWindow {
Q_OBJECT
public:
explicit MainWindow(QWidget* parent = nullptr);
signals:
public slots:
void carte_OK();
protected:
QComboBox* carte_type_combo_box;
devices_left_widget* left_widget;
};
#endif // MAINWINDOW_H
device_left_widget.h :
#ifndef DEVICE_LEFT_WIDGET_H
#define DEVICE_LEFT_WIDGET_H
#include <QWidget>
#include <QCheckBox>
#include <QComboBox>
#define STRING_DEVICE1 "DEVICE1"
#define STRING_DEFAULT ""
class device_left_widget : public QWidget {
Q_OBJECT
public:
explicit device_left_widget(QWidget* parent = nullptr);
signals:
public slots:
protected:
QGridLayout* main_grid_layout;
QCheckBox* device_checkbox;
QComboBox* device_type_combo_box;
};
#endif // DEVICES_LEFT_WIDGET_H
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
让我们调用您的小部件的名称
容器
。我们想将Qcombobox的CurrentTextChanged(const qString&amp; text)
信号连接到小部件,因此我们创建了一个与信号相对应的插槽,让它为chosentextChanged(const qString&text&text)<) /代码>。我们将它们连接到
mainWindow
构造函数中:在您的容器类中,将插槽定义为公共:
Let's call your widget's name
container
. We want to connect QComboBox'scurrentTextChanged(const QString &text)
signal to the widget, so we create a slot that corresponds to the signal, let it bechosenTextChanged(const QString& text)
. We connect them insideMainWindow
constructor:And inside your container class, define the slot as public: