使用2类QT之间的信号

发布于 2025-01-27 19:50:20 字数 1351 浏览 1 评论 0原文

我有一个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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

冷弦 2025-02-03 19:50:20

让我们调用您的小部件的名称容器。我们想将Qcombobox的CurrentTextChanged(const qString&amp; text)信号连接到小部件,因此我们创建了一个与信号相对应的插槽,让它为chosentextChanged(const qString&text&text)<) /代码>。我们将它们连接到mainWindow构造函数中:

    connect(ui->comboBox, SIGNAL(currentTextChanged(const QString &)),
            ui->container, SLOT(chosenTextChanged(const QString &)));

在您的容器类中,将插槽定义为公共:

public slots:
    void chosenTextChanged(const QString &text) {
        //change your QCheckBox's state and
        //change your QComboBox's text
}

Let's call your widget's name container. We want to connect QComboBox's currentTextChanged(const QString &text) signal to the widget, so we create a slot that corresponds to the signal, let it be chosenTextChanged(const QString& text). We connect them inside MainWindow constructor:

    connect(ui->comboBox, SIGNAL(currentTextChanged(const QString &)),
            ui->container, SLOT(chosenTextChanged(const QString &)));

And inside your container class, define the slot as public:

public slots:
    void chosenTextChanged(const QString &text) {
        //change your QCheckBox's state and
        //change your QComboBox's text
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文