Results 1 to 2 of 2

Thread: How to perform action on clicking a QMenu object only?

  1. #1
    Join Date
    Feb 2014
    Posts
    11
    Thanks
    1
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: How to perform action on clicking a QMenu object only?

    here's a snapshot of the GUI. I want to perform simple actions solely by clicking on QMenu object Help. This QMenu object does NOT have any submenus.

    snap3.jpg

    Can you please advise me how to perform actions when only the QMenu is clicked Here's what I have tried, but I got an empty output.

    mainwindow.h
    Qt Code:
    1. #ifndef MAINWINDOW_H
    2. #define MAINWINDOW_H
    3.  
    4. #include <QMainWindow>
    5. #include <QDebug>
    6. #include <QSignalMapper>
    7.  
    8. namespace Ui {
    9. class MainWindow;
    10. }
    11.  
    12. class MainWindow : public QMainWindow
    13. {
    14. Q_OBJECT
    15.  
    16. public:
    17. explicit MainWindow(QWidget *parent = 0);
    18. ~MainWindow();
    19.  
    20. private:
    21. Ui::MainWindow *ui;
    22. void createActions();
    23. QSignalMapper *pSignalMapper;
    24.  
    25. private slots:
    26. void help();
    27.  
    28. };
    29.  
    30. #endif // MAINWINDOW_H
    To copy to clipboard, switch view to plain text mode 

    mainwindow.cpp
    Qt Code:
    1. #include "mainwindow.h"
    2. #include "ui_mainwindow.h"
    3. MainWindow::MainWindow(QWidget *parent) :
    4. QMainWindow(parent),
    5. ui(new Ui::MainWindow)
    6. {
    7. ui->setupUi(this);
    8. createActions();
    9. }
    10.  
    11. MainWindow::~MainWindow()
    12. {
    13. delete ui;
    14. }
    15.  
    16. void MainWindow::createActions()
    17. {
    18. pSignalMapper = new QSignalMapper(this);
    19. connect(ui->menuHelp, SIGNAL(triggered(QAction*)), this, SLOT(help()));
    20.  
    21. }
    22.  
    23. void MainWindow::help()
    24. {
    25. qDebug()<<"inside help qdialog";
    26. }
    To copy to clipboard, switch view to plain text mode 

    main.cpp
    Qt Code:
    1. #include "mainwindow.h"
    2. #include <QApplication>
    3. #include <ui_mainwindow.h>
    4. int main(int argc, char *argv[])
    5. {
    6. QApplication a(argc, argv);
    7. MainWindow w;
    8. w.show();
    9.  
    10. return a.exec();
    11. }
    To copy to clipboard, switch view to plain text mode 

    Output when I click on Help QMenu, absolutely nothing:
    Qt Code:
    1. Starting E:\Qt2\modules\guiPrototype2\build-guiPrototype2-Desktop_Qt_5_2_0_MSVC2010_32bit-Debug\debug\guiPrototype2.exe...
    To copy to clipboard, switch view to plain text mode 


    Added after 9 minutes:


    This works:
    Qt Code:
    1. void MainWindow::createActions()
    2. {
    3. [..]
    4. connect(ui->menuHelp, SIGNAL(aboutToShow()), this, SLOT(help()));
    5. }
    6.  
    7. void MainWindow::help()
    8. {
    9. qDebug()<<"inside help qdialog";
    10. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by Sai Kamat; 5th March 2014 at 13:29.

  2. #2
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,419
    Thanks
    37
    Thanked 1,546 Times in 1,496 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: How to perform action on clicking a QMenu object only?

    If the help menu is not a menu, i.e. does't contain entries, then don't add a menu, add an action.

    Cheers,
    _

Similar Threads

  1. Replies: 11
    Last Post: 8th February 2013, 14:59
  2. Hide shorcut text of QMenu Action
    By KillGabio in forum Qt Programming
    Replies: 5
    Last Post: 19th January 2012, 10:43
  3. Replies: 5
    Last Post: 27th May 2011, 03:51
  4. Replies: 2
    Last Post: 3rd April 2011, 13:48
  5. QMenu still highligheted after action fired in mac
    By berinder in forum Qt Programming
    Replies: 2
    Last Post: 1st September 2008, 20:53

Tags for this Thread

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.