Results 1 to 17 of 17

Thread: Why are actions limited to menus and toolbars?

  1. #1
    Join Date
    Feb 2006
    Posts
    32
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Why are actions limited to menus and toolbars?

    I have a menu which contains a list of checkable items. I want to create a dockwindow which also contains the same list of checkable items, so when I tick an item in the menu it it automatically ticked in the dock window.

    Keeping seperate GUI elements in sync like this is exactly what QAction is designed for so I was suprised to see it only works for menus and toolbars! Why is that? Do I have to provide the syncrhonisation manually?

  2. #2
    Join Date
    Jan 2006
    Location
    Ukraine,Lviv
    Posts
    454
    Thanks
    9
    Thanked 27 Times in 27 Posts
    Qt products
    Qt3
    Platforms
    Unix/X11 Windows

    Default Re: Why are actions limited to menus and toolbars?

    QToolBar inherits QDockWindow, so i think you can use it instead QDockWindow
    a life without programming is like an empty bottle

  3. #3
    Join Date
    Feb 2006
    Posts
    32
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Why are actions limited to menus and toolbars?

    Quote Originally Posted by zlatko
    QToolBar inherits QDockWindow, so i think you can use it instead QDockWindow
    Do you mean I can use a QToolbar in a QDockWindow? I don't want to do that.

    Or Do you mean I can use QActions in a QDockWindow the same way as I can use them in QToolbar?

  4. #4
    Join Date
    Jan 2006
    Location
    Ukraine,Lviv
    Posts
    454
    Thanks
    9
    Thanked 27 Times in 27 Posts
    Qt products
    Qt3
    Platforms
    Unix/X11 Windows

    Default Re: Why are actions limited to menus and toolbars?

    I mean that you must use QToolBar instead QDockWindow
    a life without programming is like an empty bottle

  5. #5
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Why are actions limited to menus and toolbars?

    Use QToolButton objects.

  6. #6
    Join Date
    Feb 2006
    Posts
    32
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Why are actions limited to menus and toolbars?

    Quote Originally Posted by wysota
    Use QToolButton objects.
    I have tried that but I specifically want to use checkboxes. My menu consists of checkable items and I want my dock window to contain a list of checkable items also.

    I notice that QWidget has an addAction() method. I thought that I could create a QCheckbox then call addAction() on it to syncronise it with the action, but that doesn't work. What's the point of QWidget::addAction()?

  7. #7
    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: Why are actions limited to menus and toolbars?

    Quote Originally Posted by Paul Drummond
    What's the point of QWidget::addAction()?
    Well, docs say about this:
    All QWidgets have a list of QActions, however they can be represented graphically in many different ways. The default use of the QAction list (as returned by actions()) is to create a context QMenu.
    As I understand, it is just for example for simplifying constructing of context menus and so on. At least it is an easy way to pass a set of actions to a widget for later usage without needing to dangle them around as member variables or so..

    Basically you could try adding your checkboxes where ever you wish, and then connecting check boxes' toggled(bool) signals to according QAction::toggle(bool) slots. This could work as long as the check boxes are't tri-state (which wouldn't make sense either).
    Last edited by jpn; 2nd March 2006 at 11:16.

  8. #8
    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: Why are actions limited to menus and toolbars?

    Someone's a bit dulled..

    Here's a tiny example of syncing QActions with QCheckboxes. All behaviour done with Designer.. All I did was connecting toggle(d) signals 'n slots between checkboxes and actions to both directions and voilá!
    Attached Files Attached Files

  9. #9
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Why are actions limited to menus and toolbars?

    Quote Originally Posted by Paul Drummond
    I have tried that but I specifically want to use checkboxes.
    A checkbox is equal to a button with checkable property set to true.

  10. #10
    Join Date
    Feb 2006
    Posts
    32
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Why are actions limited to menus and toolbars?

    Quote Originally Posted by wysota
    A checkbox is equal to a button with checkable property set to true.
    Yeah, but it doesn't look the same does it!?!! I want a little white box that I can tick. When using a QToolbutton with the checked property set, it just sets the button to be "pressed".

  11. #11
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Why are actions limited to menus and toolbars?

    Quote Originally Posted by Paul Drummond
    Yeah, but it doesn't look the same does it!?!! I want a little white box that I can tick. When using a QToolbutton with the checked property set, it just sets the button to be "pressed".
    Well... yes, that's how tool buttons usually behave

  12. #12
    Join Date
    Feb 2006
    Posts
    32
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Why are actions limited to menus and toolbars?

    Quote Originally Posted by wysota
    Well... yes, that's how tool buttons usually behave
    That's my point, I want CHECKBOXES!

    I understand what QAction does. You define an action once then the menu item and toolbar button associated with the action are sync'd. It's just a shame QAction is limited to menus and toolbars that's all.

    It's quite easy for me to do the synchronisation myself, I just didn't want to if QAction did it for me, but it obviously doesn't.

    Thanks guys for helping to make this clear to me.

  13. #13
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Why are actions limited to menus and toolbars?

    Hmm... when I think of "action" I belive this is something that I can trigger. With checkboxes I don't see how marking it as checked or not triggeres some action. Of course, an action can be checkable, but it still is (logically) associated with some action being taken. For me it would make more sense to associate an action with a QPushButton more than with QCheckBox. But we have QToolButton for that An action associates things like text, icon, tool tip, etc. not only a slot which will get run after activating the widget. Both QPushButton and QCheckBox (and QRadioButton too) are just not meant for actions. If you want, you can always subclass them and add a feature of associating an action with each of them. There is a changed() signal in QAction. I belive you can use it for that.

  14. The following user says thank you to wysota for this useful post:

    desch (27th November 2007)

  15. #14
    Join Date
    Feb 2006
    Posts
    32
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Why are actions limited to menus and toolbars?

    What about checkable menu items then? They can be associated with actions. I can't see how QToolButton and QMenuItems are "special" in that they are the only widgets that can be syncronised with QAction. A Checkbox is a button just like a QToolButton so it should work with QAction IMO.

  16. #15
    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: Why are actions limited to menus and toolbars?

    A checkbox can have 3 states..

  17. #16
    Join Date
    Feb 2006
    Posts
    32
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Why are actions limited to menus and toolbars?

    Quote Originally Posted by jpn
    A checkbox can have 3 states..
    You are missing my point. There is no point arguing that a QAction can't work with a checkbox!!!! It works with checkable menu items!!!

  18. #17
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Why are actions limited to menus and toolbars?

    Qt Code:
    1. class QActionCheckBox : public QCheckBox {
    2. Q_OBJECT
    3. public:
    4. QActionCheckBox(QAction *act, QWidget *p = 0);
    5. const QAction *action() const { return _action; }
    6. void setAction(QAction *a){ initAction(a); }
    7. private:
    8. QPointer<QAction> _action;
    9. void initAction(QAction *a);
    10. protected slots:
    11. void updateAction();
    12. };
    13.  
    14. QActionCheckBox::QActionCheckBox(QAction *act, QWidget *p) : QCheckBox(p){
    15. if(_action) initAction(act);
    16. }
    17.  
    18. void QActionCheckBox::initAction(QAction *a){
    19. _action = a;
    20. if(_action){
    21. connect(_action, SIGNAL(changed()), this, SLOT(updateAction()));
    22. connect(this, SIGNAL(stateChanged(bool)), _action, SLOT(setChecked(bool)));
    23. updateAction();
    24. }
    25. }
    26.  
    27. void QActionCheckBox::updateAction(){
    28. if(!action) return;
    29. setEnabled(_action->isEnabled());
    30. setText(_action->text());
    31. if(_action->isCheckable()) setChecked(_action->isChecked());
    32. }
    To copy to clipboard, switch view to plain text mode 

    Satisfied? You can make a designer plugin with this class and place this widget on a form. I haven't checked it but even if it contains errors, the idea is simple, so you'll be able to modify it.
    Last edited by wysota; 3rd March 2006 at 12:10. Reason: Added a line of code

Similar Threads

  1. More problems with Menus and Toolbars
    By mridey in forum Qt Tools
    Replies: 2
    Last Post: 16th November 2006, 11:37

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.