QAction won't connect to my slot

梦想与她 提交于 2019-12-11 17:18:35

问题


I'm trying to make an IHM with Qt, and I started by making a basic menu (File,Edit...). So far, I have my menu containing "File", which then display "New Project, Open Project, Exit". Look great, but my problem is I can't seem to trigger these Action (clicking them or by key shortcut).

Here is my slot:

void KoriganEngine::launchNewProjectWidget(){
   //External QWidget
   m_nwProj = new NewProjectWidget(NULL,Qt::MSWindowsFixedSizeDialogHint);
   m_nwProj->show();
}

If I use this slot with a pushbutton connected, my new QWidget is displayed properly. However, impossible to do the same thing with an action...

Here is the code of my actions and menu:

    void KoriganEngine::KG_createMenus(){
//init actions
KG_createMenuActions();

//add menu to the bar
m_fileMenu = menuBar()->addMenu("File");
m_fileMenu->addAction(m_newProjAction);
m_fileMenu->addAction(m_openProjAction);
m_fileMenu->addSeparator();
m_fileMenu->addAction(m_quitAction);

m_editMenu = menuBar()->addMenu("Edit");

}

    void KoriganEngine::KG_createMenuActions(){
m_newProjAction = new QAction("New Project...", this);
m_newProjAction->setShortcuts(QKeySequence::New);
m_newProjAction->setStatusTip(QString("Create a new Project"));
connect(m_newProjAction, SIGNAL(trigerred()), this, SLOT(slottest()));

m_openProjAction = new QAction("Open Project...", this);
m_openProjAction->setShortcuts(QKeySequence::Open);
m_openProjAction->activate( QAction::Hover);
connect(m_openProjAction, SIGNAL(trigerred()), this, SLOT(launchNewProjectWidget())); //TODO replace the slots

m_quitAction = new QAction("Exit", this);
connect(m_quitAction, SIGNAL(trigerred()), this, SLOT(quit()));

}

And the code that works with a button:

connect(m_button, SIGNAL(clicked()), this, SLOT(launchNewProjectWidget()));

So I don't really get why it should not react the same, I've read the Qt examples over and over... I must have missed something, but if anyone as an idea, I'll be more than grateful, as it's starting to make me hate life :p

Thank you all.

PS: Ok, not sure I handle great the code blocks buisness, in my defence it's really weird to use... :p


回答1:


You made a mistake in triggered word :P It should be:

connect(m_quitAction, SIGNAL(triggered()), this, SLOT(quit()));
                                ------

Triggered, not trigerred! :)




回答2:


if I get this right you problem is m_openProjAction->activate( QAction::Hover); which causes QAction to emit hovered() instead of triggered();



来源:https://stackoverflow.com/questions/12447972/qaction-wont-connect-to-my-slot

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!