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:
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <QTableWidget>
Q_OBJECT
public:
MainWindow();
private slots:
void open();
private:
void createActions();
void createMenus();
void createToolBars();
};
Q_OBJECT
public:
protected:
};
#endif
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <QTableWidget>
class QAction;
class QMenu;
class MainWindow : public QMainWindow {
Q_OBJECT
public:
MainWindow();
private slots:
void open();
private:
void createActions();
void createMenus();
void createToolBars();
QMenu *fileMenu;
QMenu *helpMenu;
QToolBar *fileToolBar;
QToolBar *helpToolBar;
QAction *openAct;
QAction *aboutQtAct;
};
class TableWidget : public QTableWidget {
Q_OBJECT
public:
TableWidget(QWidget *parent = 0);
protected:
void mouseReleaseEvent(QMouseEvent *e);
};
#endif
To copy to clipboard, switch view to plain text mode
#include <QtGui>
#include "mainwindow.h"
MainWindow::MainWindow() {
TableWidget *table = new TableWidget;
setCentralWidget(table);
createActions();
createMenus();
createToolBars();
}
void MainWindow::open() {
QMessageBox::information(this,
"title",
"MainWindow::open()");
}
void MainWindow::createActions() {
openAct
= new QAction(QIcon(":/images/open.png"), tr
("&Open..."),
this);
openAct->setStatusTip(tr("Open an existing file"));
connect(openAct, SIGNAL(triggered()), this, SLOT(open()));
aboutQtAct
= new QAction(tr
("About &Qt"),
this);
aboutQtAct->setStatusTip(tr("Show the Qt library's About box"));
connect(aboutQtAct, SIGNAL(triggered()), qApp, SLOT(aboutQt()));
}
void MainWindow::createMenus() {
fileMenu = menuBar()->addMenu(tr("&File"));
fileMenu->addAction(openAct);
menuBar()->addSeparator();
helpMenu = menuBar()->addMenu(tr("&Help"));
helpMenu->addAction(aboutQtAct);
}
void MainWindow::createToolBars() {
fileToolBar = addToolBar(tr("File"));
fileToolBar->addAction(openAct);
helpToolBar = addToolBar(tr("Help"));
helpToolBar->addAction(aboutQtAct);
}
setRowCount(10);
setColumnCount(10);
}
if (e->button() == Qt::RightButton) {
QAction * action_foo
= menu.
addAction( "foo" );
QAction * action_dlg
= menu.
addAction( "open dialog" );
QAction * action_bar
= menu.
addAction( "bar" );
if (action == action_dlg) {
}
}
}
#include <QtGui>
#include "mainwindow.h"
MainWindow::MainWindow() {
TableWidget *table = new TableWidget;
setCentralWidget(table);
createActions();
createMenus();
createToolBars();
}
void MainWindow::open() {
QMessageBox::information(this, "title", "MainWindow::open()");
}
void MainWindow::createActions() {
openAct = new QAction(QIcon(":/images/open.png"), tr("&Open..."), this);
openAct->setShortcuts(QKeySequence::Open);
openAct->setStatusTip(tr("Open an existing file"));
connect(openAct, SIGNAL(triggered()), this, SLOT(open()));
aboutQtAct = new QAction(tr("About &Qt"), this);
aboutQtAct->setStatusTip(tr("Show the Qt library's About box"));
connect(aboutQtAct, SIGNAL(triggered()), qApp, SLOT(aboutQt()));
}
void MainWindow::createMenus() {
fileMenu = menuBar()->addMenu(tr("&File"));
fileMenu->addAction(openAct);
menuBar()->addSeparator();
helpMenu = menuBar()->addMenu(tr("&Help"));
helpMenu->addAction(aboutQtAct);
}
void MainWindow::createToolBars() {
fileToolBar = addToolBar(tr("File"));
fileToolBar->addAction(openAct);
helpToolBar = addToolBar(tr("Help"));
helpToolBar->addAction(aboutQtAct);
}
TableWidget::TableWidget(QWidget *parent) : QTableWidget(parent) {
setRowCount(10);
setColumnCount(10);
}
void TableWidget::mouseReleaseEvent(QMouseEvent *e) {
if (e->button() == Qt::RightButton) {
QMenu menu;
QAction * action_foo = menu.addAction( "foo" );
QAction * action_dlg = menu.addAction( "open dialog" );
QAction * action_bar = menu.addAction( "bar" );
QAction * action = menu.exec( QCursor::pos() );
if (action == action_dlg) {
QColorDialog::getRgba();
}
}
}
To copy to clipboard, switch view to plain text mode
#include <QApplication>
#include "mainwindow.h"
int main(int argc, char *argv[]) {
MainWindow mainWin;
mainWin.show();
return app.exec();
}
#include <QApplication>
#include "mainwindow.h"
int main(int argc, char *argv[]) {
QApplication app(argc, argv);
MainWindow mainWin;
mainWin.show();
return app.exec();
}
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?
Bookmarks