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

Thread: custom painting in QListWidget

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

    Default custom painting in QListWidget

    Hello,

    I am using a QListWidget in ListMode and I want the items to be displayed a little offset'ed (10 pixels in the right and down)

    How can I override the default items paint ?

    Do I need to create my own model ? Can i override paint event and change the input rectangle ?

    thank you.
    Last edited by Andrew; 28th March 2007 at 17:03.

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

    Default Re: How customize items of ModelView?

    { forked from: How customize items of ModelView? }

    if instead of the QTableView would be a QListView what are the posibilities to change an item position ?

    for example, when the mode in the list is Free you can move the items around in the widget so this tells me that items can be placed in a different position.

    what i want to achieve to make all the items in a QListView \ QListWidget to be displayed at an offset from the default position ( i want to make them appear more to the right and down)
    Last edited by jacek; 28th March 2007 at 23:39.

  3. #3
    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 customize items of ModelView?

    It doesn't matter which view you use, the model remains the same. If you want to modify the position of the text/icon of the item, it should be enough to reimplement the delegate and shift the painter before calling the base class drawing method.

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

    Default Re: How customize items of ModelView?

    Quote Originally Posted by wysota View Post
    and shift the painter before calling the base class drawing method.
    i implemented my own delegate and it works (it gets called)

    void MyDelegate:aint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
    {
    QItemDelegate:aint(painter, option, index);
    }

    but I don't know exactly how to achieve what i want (make the items to be displayed to the right with lets say 50 pixels)

    i tried with painter->translate(QPoint(50, 0))
    but obviously it will translate the coordinate system on each call to paint( ) which makes the items to be displayed in a diagonal

    so what i need is to set the origin of the system.

    i tried with setViewport and setWindow but its not working ( i guess the default implemenation of QItemDelegate:aint( ) set it )

    what remains is to change the matrix

    what do you think

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

    Default Re: How customize items of ModelView?

    no, it does not work
    modifying the coordinate system is not an option.

    when I select an item the selection is displayed on the old position. it's a mess.

    any other option for modifying a QListView\QListWidget item position ?

    i tried to look at how to implement my own model but i dont see a way to modify item position.
    i was not able to find any 'position' in QModelIndex \ QStyle

    any ideas ?
    Last edited by Andrew; 28th March 2007 at 19:14.

  6. #6
    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 customize items of ModelView?

    Quote Originally Posted by Andrew View Post
    i tried with painter->translate(QPoint(50, 0))
    but obviously it will translate the coordinate system on each call to paint( ) which makes the items to be displayed in a diagonal
    So translate it back after painting.
    Qt Code:
    1. void MyDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const{
    2. painter->translate(50, 0);
    3. QItemDelegate::paint(painter, option, index);
    4. painter->translate(-50,0);
    5. }
    To copy to clipboard, switch view to plain text mode 

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

    Default Re: How customize items of ModelView?

    no, modifying the coords does not work.. see my previous post

    btw, what i am trying to do is to use a QListWidget as a thumbnail viewer.
    but i need to modify the position of the items (offset'ed to the right) so they appear more centered

  8. #8
    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
    Do I need to create my own model ?
    You can't use a custom model with QListWidget. To do that you will have to switch to QListView.

    Quote Originally Posted by Andrew View Post
    Can i override paint event and change the input rectangle ?
    A custom delegate should be enough. See QItemDelegate.

  9. #9
    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 customize items of ModelView?

    Quote Originally Posted by Andrew View Post
    no, modifying the coords does not work.. see my previous post
    Modify the QStyleOptionViewItem argument then. You can change the rectangle for instance...

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

    Default Re: custom painting in QListWidget

    I did create my own delegate (a class derived from QItemDelegate and set using setItemDelegate )
    and overriden
    void paint ( QPainter * painter, const QStyleOptionViewItem & option, const QModelIndex & index ) const

    but i dont know how to modify the items position.
    can you please be specific ?

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

    Default Re: How customize items of ModelView?

    i dont see how you can modify QStyleOptionViewItem::rect
    pls tell

  12. #12
    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: How customize items of ModelView?

    Quote Originally Posted by Andrew View Post
    i dont see how you can modify QStyleOptionViewItem::rect
    You can modify its copy.

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

    Default Re: custom painting in QListWidget

    You can modify its copy.
    what's the purpose of modifying an object copy if you don;t modify the object itself ?

    i begin to lose any confident to find a solution to my problem.
    i see some people here reply just for the pleasure of doing that.

  14. #14
    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
    what's the purpose of modifying an object copy if you don;t modify the object itself ?
    Too pass further the changed object:
    Qt Code:
    1. void MyDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
    2. {
    3. QStyleOptionViewItem newOption( option );
    4. // change newOption
    5. QItemDelegate::paint(painter, newOption, index);
    6. }
    To copy to clipboard, switch view to plain text mode 

  15. #15
    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

  16. #16
    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.

  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: 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...

  18. #18
    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.

  19. #19
    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.

  20. #20
    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: 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.

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
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.