Trolltech Home | Qt-interest Home | Recent Threads | All Threads | Author | Date
All threads index page 5

Qt-interest Archive, December 2006
QMenuBar bug, or change of behavior from Qt3?


Message 1 in thread

I found something which may be a bug in Qt4 (tested in QT 4.2.1
Linux/4.2.2 Windows) or at least a change of behavior.
I used following code to create a menubar, assign it to main window
and insert item in it.

Basically, if I press the item in the menu with mouse, the slot is
called. If I press the shortcut (CTRL+N) the "TrWindow::menuActivated"
slot is not called

I remember similar code in qt3.3 (from which this one was ported and
stripped down to this small example) that was connected to
"activated(int id)" signal from QMenubar worked fine, it was called
when the menu item was selected by mouse AND when its shortcut key was
pressed.

Seems either shotcut keys in menu are ignored or something from qt3 to
qt4 was changed and I made this wrong, but I can't see any problem
here ...

Martin Petricek

The code:

--pok.cc--
#include "pok.h"

TrWindow::TrWindow(QWidget *parent):QMainWindow(parent,Qt::Window) {
 QWidget::setAttribute(Qt::WA_DeleteOnClose);
 QMenuBar *qb=new QMenuBar(this);
 QMenu *file=qb->addMenu("&File");
 QAction *n=new QAction("&New",this);
 n->setShortcut(QKeySequence("Ctrl+N"));
 file->addAction(n);
 setMenuBar(qb);
 connect(qb, SIGNAL(triggered(QAction *)), SLOT(menuActivated(QAction *)));
}

void TrWindow::menuActivated(QAction *id) {
 QMessageBox::critical(0,"Action","Some action invoked: "+id->text());
}

int main(int argc, char *argv[]){
 QApplication app(argc,argv);
 TrWindow *main=new TrWindow();
 main->show();
 QObject::connect(&app, SIGNAL(lastWindowClosed()), &app, SLOT(quit()));
 return app.exec();
}

--pok.h--
#include <iostream>
#include <QApplication>
#include <QAction>
#include <QWidget>
#include <QObject>
#include <QFont>
#include <QMenuBar>
#include <QMainWindow>
#include <QMessageBox>
#include <QString>

class TrWindow : public QMainWindow {
 Q_OBJECT
public:
 TrWindow(QWidget *parent=0);
protected slots:
 void menuActivated(QAction *id);
};

--pok.pro--
TEMPLATE = app
TARGET = pok
SOURCES += pok.cc
HEADERS += pok.h

--
 [ signature omitted ]