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

Qt-interest Archive, July 2007
Shortcuts of hidden menu bar don't work


Message 1 in thread

Hi list,

I have a Qt4 application with a menu bar containing some actions with
shortcuts (e.g. Ctrl+M). Now, when I hide the menu bar, the shortcuts
don't work anymore. The reason might be that invisible widgets don't
process events and so the shortcut events don't reach the actions.

My question is, how do I make the shortcuts work when the menu bar is
hidden? A quick solution would be to add each action not only to the
menu but also to the main window - but with many actions that's ugly.

Interestingly, in Qt3 it works. Could this even be a bug in Qt4, or is
it deliberate? Here's a minimal example for Qt4 to show the behaviour:


#include <QtGui>

int main(int argc, char ** argv)
{
    QApplication app(argc, argv);

    QMainWindow * window = new QMainWindow();
    QAction * action = new QAction(window);
    action->setCheckable(true);
    action->setText("Show menu bar");
    action->setChecked(true);
    action->setShortcut(Qt::CTRL + Qt::Key_M);

    window->connect(action, SIGNAL(toggled(bool)),
                    window->menuBar(), SLOT(setVisible(bool)));

    QMenu * menu = window->menuBar()->addMenu("&View");
    menu->addAction(action);

    window->show();

    return app.exec();
}


Do you have any suggestions? Maybe I have to overwrite an event-Method,
but I don't know where and which, as I had a look at the sources but
couldn't find out which path the QEvent::Shortcut takes.

Thanks,
  Robin Stocker

--
 [ signature omitted ] 

Message 2 in thread

On tirsdag den 24. Juli 2007, Robin Stocker wrote:
> Hi list,
>
> I have a Qt4 application with a menu bar containing some actions with
> shortcuts (e.g. Ctrl+M). Now, when I hide the menu bar, the shortcuts
> don't work anymore. The reason might be that invisible widgets don't
> process events and so the shortcut events don't reach the actions.

This is indeed a quite evil problem to figure out. The problem is that all 
actions check if they are added to the list of some active widget. If that is 
not the case, they assume they are disabled. This is most of the times what 
you actually want, since this allows you to disable a bunch of actions just 
by disabling or hiding a single object.

The workaround to this is surprisingly simple: Just add the action to your 
main window. The action will see that it has both a menu and a main window 
as "owner" and if just one of them is enabled and visible, it will react to 
the shortcut.

Bo.

-- 
 [ signature omitted ] 

Message 3 in thread

Bo Thorsen schrieb:
> On tirsdag den 24. Juli 2007, Robin Stocker wrote:
>> Hi list,
>>
>> I have a Qt4 application with a menu bar containing some actions with
>> shortcuts (e.g. Ctrl+M). Now, when I hide the menu bar, the shortcuts
>> don't work anymore. The reason might be that invisible widgets don't
>> process events and so the shortcut events don't reach the actions.
> 
> This is indeed a quite evil problem to figure out. The problem is that all 
> actions check if they are added to the list of some active widget. If that is 
> not the case, they assume they are disabled. This is most of the times what 
> you actually want, since this allows you to disable a bunch of actions just 
> by disabling or hiding a single object.
> 
> The workaround to this is surprisingly simple: Just add the action to your 
> main window. The action will see that it has both a menu and a main window 
> as "owner" and if just one of them is enabled and visible, it will react to 
> the shortcut.

Thank you for clarifying (and for the quick response). Then I'll do it
like that. Because I work with KDE classes, all the actions are stored
in a collection anyway, so it's not a big deal.

Cheers,
  Robin Stocker

--
 [ signature omitted ]