Results 1 to 4 of 4

Thread: how to add my menu class to my MainWindow?

  1. #1
    Join Date
    Feb 2012
    Location
    Armenia/Yerevan
    Posts
    400
    Thanks
    15
    Thanked 16 Times in 15 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11

    Default how to add my menu class to my MainWindow?

    I created a menu inside my mainwindow:
    Qt Code:
    1. MainWindow::MainWindow(QWidget *parent) :
    2. QMainWindow(parent),
    3. ui(new Ui::MainWindow)
    4. {
    5. ui->setupUi(this);
    6. Menu *settingsMenu = new Menu(this);
    7. // settingsMenu->show();
    To copy to clipboard, switch view to plain text mode 

    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?

    Qt Code:
    1. Menu::Menu(QWidget *parent) :
    2. QMainWindow(parent),
    3. ui(new Ui::MainWindow)
    4. {
    5. ui->setupUi(this);
    6.  
    7. createActions();
    8. createMenus();
    9. }
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: how to add my menu class to my MainWindow?

    There are two things you should realise:
    1. 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().
    2. 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.

  3. #3
    Join Date
    Feb 2012
    Location
    Armenia/Yerevan
    Posts
    400
    Thanks
    15
    Thanked 16 Times in 15 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11

    Default Re: how to add my menu class to my MainWindow?

    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:
    Qt Code:
    1. myMenu = new Menu();
    2. myMenu->setList(ui->menuVideo, ui->menuSettings, ui->menuHelp);
    To copy to clipboard, switch view to plain text mode 

    inside Menu class
    Qt Code:
    1. void Menu::setList(QMenu *video, QMenu *setting, QMenu *help)
    2. {
    3. videoMenu = video;
    4. settingsMenu = setting;
    5. helpMenu = help;
    6. connect(settingsMenu, SIGNAL(triggered(QAction*)), this, SLOT(Func_CAM_IRCorrection_standard(QAction *)));
    7. }
    To copy to clipboard, switch view to plain text mode 
    the above variables are defined in the Menu class:
    Qt Code:
    1. QMenu *videoMenu;
    2. QMenu *helpMenu;
    3. QMenu *settingsMenu;
    4.  
    5. QMenu *menuCAM_IRCorrectionMenu;
    6. QAction *actionCAM_IRCorrection_standard;
    7. QAction *actionCAM_IRCorrection_IRLight;
    To copy to clipboard, switch view to plain text mode 

    the problem I have is here:
    Qt Code:
    1. connect(settingsMenu, SIGNAL(triggered(QAction*)), this, SLOT(Func_CAM_IRCorrection_standard(QAction *)));
    To copy to clipboard, switch view to plain text mode 

    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?
    Last edited by saman_artorious; 14th July 2012 at 13:06.

  4. #4
    Join Date
    Feb 2012
    Location
    Armenia/Yerevan
    Posts
    400
    Thanks
    15
    Thanked 16 Times in 15 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11

    Default Re: how to add my menu class to my MainWindow?

    Quote Originally Posted by ChrisW67 View Post
    There are two things you should realise:
    1. 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().
    2. 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.
    Last edited by saman_artorious; 14th July 2012 at 21:36.

Similar Threads

  1. show object (scene) in widget in class class mainwindow
    By rimie23 in forum Qt Programming
    Replies: 8
    Last Post: 1st May 2012, 16:15
  2. Replies: 4
    Last Post: 23rd August 2011, 21:53
  3. Replies: 3
    Last Post: 12th April 2011, 10:58
  4. Replies: 4
    Last Post: 17th January 2011, 14:05
  5. MainWindow Menu bug in Qt 4.2.3?
    By No-Nonsense in forum Qt Programming
    Replies: 4
    Last Post: 11th March 2007, 11:47

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.