代码之家  ›  专栏  ›  技术社区  ›  Michael1248

信号指向结构中的插槽

  •  1
  • Michael1248  · 技术社区  · 6 年前

    我想要一个信号连接到结构内部的插槽。我的结构看起来像:

    //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.
    

    现在连接 插槽位于特定结构中的事件不起作用。

    connect(cmd, SIGNAL(clicked()), mListOfStructs[3], SLOT(someSlot()));
    

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

    1 回复  |  直到 6 年前
        1
  •  0
  •   ΦXocę 웃 Пepeúpa ツ    6 年前

    您的结构需要qu对象元属性,以便发出信号和接收槽事件。。。

    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()));