1

我认为我对一个对我来说似乎很基本的概念有一些重大问题。

我创建了一个自定义小部件,它实际上只是一小部分小部件,因此会出现多次。

class CustomWidget : public QWidget {
    Q_OBJECT
public:
    explicit CustomWidget(QWidget parent=nullptr) : QWidget(parent) {
        spinboxA = new QSpinBox;
        spinboxB = new QSpinBox;
        QHBoxLayout* layout = new QHBoxLayout(this);
        layout.addWidget(spinboxA);
        layout.addWidget(spinboxB);
        this->setLayout(layout);
    }
private:
    QSpinBox* spinboxA;
    QSpinBox* spinboxB;
};

然后在 gui 中使用此自定义小部件。当然,我希望这个 gui 对旋转框值的变化做出反应。据我了解,我可以

1)为QSpinBoxes提供getter并在类外连接它们的信号。2)“重新路由”他们的信号,如下例所示

@1) 使用 via connect(customwidget->getSpinboxA(),SIGNAL(valueChanged(int)),this,SLOT(doSomething(int)));,我猜?

@2)

class CustomWidget : public QWidget {
    Q_OBJECT
public:
    explicit CustomWidget(QWidget parent=nullptr) : QWidget(parent) {
        spinboxA = new QSpinBox;
        spinboxB = new QSpinBox;
        QHBoxLayout* layout = new QHBoxLayout;
        layout.addWidget(spinboxA);
        layout.addWidget(spinboxB);
        this->setLayout(layout);
        connect(spinboxA,SIGNAL(valueChanged(int)),//...
            this,SLOT(onSpinboxAValueChanged(int)));
    }
private:
    QSpinBox* spinboxA;
    QSpinBox* spinboxB;
private slots:
    void onSpinboxAValueChanged(int x) {emit spinboxAValueChanged(x);}
    //...
signals:
    void spinboxAValueChanged(int x)
};

在 gui 类中,一个会connect(customwidget,SIGNAL(spinboxAValueChanged(int),this,SLOT(doSomething(int)));

尤其是版本 2) 看起来非常混乱,而且......我在问自己 - 我如何连接到我的自定义小部件内的小部件的信号?

4

1 回答 1

3

CustomWidget 应该是模块化的,也就是说,它应该像一个黑匣子,应该在其中建立输入并获得输出,所以对我来说第二个解决方案非常接近它,但我看到了可以改进的地方:它不是只需要创建一个槽来发出一个信号,信号可以连接到其他信号,我也推荐使用新的连接语法。

#include <QApplication>
#include <QHBoxLayout>
#include <QSpinBox>
#include <QWidget>

#include <QDebug>

class CustomWidget : public QWidget {
    Q_OBJECT
public:
    explicit CustomWidget(QWidget *parent =nullptr):
        QWidget(parent),
        spinboxA(new QSpinBox),
        spinboxB(new QSpinBox)
    {
        QHBoxLayout* layout = new QHBoxLayout(this);
        layout->addWidget(spinboxA);
        layout->addWidget(spinboxB);
        connect(spinboxA, QOverload<int>::of(&QSpinBox::valueChanged), this, &CustomWidget::spinboxAValueChanged);
        connect(spinboxB, QOverload<int>::of(&QSpinBox::valueChanged), this, &CustomWidget::spinboxBValueChanged);
        // old syntax:
        // connect(spinboxA, SIGNAL(valueChanged(int)), this, SIGNAL(spinboxAValueChanged(int)));
        // connect(spinboxB, SIGNAL(valueChanged(int)), this, SIGNAL(spinboxBValueChanged(int)));
    }
private:
    QSpinBox *spinboxA;
    QSpinBox *spinboxB;
signals:
    void spinboxAValueChanged(int x);
    void spinboxBValueChanged(int x);
};

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    CustomWidget w;

    QObject::connect(&w, &CustomWidget::spinboxAValueChanged, [](int i){
       qDebug()<< "spinboxAValueChanged: "<< i;
    });
    QObject::connect(&w, &CustomWidget::spinboxBValueChanged, [](int i){
       qDebug()<< "spinboxBValueChanged: "<< i;
    });

    w.show();

    return a.exec();
}

#include "main.moc"
于 2018-09-09T19:48:04.893 回答