Results 1 to 20 of 22

Thread: custom painting in QListWidget

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Mar 2007
    Posts
    13
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default Re: custom painting in QListWidget

    thanks.
    i simply could not see that.
    but now i seem to have another problem.

    Qt Code:
    1. void MyDelegate::paint ( ..)
    2. {
    3. QStyleOptionViewItem newOption( option );
    4. newOption.rect.translate(QPoint(40,40));
    5.  
    6. QItemDelegate::paint(painter, newOption, index);
    7. }
    To copy to clipboard, switch view to plain text mode 

    when i select items the widget doesnt display the items correctly.
    for example, if i select the 2nd item, the horizontal lower half of the first item is erased but the selected item is displayed ok.
    but after i play around with selection the whole thing becomes a bigger mess.

    what do you think ?
    is delegate an option for modifying ListWidget items position ?
    is this possible ?

    thanks
    Last edited by wysota; 29th March 2007 at 06:25. Reason: missing [code] tags

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: custom painting in QListWidget

    Quote Originally Posted by Andrew View Post
    when i select items the widget doesnt display the items correctly.
    Actually it's almost the same solution as using QPainter::translate(). What you can try to do is to change the decorationSize --- this should move items to the right.

  3. #3
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,018 Times in 4,794 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: custom painting in QListWidget

    Maybe you could just center the item using regular methods (by setting the alignment role)? Depends how much you want your items shifted...

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: custom painting in QListWidget

    If you could post a screenshot, we would be able to fully understand why and where do you want to move those items.

  5. #5
    Join Date
    Mar 2007
    Posts
    13
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default Re: custom painting in QListWidget

    the default item painting in QListWidget is with icon painted very close to the widget left margin.
    i want to have an offset.
    in the same way, i want a vertical offset from the top.

    i am using icon and text in items.

    at this moment I see that the solution is to custom paint in a delegate the item (paint the icon and text and my desired locations).

    what do you think ?

    I have a question. how do I find out the QListWidget client rectangle minus the area occupied by the horizontal scrollbar widget ?
    after i add items, I need to determine the width of this area.
    i guess QListWidget::contentsRect( ) is the widget client rect but how do I find the scrollbar width ?
    i tried with
    ui.listWidget->verticalScrollBar()->rect()
    after i inserted the items but it doesnt give the real size.
    is there any way to find a 'metric' of a scrollbar (like how in win32 api is GetSystemMetrics if i remember right)
    Last edited by Andrew; 30th March 2007 at 01:52.

  6. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,018 Times in 4,794 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: custom painting in QListWidget

    You don't have to do any custom painting. Just subclass QItemDelegate and reimplement its drawDisplay and drawDecoration methods calling the base class implementation with a reduced rectangle.
    Qt Code:
    1. void MyItemDelegate::drawDecoration ( QPainter * painter, const QStyleOptionViewItem & option, const QRect & rect, const QPixmap & pixmap ) const {
    2. QItemDelegate::drawDecoration(painter, option, rect.adjusted(2,2,0, -2), pixmap);
    3. }
    To copy to clipboard, switch view to plain text mode 

    You can also reimplement sizeHint to make sure the item is bigger than is needed to fit the text and icon.

  7. #7
    Join Date
    Mar 2007
    Posts
    13
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default Re: custom painting in QListWidget

    I didnt look at QItemDelegate to see what can be overriden.
    Thanks alot !

    I still need to know the width of the vertical scrollbar.
    I'll try to explain why.
    I want QListWidget to display only 4 items at once (icons with text underneath)
    (so the client rect is splitted in 4 equal parts).

    For this i set IconMode view mode.
    In order to display only 4 items I need to compute an item size and call QListWidgetItem::setHintSize( )
    The item width cannot be just QListWidget::contentsRect().Width( ) / 2 because I need to take into account the left vertical scrollbar width also. Otherwise a horizontal scrollbar will appear.

    that's why I need the width of the vertical scrollbar.

    After I inserted items I tried to call
    QRect rc = ui.listWidget->verticalScrollBar()->rect();
    but it does not give consistent results.
    I observed that the right size is computed after the first display.
    is there a way to get scrollbar metrics \ force internal computation before actual painting ?
    Last edited by Andrew; 30th March 2007 at 13:27.

  8. #8
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,018 Times in 4,794 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: custom painting in QListWidget

    Qt Code:
    1. int width = style()->pixelMetric(QStyle::PM_ScrollBarExtent);
    To copy to clipboard, switch view to plain text mode 

Similar Threads

  1. custom widgets painting image formats
    By TheKedge in forum Qt Programming
    Replies: 1
    Last Post: 12th March 2007, 10:33
  2. Replies: 1
    Last Post: 5th March 2007, 20:50
  3. Replies: 13
    Last Post: 15th December 2006, 11:52
  4. keypress while editing an item in QListWidget
    By Beluvius in forum Qt Programming
    Replies: 3
    Last Post: 4th April 2006, 09:56
  5. Replies: 16
    Last Post: 7th March 2006, 15:57

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.