PDA

View Full Version : Invisible but still active QActions



barnabyr
20th November 2006, 21:49
Hello

I am porting some code from Qt3 to Qt4.

In the Qt3 code I created a QToolBar inside the
constructor of a QMainWindow superclass.



// add actions to invisible tool bar
// toolbar is invisible because parent is 0
QToolBar * qtb = new QToolBar( 0, "qtb");
viewPan_LeftAction->addTo( qtb );
viewPan_Left10Action->addTo( qtb );
viewPan_Left100Action->addTo( qtb );
...


Then I added QActions to a QToolBar using the addTo method.

As it says in the comments the toolbar is invisible due to
a trick that hid it by setting its parent to '0'. BUT the actions
that were all added to it still work !

The reason I wanted that was because I have lots of actions that all do more
or less the same thing (panning an image in varying amounts) that I didn't want
to clutter up my menus with.

How do I get the same functionality in Qt4 ?

I am guessing that I shouldn't use a QToolBar at all but use
QToolButtons ? But how do I add a QToolButton and have it hidden
but still functional ?

Thanks for any insight.

barnaby.

jpn
20th November 2006, 22:27
So how do you trigger the actions if they are visible nowhere? By using shortcuts (http://doc.trolltech.com/4.2/qshortcut)?

barnabyr
20th November 2006, 22:38
I used accelerators.


viewPan_LeftAction->setAccel( tr( "Left" ) );
viewPan_Left10Action->setAccel( tr("Ctrl+Left") );
viewPan_Left100Action->setAccel( tr("Shift+Left") );
...

sunil.thaha
21st November 2006, 05:48
As jpn has mentioned. Using QShortcut is better for this situation

barnabyr
21st November 2006, 19:13
I am having trouble using the QShortcut. I'm sorry to have to ask but could you give me an idiot's guide to how to use a QShortcut in my situation ? Previously I was routing QAction::triggered() signals in to a mapper.

Here is a snippet of the code where I set it up. (Let's just stick with "Left" actions only, as above)


viewPan_LeftAction = new QAction( this, "viewPan_LeftAction" );
viewPan_Left10Action = new QAction( this, "viewPan_Left10Action" );
viewPan_Left100Action = new QAction( this, "viewPan_Left100Action" );

viewPanXMapper = new QSignalMapper( this );
connect( viewPanXMapper, SIGNAL( mapped( int ) ), iview, SLOT( viewPanX(int) ) );

connect( viewPan_LeftAction, SIGNAL( triggered() ), viewPanXMapper, SLOT(map()) );
connect( viewPan_Left10Action, SIGNAL( triggered() ), viewPanXMapper, SLOT(map()) );
connect( viewPan_Left100Action, SIGNAL( triggered() ), viewPanXMapper, SLOT(map()) );

viewPanXMapper->setMapping( viewPan_LeftAction, -1 );
viewPanXMapper->setMapping( viewPan_Left10Action, -10 );
viewPanXMapper->setMapping( viewPan_Left100Action, -100 );

Do I want to create shortcuts and connect activated to the mapper like this ?


QShortcut *sLeft = new QShortcut(QKeySequence(tr("Left")), this);
connect(sLeft,SIGNAL(activated()),viewPanXMapper, SLOT(map()));
viewPanXMapper->setMapping( sLeft, -1 );


I hope not because it crashes when I try :(

Thanks for any pointers.

barnaby.

jpn
21st November 2006, 20:07
I don't see anything wrong with the code snippet. Did you remember to allocate a new signal mapper object as well so that viewPanXMapper is not a null/dangling pointer? :)
I suggest you to run it through debugger to see where does it actually crash..

barnabyr
21st November 2006, 20:22
Did you remember to allocate a new signal mapper object as well so that viewPanXMapper is not a null/dangling pointer?

:o No I didn't remember :o :o :o !!!

Thank you .. it works now !

p.s for maximum compatibility with the older code I changed things so that the QShortcut::activated() is connected to the QAction::trigger() and left the mapper set
up as before.


QShortcut *sLeft = new QShortcut(QKeySequence(tr("Left")), this);
connect(sLeft,SIGNAL(activated()),viewPan_LeftActi on, SLOT(trigger()));