简体   繁体   中英

How to pass a QAction to a Qt slot from a QMenu

Iam new in Qt and I have problem how to pass QAction as parameter like this code:

connect(fileToolBarAct, SIGNAL(toggled(bool)), this, SLOT(ToggleBar(fileToolBarAct));

And this my slots function:

void MainWindow::ToggleBar(QAction& what)
{
    what.isCheckable();
}

QObject::connect doesn't work like this. You can not pass objects to SIGNAL and SLOT macros. SIGNAL and SLOT macros should take function signatures. In addition the signature of a signal must match the signature of the receiving slot as described in the Qt documentation.

I see that you lack in understanding the signals and slots mechanism and I recommend you read the Qt Signals and Slots documentation for more info. Reading the Qt Signals and Slots documentation will clear everything for you.

onnect(fileToolBarAct, SIGNAL(toggled(bool)), this, SLOT(ToggleBar(bool));


void MainWindow::ToggleBar(bool checked)
{
    QAction* action = qobject_cast<QOAction*>(sender());
    if (action) 
         action->setChecked(checked);
}

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