Results 1 to 3 of 3

Thread: Trying to overlay widget on a QTreeView item

  1. #1
    Join Date
    Dec 2011
    Posts
    60
    Thanks
    12
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Trying to overlay widget on a QTreeView item

    I'm trying to align a rectangle over the text portion of a QTreeView item. Calculating the rectangle is a bit complicated because the checkbox indents add up depending on the number of parents, etc.

    So to debug this I figured I'd just draw a physical rectangle over the math prediction. However, I'm unable to render a QWidget on top of a tree view item. It never shows up. Any ideas what's wrong or how to overlay a visual over a QTreeView item?

    Basically, I want to see something like this, so I can tell where my textRect calc is off.

    caThHaZ.png

    My attempt below. I'm trying to draw the rectangle in the treeview's mousePressEvent. But, no matter what I do, the overlay widget never appears...
    Qt Code:
    1. void TreeView::mousePressEvent (QMouseEvent *event)
    2. {
    3. if (event->button() == Qt::LeftButton)
    4. {
    5. QRect textRect = QRect(checkboxX
    6. + style()->pixelMetric(QStyle::PM_IndicatorWidth)
    7. + style()->pixelMetric(QStyle::PM_CheckBoxLabelSpacing)
    8. + 30 // color chip and padding
    9. + 3, // little padding
    10. vrect.y(),
    11. treeItemWidth,
    12. vrect.height());
    13.  
    14. // Overlay colored widget to see the rectangle
    15. PaintWidget *overlay = new PaintWidget(textRect, this);
    16. overlay->repaint(); // try to force the repaint
    17.  
    18. }
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. class PaintWidget : public QWidget
    2. {
    3. Q_OBJECT
    4.  
    5. public:
    6. explicit PaintWidget(const QRect &paintArea, QWidget *parent=nullptr)
    7. : QWidget(parent), m_area(paintArea)
    8. {
    9. setGeometry(m_area);
    10. QPalette pal = palette();
    11. // set white background
    12. pal.setColor(QPalette::Background, Qt::white);
    13. setPalette(pal);
    14. this->update();
    15. }
    16. ~PaintWidget(){};
    17.  
    18. private:
    19. QRect m_area;
    20. };
    To copy to clipboard, switch view to plain text mode 
    Last edited by alketi; 29th April 2020 at 18:25.

  2. #2
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,240
    Thanks
    303
    Thanked 865 Times in 852 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Trying to overlay widget on a QTreeView item

    You cannot do any on-screen painting anywhere except in a paintEvent().

    I think you can probably accomplish what you want with a QStyledItemDelegate. All of the geometry calculations are made for you. Take a look at the Star Delegate example.

    You might also be able to do this by adding additional cases to your QAbstractItemModel::data() method to handle the Qt::BackgroundRole.
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

  3. #3
    Join Date
    Dec 2011
    Posts
    60
    Thanks
    12
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Trying to overlay widget on a QTreeView item

    Thanks d_stranz, after some more attempts here's the working code.
    Qt Code:
    1. #include <QPaintEvent>
    2. #include <QPainter>
    3. #include <QDebug>
    4. class PaintWidget : public QWidget
    5. {
    6. Q_OBJECT
    7.  
    8. public:
    9. explicit PaintWidget(const QRect &paintArea, QWidget *parent=nullptr)
    10. : QWidget(parent), m_area(paintArea)
    11. {
    12. setAttribute(Qt::WA_NoSystemBackground);
    13. setAttribute(Qt::WA_TransparentForMouseEvents);
    14. }
    15. ~PaintWidget(){};
    16.  
    17. protected:
    18. void paintEvent(QPaintEvent */*event*/) override
    19. {
    20. QPainter(this).fillRect(m_area, {255, 0, 0, 100});
    21. }
    22. private:
    23. QRect m_area;
    24. };
    To copy to clipboard, switch view to plain text mode 

    And the calling function I resize the overlay widget to be the entire size of the treeview, but only paint the rectangle of interest.
    Qt Code:
    1. PaintWidget *overlay = new PaintWidget(textRect, this);
    2. overlay->resize(this->width(), this->height());
    3. overlay->show();
    To copy to clipboard, switch view to plain text mode 

Similar Threads

  1. Replies: 4
    Last Post: 25th August 2015, 10:12
  2. Replies: 2
    Last Post: 15th February 2011, 14:10
  3. QGraphicsscene item overlay issue
    By augusbas in forum Qt Programming
    Replies: 0
    Last Post: 9th November 2010, 06:16
  4. Layout (widget overlay)
    By whitefurrows in forum Qt Programming
    Replies: 3
    Last Post: 3rd July 2009, 23:54

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.