PDA

View Full Version : how to add my menu class to my MainWindow?



saman_artorious
14th July 2012, 06:54
I created a menu inside my mainwindow:


MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
Menu *settingsMenu = new Menu(this);
// settingsMenu->show();


it works fine. But now I decided to create a menu class and add the menu creation code in that class.
the menu class I created also inherits QMainWindow. When I create an instance of it in the MainWindow constructor, it doesn't show anything.
of course if I call the show() method it will create it in a separate window.

How can I merge the menu class to the main window?



Menu::Menu(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);

createActions();
createMenus();
}

ChrisW67
14th July 2012, 08:49
There are two things you should realise:

QMainWindow already has a menu bar that you can simply add to if you want to manually do it. Get a pointer to it using QMainWindow::menuBar().
You can construct your menus in Designer, which you are already using, and not have to code them at all. For more complex menus you can even partially construct the menus in Designer and add to them later in code.

saman_artorious
14th July 2012, 09:39
well yes, I solved the problem using qt Designer. But, now, I have problem accessing the QActions defined in the menus.

because I wanted to have a separate menu class and add it to the main window, in my code, i created a function to which mainwindow uses to send the menu pointers to the Menu class:

before that this is what happens inside mainwindow constructor:


myMenu = new Menu();
myMenu->setList(ui->menuVideo, ui->menuSettings, ui->menuHelp);


inside Menu class


void Menu::setList(QMenu *video, QMenu *setting, QMenu *help)
{
videoMenu = video;
settingsMenu = setting;
helpMenu = help;
connect(settingsMenu, SIGNAL(triggered(QAction*)), this, SLOT(Func_CAM_IRCorrection_standard(QAction *)));
}

the above variables are defined in the Menu class:


QMenu *videoMenu;
QMenu *helpMenu;
QMenu *settingsMenu;

QMenu *menuCAM_IRCorrectionMenu;
QAction *actionCAM_IRCorrection_standard;
QAction *actionCAM_IRCorrection_IRLight;


the problem I have is here:

connect(settingsMenu, SIGNAL(triggered(QAction*)), this, SLOT(Func_CAM_IRCorrection_standard(QAction *)));

when I call the two QActions of the CAM_IRCorrectionMenu to their own individual slots, all QActions trigger that slot!
what I want is every individual QAction should trigger its own specified slot, but here all QActions trigger that slot, or simply whenever any of the settingsmenu item is clicked!

I know the above connect is not correct, I wrote it because there is no way to call the QAction of a specific QMenu

any idea how I can solve it?

saman_artorious
14th July 2012, 21:27
There are two things you should realise:

QMainWindow already has a menu bar that you can simply add to if you want to manually do it. Get a pointer to it using QMainWindow::menuBar().
You can construct your menus in Designer, which you are already using, and not have to code them at all. For more complex menus you can even partially construct the menus in Designer and add to them later in code.


Thanks Chris,
I solved the problem using the using the first method, that is, I passed a pointer from mainwindow::menubar to the menu class. :)
But, for the second method I have a problem implementing it. Could you please have a look at the previous post n see how we can solve it in Designer please.