Results 1 to 19 of 19

Thread: Need Image rightside of QListwidget

Hybrid View

Previous Post Previous Post   Next Post Next Post
  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

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
  •  
Qt is a trademark of The Qt Company.