Results 1 to 7 of 7

Thread: QToolbar - change icon at runtime ?

  1. #1
    Join Date
    Oct 2010
    Posts
    95
    Thanks
    2
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Question QToolbar - change icon at runtime ?

    Hi,

    In my main window, I have a toolbar with an action. This action/button has an icon (a png image) that is visible in the QT Creator form designer. But when I run the application, I see no image !!! Can you tell me why ?

    Also, at run time, with code, I would like to change the image of the toolbar button. Is it possible ? And how to do this ?

    Thanks for your help

  2. #2
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: QToolbar - change icon at runtime ?

    But when I run the application, I see no image !!! Can you tell me why ?
    Has probably to do with path resolution of the image.
    But without code its hard to tell exactly.

    I would like to change the image of the toolbar button. Is it possible ? And how to do this ?
    QAction::setIcon() ?
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  3. #3
    Join Date
    Oct 2010
    Posts
    95
    Thanks
    2
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QToolbar - change icon at runtime ?

    Thanks,

    I have put my images in ressources and all is fine, it is displayed now.

    In order to change the icon I have do this :
    Qt Code:
    1. actions().first()->setIcon(QIcon(":/images/pause.png"));
    To copy to clipboard, switch view to plain text mode 
    But it crash :-P

    The problem is that actions(); return an empty list !

  4. #4
    Join Date
    Oct 2010
    Location
    Berlin, Germany
    Posts
    358
    Thanks
    18
    Thanked 68 Times in 66 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QToolbar - change icon at runtime ?

    use the name of the action instead of ations()->first()

  5. #5
    Join Date
    Oct 2010
    Posts
    95
    Thanks
    2
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QToolbar - change icon at runtime ?

    Thanks,

    How can I use the name of the action, there is no findAction("") method ?

    Also actions() is empty in my main window ! do you know why ?

  6. #6
    Join Date
    Oct 2010
    Location
    Berlin, Germany
    Posts
    358
    Thanks
    18
    Thanked 68 Times in 66 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QToolbar - change icon at runtime ?

    somewhere in your code must be something like this:

    Qt Code:
    1. QAction myAction;
    To copy to clipboard, switch view to plain text mode 

    if you create your form via QtDesigner, you have to create all actions via the action editor. there you give each action a name. You can access your actions the same way as each other gui-element.

  7. #7
    Join Date
    Feb 2010
    Location
    Poland
    Posts
    27
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: QToolbar - change icon at runtime ?

    Qt Code:
    1. ui->toolBar->actions().first()->setIcon(QIcon(":/images/pause.png"));
    To copy to clipboard, switch view to plain text mode 
    ?

Similar Threads

  1. override shortcut icon in QToolBar
    By sudhansu in forum Qt Programming
    Replies: 1
    Last Post: 18th February 2010, 08:20
  2. Flat Icon in a QToolBar
    By schall_l in forum Qt Programming
    Replies: 8
    Last Post: 23rd June 2009, 09:31
  3. how change language at runtime?
    By mattia in forum Newbie
    Replies: 3
    Last Post: 14th November 2007, 18:36
  4. Various icon sizes for QToolButtons on QToolbar
    By Erlendhg in forum Qt Programming
    Replies: 4
    Last Post: 30th June 2007, 18:35
  5. QToolBar icon size
    By ^NyAw^ in forum Qt Programming
    Replies: 3
    Last Post: 29th March 2007, 13:12

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
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.