Results 1 to 19 of 19

Thread: Need Image rightside of QListwidget

  1. #1
    Join Date
    Jun 2007
    Location
    India/Bangalore
    Posts
    156
    Thanks
    26
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Thumbs up Need Image rightside of QListwidget

    Hi all ,

    I am using QlistWidget icon along with text as iems, that icon is comming in left side,
    I need another icon in rightside of the text? how can we draw icon within QListWidget?
    please help me.

    Thanks
    Rajesh.S

  2. #2
    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: Need Image rightside of QListwidget

    J-P Nurmi

  3. The following user says thank you to jpn for this useful post:

    rajeshs (27th June 2007)

  4. #3
    Join Date
    Jun 2007
    Location
    India/Bangalore
    Posts
    156
    Thanks
    26
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Need Image rightside of QListwidget

    Thank you for your reply , I am beginer to Qt, If you give some tips to how to use QItemDelegate, it will realy helpful to me.

    Thanks
    Rajesh.S

  5. #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: Need Image rightside of QListwidget

    I was thinking of something like this. Keep in mind that the following snippet was written on the fly for demonstration purposes and is missing a whole lot of checks for valid values (QVariant::isValid()) etc.
    Qt Code:
    1. class MyItemDelegate : public QItemDelegate
    2. {
    3. public:
    4. ...
    5.  
    6. void paint(QPainter* painter, const QStyleOptionViewItem& option, const QModelIndex& index) const
    7. {
    8. // calculate suitable area for text (excluding decorations)
    9. QString text = index.data(Qt::DisplayRole).toString();
    10. QRect displayRect = calcDisplayRect(text, option.fontMetrics);
    11.  
    12. // calculate suitable area for decoration on the left
    13. QIcon icon1 = qvariant_cast<QIcon>(index.data(Qt::DecorationRole));
    14. QPixmap pixmap1 = icon1.pixmap(option.decorationSize);
    15. QRect decoration1Rect = calcDecorationRect(pixmap1, Qt::AlignLeft);
    16.  
    17. // calculate suitable area for decoration on the right
    18. QIcon icon2 = qvariant_cast<QIcon>(index.data(Qt::UserRole));
    19. QPixmap pixmap2 = icon2.pixmap(option.decorationSize);
    20. QRect decoration2Rect = calcDecorationRect(pixmap2, Qt::AlignRight);
    21.  
    22. // draw 'em
    23. drawBackground(painter, option, index);
    24. drawDecoration(painter, option, decoration1Rect, pixmap1);
    25. drawDecoration(painter, option, decoration2Rect, pixmap2);
    26. drawDisplay(painter, option, displayRect, text);
    27. drawFocus(painter, option, displayRect);
    28. }
    29. };
    30.  
    31. // usage:
    32. listWidget->setItemDelegate(new MyItemDelegate(listWidget));
    To copy to clipboard, switch view to plain text mode 
    J-P Nurmi

  6. #5
    Join Date
    Jun 2007
    Location
    India/Bangalore
    Posts
    156
    Thanks
    26
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Thumbs up Re: Need Image rightside of QListwidget

    Thank you for your valuable answer,

    I got idea about item delegate,and how to use this delegate,

    How can we pass second image to our list using this delegate,

    Thanks
    Rajesh.S
    Last edited by rajeshs; 27th June 2007 at 05:28.

  7. #6
    Join Date
    Jun 2007
    Location
    India/Bangalore
    Posts
    156
    Thanks
    26
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Thumbs up Re: Need Image rightside of QListwidget

    Hello Sir,

    I am using the following code to set image as list item,
    using itemdelegate how can i pass second image

    Please help me,


    QString filen(":/resources/designer.png");
    QListWidget listwidget;
    QIcon icon(filen);

    for(int i=0;i<l5.size();i++)
    {
    //populating into listwidget by text and image1
    QListWidgetItem *item = new QListWidgetItem(icon,i,listwidget);
    item->setTextAlignment(Qt::AlignHCenter);
    item->setFont(fontSplitter3);
    }


    thanks
    Rajesh.S

  8. #7
    Join Date
    Jun 2007
    Location
    India/Bangalore
    Posts
    156
    Thanks
    26
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Need Image rightside of QListwidget

    I am using QT 4.2 which header file i have to use for

    calcDisplayRect() and calcDecorationRect()

    Thanks
    Rajesh.S

  9. #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: Need Image rightside of QListwidget

    The example snippet above draws the ordinary icon (Qt::DecorationRole) on the left and a custom icon (Qt::UserRole) on the right:
    Qt Code:
    1. listWidgetItem->setIcon(icon1); // same as setData(Qt::DecorationRole, QVariant::fromValue(icon1))
    2. listWidgetItem->setData(Qt::UserRole, QVariant::fromValue(icon2));
    To copy to clipboard, switch view to plain text mode 

    Quote Originally Posted by rajeshs View Post
    I am using QT 4.2 which header file i have to use for

    calcDisplayRect() and calcDecorationRect()
    These ones you are supposed to write by yourself. :)
    J-P Nurmi

  10. #9
    Join Date
    Jun 2007
    Location
    India/Bangalore
    Posts
    156
    Thanks
    26
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Thumbs up Re: Need Image rightside of QListwidget

    Hello sir,

    Thank you for your reply,Is it possible to use same itemdelegate for 2 different listwidgets?
    Thaks
    Rajesh.S

  11. #10
    Join Date
    Jun 2007
    Location
    India/Bangalore
    Posts
    156
    Thanks
    26
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Thumbs up Re: Need Image rightside of QListwidget

    I am using itemdelegate for Qlistwidget and In item delegate,i am using painting to display icons and text as follows ,

    if any other window pops up above this list , the painting in the list are faded away,

    How can we prevent this,

    My code in item delegate,

    painter->drawPixmap(MyDelegate::x,MyDelegate::y,pixmap1) ;
    painter->drawText(x+30,y+10,text);
    if(text==" 11 Esig")
    painter->drawPixmap(x+304,y,pixmap2);


    Here x and y are coordinates,

    please help me

  12. #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: Need Image rightside of QListwidget

    Quote Originally Posted by rajeshs View Post
    Is it possible to use same itemdelegate for 2 different listwidgets?
    Yes, it is. Use QAbstractItemView::setItemDelegate() as usual.
    J-P Nurmi

  13. #12
    Join Date
    Jun 2007
    Location
    India/Bangalore
    Posts
    156
    Thanks
    26
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Thumbs up Re: Need Image rightside of QListwidget

    I used same item delegate for 2 listwidgets if i do that 2 listwidgets are comming empty.

    and if any dialog pops up above to listwidget all items are faded away?

    How can we handel this? Please help me!

    Thaks
    Rajesh.S

  14. #13
    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: Need Image rightside of QListwidget

    Geometry of painted item is passed in the style option. What are MyDelegate::x and MyDelegate::y? What's wrong with the approach I presented above?
    J-P Nurmi

  15. #14
    Join Date
    Jun 2007
    Location
    India/Bangalore
    Posts
    156
    Thanks
    26
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Need Image rightside of QListwidget

    Hello sir,

    How to get geometry

    Sorry, I didn't get how to get geometry of painting item, So i used my own points to paint the items that x and y are my points, How can we get geometry from style option and what is style option ? Plese help me.

    Thaks for your response,

    Rajesh,S

  16. #15
    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: Need Image rightside of QListwidget

    Quote Originally Posted by rajeshs View Post
    How to get geometry

    [B]Sorry, I didn't get how to get geometry of painting item, So i used my own points to paint the items that x and y are my points, How can we get geometry from style option and what is style option ?
    A style option is used to pass information on how to render the corresponding item. A QStyleOptionViewItem is passed as second parameter to QItemDelegate::paint() (see the example in paint() docs). Use QStyleOption::rect to get the geometry.
    J-P Nurmi

  17. #16
    Join Date
    Oct 2012
    Location
    Gurgaon, Haryana, India
    Posts
    15
    Thanks
    5
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Need Image rightside of QListwidget

    Can any one give me idea for the definition of this function plz
    Qt Code:
    1. calcDecorationRect(pixmap1, Qt::AlignLeft);
    To copy to clipboard, switch view to plain text mode 

    i also needed to display same type of view.
    As i am new user in qt so thats why i am not getting any idea how to write this function's definition.

  18. #17
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Need Image rightside of QListwidget

    i am not getting any idea how to write this function's definition.
    What do you think this function is supposed to do? How is its result used? What have you tried so far?

  19. #18
    Join Date
    Oct 2012
    Location
    Gurgaon, Haryana, India
    Posts
    15
    Thanks
    5
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Need Image rightside of QListwidget

    May be this function is used for the alignment of Icons with respect to rect (item's Rect)

  20. #19
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Need Image rightside of QListwidget

    May be this function is used for the alignment of Icons with respect to rect (item's Rect)
    Yes, so how have you tried to implement it? If you want the item to look like this: [Icon1][Item text][Icon2] how would you determine the size and position of the [Icon] rectangles?

    I'm not going to write the code for you. You have to make an attempt to do it yourself first, and if you fail, post the code you tried and we will help you get it right.

Similar Threads

  1. Help needed handling image data
    By toratora in forum General Programming
    Replies: 2
    Last Post: 11th May 2007, 09:24
  2. how i can add image in my toolbar
    By jyoti in forum Qt Tools
    Replies: 7
    Last Post: 19th December 2006, 14:39
  3. Replies: 13
    Last Post: 15th December 2006, 11:52
  4. How and when to repaint a widget ?
    By yellowmat in forum Newbie
    Replies: 7
    Last Post: 3rd April 2006, 16:36
  5. Question about updating an image on screen
    By SkripT in forum Qt Programming
    Replies: 1
    Last Post: 24th February 2006, 19:01

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.