Results 1 to 4 of 4

Thread: Problem in creating more than one Qt::UserRole QListWidgetItem

  1. #1
    Join Date
    Sep 2010
    Location
    Chennai
    Posts
    7
    Thanks
    3
    Qt products
    Qt3 Qt4
    Platforms
    Windows

    Default Problem in creating more than one Qt::UserRole QListWidgetItem

    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:aint ( 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:ecorationRole)));
    QString title = index.data(Qt:isplayRole).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);

    }

  2. #2
    Join Date
    Sep 2010
    Location
    Chennai
    Posts
    7
    Thanks
    3
    Qt products
    Qt3 Qt4
    Platforms
    Windows

    Default Re: Problem in creating more than one Qt::UserRole QListWidgetItem

    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:isplayRole, title);
    item->setData(Qt::UserRole, sms->getMessage());
    item->setData(Qt::UserRole +1, sms->getDateAndTime());
    item->setData(Qt:ecorationRole, QPixmap(fileimage_));
    this->listWidget->addItem(item);

    Advance Thanks

  3. #3
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: Problem in creating more than one Qt::UserRole QListWidgetItem

    Qt Code:
    1. it.setData(Qt::UserRole, 1);
    2. it.setData(Qt::UserRole+1, 2);
    3. qWarning() << it.data(Qt::UserRole);
    4. qWarning() << it.data(Qt::UserRole + 1);
    To copy to clipboard, switch view to plain text mode 
    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.

  4. The following user says thank you to Lykurg for this useful post:

    sivambigai (7th October 2010)

  5. #4
    Join Date
    Sep 2010
    Location
    Chennai
    Posts
    7
    Thanks
    3
    Qt products
    Qt3 Qt4
    Platforms
    Windows

    Default Re: Problem in creating more than one Qt::UserRole QListWidgetItem

    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 .

Similar Threads

  1. Replies: 2
    Last Post: 14th June 2010, 09:40
  2. problem in presentation of text-icon QListWidgetItem
    By utopia500 in forum Qt Programming
    Replies: 4
    Last Post: 16th April 2010, 10:12
  3. Model/View one index.column as hidden UserRole
    By doitux in forum Qt Programming
    Replies: 2
    Last Post: 26th July 2008, 12:08
  4. setting UserRole data in QSqlTableModel
    By orgads in forum Qt Programming
    Replies: 1
    Last Post: 2nd June 2008, 09:40

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.