PDA

View Full Version : Problem in creating more than one Qt::UserRole QListWidgetItem



sivambigai
4th October 2010, 07:27
Hai ,
I need more than one Qt::UserRole. But not Displaying
Here is my code please help me to spot out the problem

void CSmsThreadListWidgetItem::paint ( QPainter * painter, const QStyleOptionViewItem & option, const QModelIndex & index ) const{
QRect r = option.rect;

//Color: #C4C4C4
QPen linePen(QColor::fromRgb(211,211,211), 1, Qt::SolidLine);

//Color: #005A83
QPen lineMarkedPen(QColor::fromRgb(0,90,131), 1, Qt::SolidLine);

//Color: #333
QPen fontPen(QColor::fromRgb(51,51,51), 1, Qt::SolidLine);

//Color: #fff
QPen fontMarkedPen(Qt::white, 1, Qt::SolidLine);

//GET TITLE, DESCRIPTION AND ICON
QIcon ic = QIcon(qvariant_cast<QPixmap>(index.data(Qt::DecorationRole)));
QString title = index.data(Qt::DisplayRole).toString();
QString description = index.data(Qt::UserRole).toString();
QString dateAndTime = index.data(Qt::UserRole+1).toString();

if(option.state & QStyle::State_Selected){
// if(title == "Me"){
QLinearGradient gradientSelected(r.left(),r.top(),r.left(),r.heigh t()+r.top());
gradientSelected.setColorAt(0.0, QColor::fromRgb(119,213,247));
gradientSelected.setColorAt(0.9, QColor::fromRgb(27,134,183));
gradientSelected.setColorAt(1.0, QColor::fromRgb(0,120,174));
painter->setBrush(gradientSelected);
painter->drawRect(r);

//BORDER
painter->setPen(lineMarkedPen);
painter->drawLine(r.topLeft(),r.topRight());
painter->drawLine(r.topRight(),r.bottomRight());
painter->drawLine(r.bottomLeft(),r.bottomRight());
painter->drawLine(r.topLeft(),r.bottomLeft());

painter->setPen(fontMarkedPen);

} else {
//BACKGROUND
//ALTERNATING COLORS
painter->setBrush( (index.row() % 2) ? Qt::white : QColor(252,252,252) );
painter->drawRect(r);

//BORDER
painter->setPen(linePen);
painter->drawLine(r.topLeft(),r.topRight());
painter->drawLine(r.topRight(),r.bottomRight());
painter->drawLine(r.bottomLeft(),r.bottomRight());
painter->drawLine(r.topLeft(),r.bottomLeft());

painter->setPen(fontPen);
}



int imageSpace = 10;
if (!ic.isNull()) {
//ICON
r = option.rect.adjusted(5, 10, -10, -10);
ic.paint(painter, r, Qt::AlignVCenter|Qt::AlignLeft);
imageSpace = 55;
}

//TITLE
r = option.rect.adjusted(imageSpace, 0, -10, -30);
painter->setFont( QFont( "Lucida Grande", 10, QFont::Bold ) );
painter->drawText(r.left(), r.top(), r.width(), r.height(), Qt::AlignBottom|Qt::AlignLeft, title, &r);

//DESCRIPTION
r = option.rect.adjusted(imageSpace, 30, -10, 0);
painter->setFont( QFont( "Lucida Grande", 7, QFont::Light) );
painter->drawText(r.left(), r.top(), r.width(), r.height(), Qt::AlignLeft, description, &r);

r = option.rect.adjusted(imageSpace, 40, -10, -20);
painter->setFont( QFont( "Lucida Grande", 5, QFont::Light ) );
painter->drawText(r.left(), r.top(), r.width(), r.height(), Qt::AlignLeft, dateAndTime, &r);

}

sivambigai
4th October 2010, 07:28
Hai i have added the size details also


QSize CSmsThreadListWidgetItem::sizeHint ( const QStyleOptionViewItem & option, const QModelIndex & index ) const{
return QSize(200, 100); // very dumb value
}


....
....
item = new QListWidgetItem();
item->setData(Qt::DisplayRole, title);
item->setData(Qt::UserRole, sms->getMessage());
item->setData(Qt::UserRole +1, sms->getDateAndTime());
item->setData(Qt::DecorationRole, QPixmap(fileimage_));
this->listWidget->addItem(item);

Advance Thanks

Lykurg
4th October 2010, 07:33
QListWidgetItem it;
it.setData(Qt::UserRole, 1);
it.setData(Qt::UserRole+1, 2);
qWarning() << it.data(Qt::UserRole);
qWarning() << it.data(Qt::UserRole + 1);works fine, so please go ahead and try to debug your code yourself.
And a minimal example would be better. Also the use of [CODE] could make your code more readable.

sivambigai
6th October 2010, 16:11
Hai,
Thanks For your reply :) while debugging the values are printing.i think i making mistake in placing the QT::UserRole+1
r = option.rect.adjusted(imageSpace, 40, -10, -20);

can u please guide me in what radio we have to adjust

Advance Thanks .