Results 1 to 7 of 7

Thread: QAction::setEnabled() Not Working as Expected

  1. #1
    Join Date
    Feb 2012
    Location
    Pittsburgh, Pennsylvania, USA
    Posts
    10
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default QAction::setEnabled() Not Working as Expected

    I've run into an odd problem when trying to disable a QAction menu item object. There's no good way to paste a small working example, but the code of note is as follows:

    Qt Code:
    1. ui.button_send->setEnabled(true);
    2. std::cout << ui.actionConnect->isEnabled() << std::endl;
    3. ui.actionConnect->setEnabled(false);
    4. std::cout << ui.actionConnect->isEnabled() << std::endl;
    To copy to clipboard, switch view to plain text mode 

    I am trying to have a button that I can click to connect to a simulator (details not important). When the simulator boots up, it sends a signal back that calls the function with this code. The print lines read "1" then "0" as expected, indicating that the actionConnect menu item is no longer enabled. However, I can still click on the meny item after this occurs, and it triggers another redundant attempt to connect (i.e., it is not disabled at all). The button_send button in ui, however, does get enabled properly (after being disabled on startup).

    I found a few places through online searching that indicate there may be bugs with QAction::setEnabled(), but nothing concrete. Is there anywhere else I should look to figure out what might be the problem? I know this is vague, so I'm mostly just looking for ideas or hints. Thanks in advance.

  2. #2
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: QAction::setEnabled() Not Working as Expected

    Is your simulator relinquishing control to the Qt event loop quickly, or taking a long time without processing events?

  3. #3
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: QAction::setEnabled() Not Working as Expected

    In the code you pasted, you show two different GUI objects, "button_send" (line 1) and "actionConnect" (lines 2 - 4), and the code never disables the "button_send" action. Is this a typo? If not, that's probably the reason why you can still execute the command.

  4. #4
    Join Date
    Feb 2012
    Location
    Pittsburgh, Pennsylvania, USA
    Posts
    10
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: QAction::setEnabled() Not Working as Expected

    Quote Originally Posted by ChrisW67 View Post
    Is your simulator relinquishing control to the Qt event loop quickly, or taking a long time without processing events?
    I don't think that's the problem. I tried it with a push button instead of a QAction menu item, and that one disables and grays out fine. Every other way of doing it has no problem, just with the QAction item.

    Anyway, for the time being, I've replaced it with a button. It doesn't need to be in the menu for my application, but I'm still curious to figure out the problem.

    Quote Originally Posted by d_stranz View Post
    In the code you pasted, you show two different GUI objects, "button_send" (line 1) and "actionConnect" (lines 2 - 4), and the code never disables the "button_send" action. Is this a typo? If not, that's probably the reason why you can still execute the command.
    The first line was actually just included in the code I pasted to show an example of something that did work. Lines 2-4 are the important ones. When I disable button_send later it disables fine.

  5. #5
    Join Date
    Nov 2010
    Posts
    315
    Thanked 53 Times in 51 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: QAction::setEnabled() Not Working as Expected

    Apparently you are misunderstanding something. After you add action to some object you shouldn't control this object, don't change it: enable state, title, icon and so on.
    All this state changes should be done by QAction. Note that not all buttons are handling QActions, QToolButton does.

  6. #6
    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: QAction::setEnabled() Not Working as Expected

    If you want an action and a button to do the same thing then replace your QPushButton instance with QToolButton and use QToolButton::setDefaultAction() passing it the action from the menu. Then the menu entry and the button will always be synchronized when you disable or otherwise change properties of the action object.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  7. #7
    Join Date
    Feb 2012
    Location
    Pittsburgh, Pennsylvania, USA
    Posts
    10
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: QAction::setEnabled() Not Working as Expected

    Quote Originally Posted by MarekR22 View Post
    Apparently you are misunderstanding something. After you add action to some object you shouldn't control this object, don't change it: enable state, title, icon and so on.
    All this state changes should be done by QAction. Note that not all buttons are handling QActions, QToolButton does.
    The menu bar item is a QAction though, and completely separate from the button. The Button and the QAction both had the same effect on the simulator side, but from Qt they send out different signals (although they shouldn't and won't when I do similar things in the future- see below). My point is that QAction.setEnabled(false) doesn't seem to be working. If I delete the button completely and just have the QAction menu item, it still doesn't work.

    Quote Originally Posted by wysota View Post
    If you want an action and a button to do the same thing then replace your QPushButton instance with QToolButton and use QToolButton::setDefaultAction() passing it the action from the menu. Then the menu entry and the button will always be synchronized when you disable or otherwise change properties of the action object.
    Yea that makes sense- I knew that and had just forgotten. I'm still learning all the ins and outs of Qt so sometimes things get forgotten .

Similar Threads

  1. QDir not working as expected
    By BettaUseYoNikes in forum Qt Programming
    Replies: 2
    Last Post: 24th August 2011, 16:55
  2. QWebElement setAttribute not working as expected
    By pherthyl in forum Qt Programming
    Replies: 2
    Last Post: 12th April 2011, 07:17
  3. Replies: 2
    Last Post: 22nd December 2010, 10:37
  4. Replies: 0
    Last Post: 17th August 2010, 14:37
  5. QTableView not working as expected.
    By junxuan in forum Qt Programming
    Replies: 7
    Last Post: 30th July 2009, 08:17

Tags for this Thread

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.