Results 1 to 7 of 7

Thread: Invisible but still active QActions

  1. #1
    Join Date
    Mar 2006
    Location
    San Francisco, CA
    Posts
    23
    Thanks
    9
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Question Invisible but still active QActions

    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.

    Qt Code:
    1. // add actions to invisible tool bar
    2. // toolbar is invisible because parent is 0
    3. QToolBar * qtb = new QToolBar( 0, "qtb");
    4. viewPan_LeftAction->addTo( qtb );
    5. viewPan_Left10Action->addTo( qtb );
    6. viewPan_Left100Action->addTo( qtb );
    7. ...
    To copy to clipboard, switch view to plain text mode 

    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.
    Last edited by barnabyr; 20th November 2006 at 21:51. Reason: updated contents

  2. #2
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: Invisible but still active QActions

    So how do you trigger the actions if they are visible nowhere? By using shortcuts?
    J-P Nurmi

  3. The following user says thank you to jpn for this useful post:

    barnabyr (21st November 2006)

  4. #3
    Join Date
    Mar 2006
    Location
    San Francisco, CA
    Posts
    23
    Thanks
    9
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Invisible but still active QActions

    I used accelerators.

    Qt Code:
    1. viewPan_LeftAction->setAccel( tr( "Left" ) );
    2. viewPan_Left10Action->setAccel( tr("Ctrl+Left") );
    3. viewPan_Left100Action->setAccel( tr("Shift+Left") );
    4. ...
    To copy to clipboard, switch view to plain text mode 
    Last edited by barnabyr; 20th November 2006 at 22:45.

  5. #4
    Join Date
    Jan 2006
    Location
    Kerala
    Posts
    371
    Thanks
    76
    Thanked 37 Times in 32 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Invisible but still active QActions

    As jpn has mentioned. Using QShortcut is better for this situation
    We can't solve problems by using the same kind of thinking we used when we created them

  6. The following user says thank you to sunil.thaha for this useful post:

    barnabyr (21st November 2006)

  7. #5
    Join Date
    Mar 2006
    Location
    San Francisco, CA
    Posts
    23
    Thanks
    9
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Question Re: Invisible but still active QActions

    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)

    Qt Code:
    1. viewPan_LeftAction = new QAction( this, "viewPan_LeftAction" );
    2. viewPan_Left10Action = new QAction( this, "viewPan_Left10Action" );
    3. viewPan_Left100Action = new QAction( this, "viewPan_Left100Action" );
    4.  
    5. viewPanXMapper = new QSignalMapper( this );
    6. connect( viewPanXMapper, SIGNAL( mapped( int ) ), iview, SLOT( viewPanX(int) ) );
    7.  
    8. connect( viewPan_LeftAction, SIGNAL( triggered() ), viewPanXMapper, SLOT(map()) );
    9. connect( viewPan_Left10Action, SIGNAL( triggered() ), viewPanXMapper, SLOT(map()) );
    10. connect( viewPan_Left100Action, SIGNAL( triggered() ), viewPanXMapper, SLOT(map()) );
    11.  
    12. viewPanXMapper->setMapping( viewPan_LeftAction, -1 );
    13. viewPanXMapper->setMapping( viewPan_Left10Action, -10 );
    14. viewPanXMapper->setMapping( viewPan_Left100Action, -100 );
    To copy to clipboard, switch view to plain text mode 

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

    Qt Code:
    1. QShortcut *sLeft = new QShortcut(QKeySequence(tr("Left")), this);
    2. connect(sLeft,SIGNAL(activated()),viewPanXMapper, SLOT(map()));
    3. viewPanXMapper->setMapping( sLeft, -1 );
    To copy to clipboard, switch view to plain text mode 

    I hope not because it crashes when I try

    Thanks for any pointers.

    barnaby.

  8. #6
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: Invisible but still active QActions

    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..
    J-P Nurmi

  9. #7
    Join Date
    Mar 2006
    Location
    San Francisco, CA
    Posts
    23
    Thanks
    9
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Invisible but still active QActions

    Quote Originally Posted by jpn View Post
    Did you remember to allocate a new signal mapper object as well so that viewPanXMapper is not a null/dangling pointer?
    No I didn't remember !!!

    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.

    Qt Code:
    1. QShortcut *sLeft = new QShortcut(QKeySequence(tr("Left")), this);
    2. connect(sLeft,SIGNAL(activated()),viewPan_LeftAction, SLOT(trigger()));
    To copy to clipboard, switch view to plain text mode 
    Last edited by barnabyr; 21st November 2006 at 22:48.

Similar Threads

  1. Qt application with live Active X camera feed.
    By bitChanger in forum Qt Programming
    Replies: 8
    Last Post: 4th September 2012, 19:26
  2. How to manage QPainter?
    By Caius Aérobus in forum Qt Programming
    Replies: 3
    Last Post: 28th April 2006, 13:20
  3. Painter not active!
    By Caius Aérobus in forum Qt Programming
    Replies: 7
    Last Post: 30th March 2006, 15:44

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Qt is a trademark of The Qt Company.