QAction's menu() returns NULL
-
Iam trying to retrieve the name of QAction's menu to which its belongs.
i am dynamically creating QActions and adding to QMenu
@void MainWindow::addSubProfiles(QString aProfileType, QString aDirectoryName, QString aDirectoryPath)
{
QMenu *loMainMenu = new QMenu();
m_titleBar->m_UserProfileMenu->addMenu(loMainMenu);
loMainMenu->addSeparator();loMainMenu->setTitle(QString(aProfileType) + QString(" (") + aDirectoryName + QString(")")); loMainMenu->addSeparator(); QStringList loDirFileNames; QDirIterator loFileNamesIter(aDirectoryPath, QDir::Files | QDir::NoSymLinks | QDir::NoDotAndDotDot, QDirIterator::NoIteratorFlags); while(loFileNamesIter.hasNext()) { loFileNamesIter.next(); loDirFileNames << loFileNamesIter.fileName().remove(".ini"); QString loStrNames = loFileNamesIter.fileName().remove(".ini"); QAction *loProfileActions = new QAction(loStrNames, loMainMenu); loProfileActions->setFont(EyGlobalUtilities::getUtilInstance()->getFont()); loProfileActions->setCheckable(true); theProfilesActGrp->addAction(loProfileActions);//theProfilesActGrp->QActionAgroup loMainMenu->addAction(loProfileActions); connect(loProfileActions, SIGNAL(triggered()), this, SLOT(onProfileClicked())); } m_titleBar->m_UserProfileMenu->addSeparator();
}
void EyGuiMainWindow::onProfileClicked()
{
QAction loSenderAction = qobject_cast<QAction>(sender());qDebug()<<"Currently Active avtion NAme"<<loSenderAction->text(); qDebug()<<"Currently Active action ActionGroup"<<loSenderAction->menu(); if(loSenderAction->menu() == NULL) { qDebug()<<"Menu is NULL"; } setDeleteProfileOption(true);
}@
here Menu is NULL i recieved .Can anybody let me know what iam doing wrong?
-
Hey,
You are thinking in the wrong direction!
a QAction can contain a QMenu (as a child item)
and if set, menu() returns this child menu item... (not the parent)
To iterate in the other direction you should call parent() (QObject method)
@
qDebug()<<"Currently Active action ActionGroup"<<loSenderAction->parent()->metaObject()->className();
@
// outputs >> Currently Active action ActionGroup QMenu