Results 1 to 12 of 12

Thread: [SOLVED] Issues with displaying rich text for a QCheckBox

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Sep 2014
    Posts
    27
    Thanks
    2
    Thanked 2 Times in 1 Post
    Qt products
    Qt5
    Platforms
    Windows

    Default [SOLVED] Issues with displaying rich text for a QCheckBox

    So I've been wrestling with this problem for most of the work day, and at this point I think I need some outside feedback. I've been working with Qt for a couple months now and I'm pretty well versed in C++ so I've got the basics of Qt down and have learned a few of its subtleties, but I can't find an elegant solution to my issue.

    I need to have superscript text displayed in the label of a QCheckBox. Now, your initial thought may be to just place and empty check box next to a label which can display rich text. I have seen this suggested many times in various posts about this issue, but I have two problems with this:

    1. Clicking the label does not toggle the check box.
    2. Hovering over the label does not trigger the hoverstate of the check box


    The first problem is easy enough to solve. You just need to subclass QLabel and implement mouseClickEvent and emit a clicked signal to connect to the check box's toggle slot. The second problem is somewhat tricky however, and I'm pretty stumped.

    My initial instinct was to look for a setState or some such method which would allow me to tell the QCheckBox that it is being hovered over, however no such method exists that I could find. I even tried to use QWidget.setProperty("hover", true but it had no effect. So then I thought that I could place a QCheckBox and a QLabel inside of a custom Widget container which would detect mouseover and if the mouse is over the label I would create a false event containing coordinates of the QCheckBox and pass that to the QCheckBox so it would think it is being hovered over. However you can't explicitly call mouseMoveEvent on a widget. I also tried subclassing QCheckBox and using QTextDocument in the paintEvent to draw rich text next to the checkBox after calling the base class paint event. This worked, but again the checkbox was not counting the drawn text as part of it's contents so hover state was not being triggered.

    I feel like I'm close to finding a workable solution here but I just need a push in the right direction. I don't need anyone to write any code for me, but if there's a method I'm missing or something else I could try while subclassing QCheckBox I would be very grateful to have that pointed out.

    If all else fails I will just have to stick with a clickable label next to a checkbox, but I would like to avoid that because there are many other checkboxes without the need for rich text in my application and it would be great for them all to have the same behavior.

  2. #2
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Issues with displaying rich text for a QCheckBox

    Quote Originally Posted by forgottenduck View Post
    So then I thought that I could place a QCheckBox and a QLabel inside of a custom Widget container which would detect mouseover and if the mouse is over the label I would create a false event containing coordinates of the QCheckBox and pass that to the QCheckBox so it would think it is being hovered over. However you can't explicitly call mouseMoveEvent on a widget.
    No, but you could call the event() method (which is the entry point for all events a widget processes) or even us QCoreApplication::sendEvent() to push an event through the normal event processing chain.

    Cheers,
    _

  3. #3
    Join Date
    Sep 2014
    Posts
    27
    Thanks
    2
    Thanked 2 Times in 1 Post
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Issues with displaying rich text for a QCheckBox

    or even us QCoreApplication::sendEvent() to push an event through the normal event processing chain.
    Using send event did allow me to do what I intended but nothing appears to be happening. Here's my class as of right now:

    Qt Code:
    1. CheckBoxTest::CheckBoxTest(QString text, QWidget *parent) :
    2. QWidget(parent)
    3. {
    4. this->setMouseTracking(true);
    5. label = new QLabel(text);
    6. checkBox = new QCheckBox;
    7. QHBoxLayout* layout = new QHBoxLayout;
    8. layout->addWidget(label);
    9. layout->addWidget(checkBox);
    10. this->setLayout(layout);
    11. }
    12.  
    13. void CheckBoxTest::mouseMoveEvent(QMouseEvent *event){
    14. if(label->contentsRect().contains(event->pos())){
    15. QMouseEvent* newEvent = new QMouseEvent(QEvent::MouseMove, checkBox->mapFromGlobal(checkBox->pos()), Qt::NoButton, Qt::NoButton, Qt::NoModifier);
    16. QCoreApplication::sendEvent(checkBox, newEvent);
    17. }else{
    18. event->ignore();
    19. }
    20. }
    To copy to clipboard, switch view to plain text mode 

    Mouse events are passed on to checkBox when not within the label. When within the label the newEvent is created with a mouse position of checkBox's position and I use sendEvent to send it to checkBox which should trigger a hover state whenever the mouse is in the contents of the label. Any idea why nothing seems to be happening?

  4. #4
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Issues with displaying rich text for a QCheckBox

    Maybe the trigger is not a mouse event but the enter event.

    After all, "hover" only changes on enter/leave

    Cheers,
    _

Similar Threads

  1. [SOLVED] QCheckBox Tristate
    By ToddAtWSU in forum Qt Programming
    Replies: 0
    Last Post: 15th January 2014, 19:33
  2. QTabbar with rich text / html tab text
    By Berryblue031 in forum Qt Programming
    Replies: 1
    Last Post: 21st May 2012, 09:46
  3. Rich text with QPainter?
    By sarefo in forum Qt Programming
    Replies: 3
    Last Post: 7th April 2008, 14:40
  4. QTextEdit + paste rich text as plain text only
    By Yong in forum Qt Programming
    Replies: 2
    Last Post: 6th February 2008, 16:45
  5. QWidget pixmap AFTER displaying html ("rich text")
    By gavrilo princep in forum Newbie
    Replies: 0
    Last Post: 17th July 2007, 01:59

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
  •  
Qt is a trademark of The Qt Company.