Hi!

Is it correct to call a modal dialog from within a mouseReleaseEvent handler?

This is what I want to do in my application, but I have encountered a problem: when the dialog is closed then a QMainWindow's popup menu appears (in the point we have clicked at).

I have written a primitive application to demonstrate the problem:
Qt Code:
  1. #ifndef MAINWINDOW_H
  2. #define MAINWINDOW_H
  3.  
  4. #include <QMainWindow>
  5. #include <QTableWidget>
  6.  
  7. class QAction;
  8. class QMenu;
  9.  
  10. class MainWindow : public QMainWindow {
  11. Q_OBJECT
  12. public:
  13. MainWindow();
  14.  
  15. private slots:
  16. void open();
  17.  
  18. private:
  19. void createActions();
  20. void createMenus();
  21. void createToolBars();
  22.  
  23. QMenu *fileMenu;
  24. QMenu *helpMenu;
  25. QToolBar *fileToolBar;
  26. QToolBar *helpToolBar;
  27. QAction *openAct;
  28. QAction *aboutQtAct;
  29. };
  30.  
  31. class TableWidget : public QTableWidget {
  32. Q_OBJECT
  33. public:
  34. TableWidget(QWidget *parent = 0);
  35.  
  36. protected:
  37. void mouseReleaseEvent(QMouseEvent *e);
  38. };
  39.  
  40. #endif
To copy to clipboard, switch view to plain text mode 
Qt Code:
  1. #include <QtGui>
  2. #include "mainwindow.h"
  3.  
  4. MainWindow::MainWindow() {
  5. TableWidget *table = new TableWidget;
  6. setCentralWidget(table);
  7.  
  8. createActions();
  9. createMenus();
  10. createToolBars();
  11. }
  12.  
  13. void MainWindow::open() {
  14. QMessageBox::information(this, "title", "MainWindow::open()");
  15. }
  16.  
  17. void MainWindow::createActions() {
  18. openAct = new QAction(QIcon(":/images/open.png"), tr("&Open..."), this);
  19. openAct->setShortcuts(QKeySequence::Open);
  20. openAct->setStatusTip(tr("Open an existing file"));
  21. connect(openAct, SIGNAL(triggered()), this, SLOT(open()));
  22.  
  23. aboutQtAct = new QAction(tr("About &Qt"), this);
  24. aboutQtAct->setStatusTip(tr("Show the Qt library's About box"));
  25. connect(aboutQtAct, SIGNAL(triggered()), qApp, SLOT(aboutQt()));
  26. }
  27.  
  28. void MainWindow::createMenus() {
  29. fileMenu = menuBar()->addMenu(tr("&File"));
  30. fileMenu->addAction(openAct);
  31. menuBar()->addSeparator();
  32. helpMenu = menuBar()->addMenu(tr("&Help"));
  33. helpMenu->addAction(aboutQtAct);
  34. }
  35.  
  36. void MainWindow::createToolBars() {
  37. fileToolBar = addToolBar(tr("File"));
  38. fileToolBar->addAction(openAct);
  39.  
  40. helpToolBar = addToolBar(tr("Help"));
  41. helpToolBar->addAction(aboutQtAct);
  42. }
  43.  
  44. TableWidget::TableWidget(QWidget *parent) : QTableWidget(parent) {
  45. setRowCount(10);
  46. setColumnCount(10);
  47. }
  48.  
  49. void TableWidget::mouseReleaseEvent(QMouseEvent *e) {
  50. if (e->button() == Qt::RightButton) {
  51. QMenu menu;
  52. QAction * action_foo = menu.addAction( "foo" );
  53. QAction * action_dlg = menu.addAction( "open dialog" );
  54. QAction * action_bar = menu.addAction( "bar" );
  55. QAction * action = menu.exec( QCursor::pos() );
  56. if (action == action_dlg) {
  57. QColorDialog::getRgba();
  58. }
  59. }
  60. }
To copy to clipboard, switch view to plain text mode 
Qt Code:
  1. #include <QApplication>
  2. #include "mainwindow.h"
  3.  
  4. int main(int argc, char *argv[]) {
  5. QApplication app(argc, argv);
  6. MainWindow mainWin;
  7. mainWin.show();
  8. return app.exec();
  9. }
To copy to clipboard, switch view to plain text mode 

Steps to reproduce the problem:
1. Right-click on the table and choose 'open dialog' option
2. Click 'OK' or 'Cancel' on the dialog (dialog closes)
Now QMainWindow's popup menu appears within the table. This popup menu contains checkable entries for tool bars. But this popup menu should only appear when a user clicks on the tool bars or dock widgets.

I suppose it is a bug in Qt? Isn't it?