2

I have a Qt form, where I have a button and menu. For various reasons I can disable certain elements, e.g button or some actions in the menu.

Is there a way I could show a tooltip or when the mouse is hovered over the disabled button or menu item with an explanation as to why it is disabled?

I am using Qt 4.8.

Thanks!

Serge
  • 1,027
  • 14
  • 21
  • Cannot edit my post for some reason: when I disable the widget, no tooltips show up, because disabled widgets don't receive mouse events – Serge Oct 26 '16 at 21:06
  • 1
    Please provide some code. As I have mentioned (and also now updated my post) tooltips work for both enabled and disabled widgets. Perhaps you are doing something with the received events (see [here](http://stackoverflow.com/a/8470888/1559401)). – rbaleksandar Oct 26 '16 at 21:36

1 Answers1

3

You can set the tooltip dynamically based on the state of the QWidget or by simply toggling both at the same time. Upon disabling/enabling the widget from somewhere just call QWidget::setToolTip(...) with the QString you want the tooltip to display when hovering with the mouse over the given widget. For example if you have a public slot called toggleButton(bool toggleFlag) which toggles the enable-setting of a button you can do:

void MyWidget::toggleButton(bool toggleFlag) {
    this->ui->myButton->setEnabled(toggleFlag);
    this->ui->myButton->setToolTip(toggleFlag ? QString("Enabled wohoo!") : QString("Disabled because I like it"));
}

You can of course do also change the tooltip by calling QWidget::isEnabled() and act upon its return value. Since you haven't given any code I can only assume how you toggle your button(s) so that's all I can give you for now.


UPDATE: It was pointed in the comments that tooltips don't work with disabled widgets due not receiving mouse events. Both statements are not true (note that I have used the same tooltip message since due to lack of minimal working example I didn't want to write a whole new project from scratch and used an existing one of mine instead):

  • Hovering a disabled button triggers the tooltip

enter image description here

  • Hovering an enabled button triggers the tooltip

enter image description here

rbaleksandar
  • 8,713
  • 7
  • 76
  • 161
  • Thanks, probably I should have mentioned it in my question - I'll update it: when I disable the widget, no tooltips show up, because disabled widgets don't receive mouse events – Serge Oct 26 '16 at 21:04
  • 1
    They do. I have tested it before I posted. Will post some images so that you can see. I also made sure I use the Qt 4.8.6 kit instead of the 5.7 that I currently use for development. – rbaleksandar Oct 26 '16 at 21:29
  • Sorry, true - there was a fault in my logic - now I do see the tooltip over the disabled button. – Serge Oct 27 '16 at 14:07
  • Glad it worked out. Btw when in doubt always check the docs and see if the behaviour you are experiencing is documented in a way. If not, then you have either found a bug or (as you found out) there is something wrong in your logic. In most situations it's the second case and not the first one that reflects reality. ;) – rbaleksandar Oct 28 '16 at 05:32