Results 1 to 4 of 4

Thread: Render child widget relative to paint event rectangles

  1. #1
    Join Date
    Jun 2014
    Posts
    30
    Thanks
    4
    Qt products
    Qt5

    Default Render child widget relative to paint event rectangles

    We want to paint some custom rectangles and then put a QPushButton in them.

    Capture.PNG

    Let's say we want to put the QPushButton in the top rectangle. How do we do it?

    Here's our paint event:

    Qt Code:
    1. void ChannelAxisList::paintEvent(QPaintEvent* pEvent)
    2. {
    3. QPainter painter(this);
    4.  
    5. QRect rt = pEvent->rect();
    6. painter.setBrush(QBrush(QColor(255, 255, 255, 0)));
    7. painter.setPen(QPen(QColor(255, 255, 255), 1));
    8.  
    9. rt.setRight(rt.right() - 1);
    10. rt.setBottom(rt.bottom() - 1);
    11.  
    12. painter.drawRect(rt);
    13.  
    14. rt.setTopLeft(QPoint(0, 0));
    15. calculateLayout(rt);
    16.  
    17.  
    18. painter.setBrush(QBrush(QColor(255, 255, 255, 0)));
    19. painter.setPen(QPen(QColor(255, 255, 255), 1));
    20.  
    21. painter.drawRect(m_mainRect);
    22.  
    23. painter.setBrush(QBrush(QColor(255, 0, 0)));
    24. painter.setPen(QPen(QColor(0, 255, 0), 1));
    25. painter.setRenderHint(QPainter::Antialiasing, false);
    26.  
    27. painter.drawRoundedRect(m_header, 2, 2);
    28. painter.drawRoundedRect(m_list, 2, 2);
    29. }
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Feb 2016
    Location
    Kanagawa, Japan
    Posts
    1
    Qt products
    Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Render child widget relative to paint event rectangles

    Creating QPusuButton code isn't in paintEvent, but in constructor.

    It seems you already prepared 'm_header'.
    Hou about use QPushButton::setGeometry() ?

    Qt Code:
    1. ChannelAxisList()
    2. {
    3. this->m_button = new QPushButton(this);
    4. }
    5.  
    6. void ChannelAxisList::otherMethod()
    7. {
    8. // m_header is changed here...
    9. this->m_header = QRect(QPoint(), this->size()); // sample
    10.  
    11. this->m_button->setGeometry(this->m_header);
    12. }
    To copy to clipboard, switch view to plain text mode 

  3. #3
    Join Date
    Jun 2014
    Posts
    30
    Thanks
    4
    Qt products
    Qt5

    Default Re: Render child widget relative to paint event rectangles

    Very bizarre things happen when I move() or setGeometry() inside a paintEvent(). It appears in the right place until the mouse hovers over it. Then the widget jumps away from the mouse and leaves a trailing rectangle behind.

  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: Render child widget relative to paint event rectangles

    Quote Originally Posted by jmalicke View Post
    Very bizarre things happen when I move() or setGeometry() inside a paintEvent(). It appears in the right place until the mouse hovers over it. Then the widget jumps away from the mouse and leaves a trailing rectangle behind.
    Why would you move a widget in its parent's paintEvent()?
    That is where you paint.

    Cheers,
    _

Similar Threads

  1. Replies: 4
    Last Post: 25th February 2013, 13:40
  2. Replies: 1
    Last Post: 2nd November 2011, 20:30
  3. Replies: 7
    Last Post: 14th January 2010, 08:47
  4. Sending event from child widget
    By scascio in forum Qt Programming
    Replies: 6
    Last Post: 28th September 2009, 21:18
  5. How to get focus event in child widget
    By YuriyRusinov in forum Qt Programming
    Replies: 4
    Last Post: 17th October 2006, 07:53

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.