views:

496

answers:

2
A: 

Such code should work:

QMainWindow wnd;
QAction *act = wnd.menuBar()->addMenu("SomeMenu")->addMenu("someSubmenu")->addAction("someAction");
QObject::connect(act,SIGNAL(triggered()),
                 someObj,SLOT(actionReaction()));

I think addMenu() addAction() should work in more reliable way. This approach works for me.

VestniK
Thanks,but my problem is, i am able to add the submenu's. i want to change the submenu dynamically.. some time i require to add more submenu some time less.. in that case, when user click on the main menu, which has submenu, it should emit "aboutToShow" signal and gets connect to slot. but in runtime if i click on mainmenu.. the app is crashing
Shadow
So you want to repopulate menu each time it's opened am I understand you correctly? Are you deleting previous actions of the menu in the move() function?
VestniK
NO..i dont want to populate any menu...just add the code added below the "additional information" and try in mainwindow constructor..see the behaviour..am not getting why "abouttoshow" signal is being emitted. could you please check once.. is it bug? or i am missing sme thing
Shadow
I've just tried to create new project. I created class derived from QMainWindow added your code absolutely without changes and created Move slot with the code: qDebug("Moved"); Application works as expected. Nothing is crashes and I see "Moved" text on the console every time menu is shown or hidden. Your problem is not in the code you've posted.
VestniK
which platform are you trying? Symbian,or desktop?
Shadow
are you using symbina, are you able to move to the "Move" slot?i mean, are you going to "Move" slot.. try in debig mode, put a message box in "Move" slot
Shadow
I've tried to run the code on my Linux desktop (Kubuntu 10.04 Qt 4.6.2). As I said I have created Move() slot in my QMainWindow subclass which just prints "Moves" line into the terminal. Code was working exactly as expected.
VestniK
A: 

I'm not sure to understand exactly what you're willing to do into your Move() slot.

But here is your own code (I removed what seemed useless to me), modified so that it is not crashing on my computer :

mainwindow.h

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>

#include <QAction>
#include <QMenu>

class MainWindow : public QMainWindow
{
   Q_OBJECT

public:
   explicit MainWindow(QWidget *parent = 0);

private:
   QMenu* menu;
   QAction *dummyaction;
   QMenu* m_pSubMenu;
 private slots:
    void Move();
};

#endif // MAINWINDOW_H

mainwindow.cpp :

#include "mainwindow.h"

#include <QMenuBar>

MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent)
{
   m_pSubMenu = NULL;
   QMenuBar* pMenuBar = new QMenuBar(this);

   setMenuBar(pMenuBar);

   dummyaction = new QAction("Testing",this);
   menu = new QMenu("Test", this);
   menu->addAction(dummyaction);
   this->menuBar()->addMenu(menu);

   connect(menu, SIGNAL(aboutToShow()), this, SLOT(Move()));
}

void MainWindow::Move() {
   if (!m_pSubMenu) {
      m_pSubMenu = new QMenu(menu);
      dummyaction->setMenu(m_pSubMenu);
   }
   QAction* pAction = new QAction("Test", this);
   m_pSubMenu->addAction(pAction);
}

I don't know exactly what you want to do into your Move() slot, but as an example, each time the Move() slot is called, a new submenu item is added.

Hope this helps.

Jérôme
Yes, i have tried it in my nokia emulator.. it works well with out crash, but the submenu "Test" is not getting added when i click on Test main menu..i mean to say,aboutToShow() signal itself not emitting.. i think its bug in Qt symbian.. which version of Qt are you using? and what environment are you using? Desktop or mobile?
Shadow
I think you should post a bug to Qt issue tracker http://bugreports.qt.nokia.com if you think that it's a bug in Symbian implementation of Qt. I already had such experience and can say that Nokia support engineers are quite friendly.
VestniK
Thanks,ya.. you are right, they are..i have posted a bug too, i will update you on this once i get any update from them
Shadow
I'm using Qt Creator 1.3.1, based on Qt 4.6.1, on Windows XP.
Jérôme