信号指向结构中的插槽

时间:2018-07-19 15:17:31

标签: qt struct connection signals-slots

我希望将信号连接到结构内部的插槽。我的结构看起来像:

show

然后创建一个按钮,该按钮应将其//Header file struct someStruct { public: int index; public slots: void someSlot(); }; QList<someStruct*> mListOfStructs; 信号转发到clicked()函数。

someSlot

现在无法将//Source file QPushButton *cmd = new QPushButton(); grd->addWidget(cmd, 3, 2, Qt::AlignCenter); //grd is a QGridLayout somewhere inside the gui. I can see it and also the button. 事件与特定结构内的插槽连接。

clicked()

一些资料告诉我,我必须添加metaObject或sth。我尝试过,但没有成功。也许你知道得更多。

我可能会使用How to connect in Qt signal and slot in dynamically added buttons to get in slot index of added button?作为解决方法。

1 个答案:

答案 0 :(得分:0)

您的结构需要Q_Object元属性才能发出信号并接收插槽事件...

struct testStruct : public QObject
{
    Q_OBJECT
    public:
        int index;
        testStruct():index(0){}

    public slots:
        void someSlot()
        {
            qDebug() << "slot called!";
        }
};

之后,您可以照常选择:

testStruct *ts= new testStruct;
connect(this, SIGNAL(someSignal()), ts, SLOT(someSlot()));