Page 1 of 2 12 LastLast
Results 1 to 20 of 25

Thread: How do draw a frame in QListView?

  1. #1
    Join Date
    Dec 2006
    Posts
    31
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default How do draw a frame in QListView?

    qt-4.2

    I try to draw this way:
    Qt Code:
    1. myView::myView(QWidget *parent)
    2. : QListView(parent)
    3. {
    4. }
    5.  
    6. void myView::paintEvent(QPaintEvent *event)
    7. {
    8.  
    9. QStyleOptionViewItem option = viewOptions();
    10.  
    11. QPainter painter(viewport());
    12. painter.save();
    13.  
    14. QPen pen(Qt::blue, 5, Qt::SolidLine, Qt::RoundCap, Qt::RoundJoin);
    15. painter.setPen(pen);
    16. QRectF rectangle(option.rect.x(), option.rect.y(), option.rect.width(),option.rect.height());
    17. painter.drawRoundRect(rectangle,5,5);
    18. painter.restore();
    19. }
    To copy to clipboard, switch view to plain text mode 
    but all items are being overlaped, though the scroll works and you can see that there are items there (behind the rectangle) How to draw on background behind items?

  2. #2
    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: How do draw a frame in QListView?

    The easiest way would be to reimplement the delegate or modify the palette (depending on what you want to do). Probably using stylesheets is an option as well, if QListView is stylable.

    BTW. Don't you think you should at least call the base class implementation for the items to be drawn?

    I think you're either trying to do something weird or trying to do something useful the weird way What exactly are you trying to achieve?

  3. #3
    Join Date
    Dec 2006
    Posts
    31
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: How do draw a frame in QListView?

    I need a frame of an arbitrary form, with background of a solid color or texture. I would like to draw everything with primitives. I've done it this way:
    Qt Code:
    1. myView::myView(QWidget *parent)
    2. : QListView(parent)
    3. {
    4. }
    5.  
    6. void myView::paintEvent(QPaintEvent *event)
    7. {
    8. QStyleOptionViewItem option = viewOptions();
    9.  
    10. QPainter painter(viewport());
    11.  
    12. QPen pen(Qt::blue, 20, Qt::SolidLine, Qt::RoundCap, Qt::RoundJoin);
    13. painter.setPen(pen);
    14. QRect rectangle(option.rect.x(), option.rect.y(), option.rect.width()-10,option.rect.height()-10);
    15. painter.drawRoundRect(rectangle,10,10);
    16.  
    17. QListView::paintEvent(event);
    18. }
    To copy to clipboard, switch view to plain text mode 
    Both items and my rectangular can be drawn. But instead of the rectangular of the QListView size, a little area of blue color appears in the upper right corner. This blue piece scrolls with my list, however, I need to replace the standard border of QListView with my frame. What's wrong?

  4. #4
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: How do draw a frame in QListView?

    QListView inherits QFrame which has several options for modifying the frame. Maybe you can find something sufficient from there?

    In the above myView::paintEvent() you are drawing something by yourself first and then calling the QListView implementation which most likely overrides what you have drawn.

    QAbstractItemView::viewOptions() returns a QStyleOptionViewItem which is intended to be filled correctly by the view and then being passed for a delegate. The QStyleOptionViewItem::rect variable is a null rectangle.
    J-P Nurmi

  5. #5
    Join Date
    Dec 2006
    Posts
    31
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: How do draw a frame in QListView?

    How to get QFrame:ainter ?

  6. #6
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: How do draw a frame in QListView?

    Quote Originally Posted by someralex View Post
    How to get QFrame:ainter ?
    What is that?

    I meant something like:
    Qt Code:
    1. listView->setLineWidth(5);
    2. listView->setFrameStyle(QFrame::Panel | QFrame::Sunken);
    To copy to clipboard, switch view to plain text mode 
    J-P Nurmi

  7. #7
    Join Date
    Dec 2006
    Posts
    31
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: How do draw a frame in QListView?

    It I did already.
    now I need to draw on a background QListView.
    How?
    if i do
    Qt Code:
    1. myView::myView(QWidget *parent)
    2. : QListView(parent)
    3. {
    4. }
    5.  
    6. void myView::paintEvent(QPaintEvent *event)
    7. {
    8.  
    9. QPainter painter(viewport());
    10.  
    11. QPen pen(Qt::green, 10, Qt::SolidLine, Qt::RoundCap, Qt::RoundJoin);
    12. painter.drawText(rect(),Qt::AlignLeft,"Test DRive this List");
    13.  
    14. QListView::paintEvent(event);
    15. }
    To copy to clipboard, switch view to plain text mode 
    i get painter from QAbstractItemView and my text scroll with item.
    it follows from this that it is needed to draw on painter from QFrame or QWidget
    Last edited by someralex; 19th December 2006 at 18:18.

  8. #8
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: How do draw a frame in QListView?

    I'm not sure if this works with QWindowsXpStyle but works with QPlastiqueStyle, at least. Set QPalette::Base brush to Qt::transparent:
    Qt Code:
    1. QPalette p = listView->palette();
    2. p.setBrush(QPalette::Base, Qt::transparent);
    3. listView->setPalette(p);
    To copy to clipboard, switch view to plain text mode 

    And in this case draw before passing the paint event to the base class implementation.
    J-P Nurmi

  9. #9
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: How do draw a frame in QListView?

    Quote Originally Posted by someralex View Post
    i get painter from QAbstractItemView and my text scroll with item.
    it follows from this that it is needed to draw on painter from QFrame or QWidget
    So you want the drawing to stay in corresponding place at background when scrolling?
    Qt Code:
    1. QPainter painter(viewport());
    2. painter.translate(-horizontalScrollBar()->value(), -verticalScrollBar()->value());
    3. painter.drawSomething(...);
    To copy to clipboard, switch view to plain text mode 
    J-P Nurmi

  10. #10
    Join Date
    Dec 2006
    Posts
    31
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: How do draw a frame in QListView?

    this code work
    Qt Code:
    1. QPainter painter(viewport());
    2. QPen pen(Qt::green, 10, Qt::SolidLine, Qt::RoundCap, Qt::RoundJoin);
    3. painter.setPen(pen);
    4. painter.drawRect(rect());
    5. painter.translate(-horizontalScrollBar()->value(), -verticalScrollBar()->value());
    6. QListView::paintEvent(event);
    To copy to clipboard, switch view to plain text mode 
    but :
    1.at scrolling of list, top and bottom lines of rectangle erase.If using painter.translate identical effect
    2.rectangle paint over item

  11. #11
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: How do draw a frame in QListView?

    Translate first, then draw. Translating the painter after painting has no effect. The default QItemDelegate painting implementation fills items' background with QPalette::Base. That's why I told you to use a transparent brush.
    J-P Nurmi

  12. #12
    Join Date
    Dec 2006
    Posts
    31
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: How do draw a frame in QListView?

    after using
    Qt Code:
    1. QPalette p = listView->palette();
    2. p.setBrush(QPalette::Base, Qt::transparent);
    3. listView->setPalette(p);
    To copy to clipboard, switch view to plain text mode 
    all ok. Thanks
    but rectangle paint over viewport of scrollView
    How i need to replace the standard border?
    QFrame:ainter() ?

  13. #13
    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: How do draw a frame in QListView?

    There is no such thing as "QFrame::Painter()".

  14. #14
    Join Date
    Dec 2006
    Posts
    31
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: How do draw a frame in QListView?

    where then to draw decoration of widget - border,shade ?

  15. #15
    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: How do draw a frame in QListView?

    It's drawn in the paintEvent (probably using QStyle primitives). I don't think you really wish to change that, but of course it is possible by reimplementing the event and drawing your own frame. It might be simpler to use stylesheets to substitute the frame with your border image or changing the style to draw another frame.

  16. #16
    Join Date
    Dec 2006
    Posts
    31
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: How do draw a frame in QListView?

    Can I obtain painter for parent QFrame in paintEvent method in QListView and re-draw it the way I want? If I can, how can I get it? Or do I need to create my own QFrame based on standard and create my ListView based on QListView and QFrame (class myView(QWidget *parent = 0) : public QListView, public myFrame) ?

  17. #17
    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: How do draw a frame in QListView?

    You don't "obtain" a painter, you create it:
    Qt Code:
    1. void MyWidget::paintEvent(QPaintEvent *e){
    2. QPainter painter(this);
    3. painter.drawRect(...);
    4. //...
    5. }
    To copy to clipboard, switch view to plain text mode 

  18. #18
    Join Date
    Dec 2006
    Posts
    31
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: How do draw a frame in QListView?

    If I use:
    Qt Code:
    1. QPainter painter(this);
    To copy to clipboard, switch view to plain text mode 
    nothing can be drawn and I get an error :

    QPainter::begin: Widget painting can only begin as a result of a paintEvent

    when cursor of the mouse enters the area of the window where widget is placed.

  19. #19
    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: How do draw a frame in QListView?

    Obiously you can only create a painter in a paint event, like in the snippet I gave you in the previous post.

  20. #20
    Join Date
    Dec 2006
    Posts
    31
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: How do draw a frame in QListView?

    yes, I do exactly just like shown in your previous post. it gives me an error and draws nothing

Similar Threads

  1. Replies: 0
    Last Post: 10th November 2006, 13:46
  2. Using QGLWidget paint engine to draw regular widgtes?
    By high_flyer in forum Qt Programming
    Replies: 11
    Last Post: 9th October 2006, 12:06
  3. Replies: 16
    Last Post: 7th March 2006, 15:57
  4. Multi frame management ... is it possible ?
    By yellowmat in forum Newbie
    Replies: 8
    Last Post: 25th January 2006, 10:41
  5. Keeping focus at bottom of QListView
    By jakamph in forum Qt Programming
    Replies: 4
    Last Post: 10th January 2006, 14:45

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.