简体   繁体   中英

Qt: the signal handler is not called when the signal is emitted

I am doing something like this:

MyClass::MyClass(QWidget *parent) : QLabel(parent)
{
    this->MyMenu = new QMenu();

    QAction* act_del = new QAction(MyMenu);
    act_delete->setText("MyAction");

    MyMenu->addAction(act_del);

    QObject::connect(act_del,SIGNAL(triggered()),this,SLOT(MySlot()));
}

void MyClass::MySlot()
{
    //Not called
}

Any suggestion on when the SIGNAL is triggered the SLOT is not called. Here is where the menu is displayed:

void MyClass::contextMenuEvent(QContextMenuEvent *ev)
{
    QPoint globalPos = this->mapToGlobal(ev->pos());

    QAction* selectedItem = MyMenu->exec(globalPos);
    if (selectedItem)
    {

    }
    else
    {
        // nothing was chosen
    }
}

Any suggestions on why the SLOT is not called?

MyClass需要包含Q_OBJECT宏以使信号槽连接起作用。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM