PDA

View Full Version : Need Image rightside of QListwidget



rajeshs
26th June 2007, 14:47
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

jpn
26th June 2007, 14:58
subclass QItemDelegate
reimplement QItemDelegate::paint()
calculate sufficient areas for decoration, check, etc.
call each protected QItemDelegate::draw*() with calculated areas so that QItemDelegate::drawDecoration() ends up called twice; once with original icon and second time with icon being drawn on the right

rajeshs
27th June 2007, 04:32
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

jpn
27th June 2007, 04:59
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. :)


class MyItemDelegate : public QItemDelegate
{
public:
...

void paint(QPainter* painter, const QStyleOptionViewItem& option, const QModelIndex& index) const
{
// calculate suitable area for text (excluding decorations)
QString text = index.data(Qt::DisplayRole).toString();
QRect displayRect = calcDisplayRect(text, option.fontMetrics);

// calculate suitable area for decoration on the left
QIcon icon1 = qvariant_cast<QIcon>(index.data(Qt::DecorationRole));
QPixmap pixmap1 = icon1.pixmap(option.decorationSize);
QRect decoration1Rect = calcDecorationRect(pixmap1, Qt::AlignLeft);

// calculate suitable area for decoration on the right
QIcon icon2 = qvariant_cast<QIcon>(index.data(Qt::UserRole));
QPixmap pixmap2 = icon2.pixmap(option.decorationSize);
QRect decoration2Rect = calcDecorationRect(pixmap2, Qt::AlignRight);

// draw 'em
drawBackground(painter, option, index);
drawDecoration(painter, option, decoration1Rect, pixmap1);
drawDecoration(painter, option, decoration2Rect, pixmap2);
drawDisplay(painter, option, displayRect, text);
drawFocus(painter, option, displayRect);
}
};

// usage:
listWidget->setItemDelegate(new MyItemDelegate(listWidget));

rajeshs
27th June 2007, 05:11
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

rajeshs
27th June 2007, 05:53
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

rajeshs
27th June 2007, 05:55
I am using QT 4.2 which header file i have to use for

calcDisplayRect() and calcDecorationRect()

Thanks
Rajesh.S

jpn
27th June 2007, 16:27
The example snippet above draws the ordinary icon (Qt::DecorationRole) on the left and a custom icon (Qt::UserRole) on the right:


listWidgetItem->setIcon(icon1); // same as setData(Qt::DecorationRole, QVariant::fromValue(icon1))
listWidgetItem->setData(Qt::UserRole, QVariant::fromValue(icon2));



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

rajeshs
28th June 2007, 04:25
Hello sir,

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

rajeshs
28th June 2007, 04:54
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

jpn
28th June 2007, 06:51
Is it possible to use same itemdelegate for 2 different listwidgets?
Yes, it is. Use QAbstractItemView::setItemDelegate() as usual.

rajeshs
28th June 2007, 07:11
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

jpn
28th June 2007, 07:37
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?

rajeshs
28th June 2007, 08:08
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

jpn
28th June 2007, 08:25
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.

Pardeep
11th February 2013, 13:10
Can any one give me idea for the definition of this function plz


calcDecorationRect(pixmap1, Qt::AlignLeft);

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.

d_stranz
11th February 2013, 21:41
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?

Pardeep
12th February 2013, 04:32
May be this function is used for the alignment of Icons with respect to rect (item's Rect)

d_stranz
12th February 2013, 20:46
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.