Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QListWidget delegate paint problem.
QtWS25 Last Chance

QListWidget delegate paint problem.

Scheduled Pinned Locked Moved General and Desktop
5 Posts 3 Posters 8.2k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • R Offline
    R Offline
    rule
    wrote on last edited by
    #1

    Hi all,
    A have just made cistom item delegate, and found some problem on implementing cross-platform paint and sizeHint class methods.

    I have Big text in my cell, and I want to adjust Height regarding it," here is the code of delegate. "Your text to link here...(code)":http://pastebin.com/dNg1MeNV

    As you can see, the sizeHint Method:

    @QSize RaspListDeleagte::sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const
    {
    if (!index.isValid()) return QStyledItemDelegate::sizeHint(option,index);

    QSize result;
    //setup fonts
    QFontMetrics fmTF(getTimeFont(option));
    result.setHeight(fmTF.lineSpacing()+fmTF.leading()*2+4);
    result.setWidth(option.rect.width());
    return result;
    

    }
    @
    And I draw the text by following code:
    @ painter->setFont(timeFont);
    painter->drawText(option.rect.x()+2,option.rect.y()+fmTF.height(),timeLeft);
    @
    I got option.rect.y() plus QFontMetrics height.
    It worked good on Mac OS, but it looks ugly on Linux:
    !http://erudenko.com/photo/temp/lrasp.png(pict)!

    As you can see, the big font text with tome shifted (I got shifted option.rect.y()). It mean that every cell of QListWidget has some top marging, ot content margin. But I didn't find any in QStyleOptionViewItem.

    How I can get this margin from Item Delegate ?

    1 Reply Last reply
    0
    • D Offline
      D Offline
      DenisKormalev
      wrote on last edited by
      #2

      As a quick answer I can advice you to add some style sheet to your instance of QListWidget with removed margines. I used such way once, but I was in opposite situation - margins were needed in some places.

      1 Reply Last reply
      0
      • R Offline
        R Offline
        rule
        wrote on last edited by
        #3

        Regarding this "doc(Box Model)":http://doc.trolltech.com/4.6/stylesheet-customizing.html: I need "Padding" property. But I did not find any direct class methods.

        I've just checked, And passed such string in initialization of QWidgetList:
        @
        ui->listView->setStyleSheet(" QListWidget { padding: 40px;}");
        @
        Unfortunately, with no results.

        1 Reply Last reply
        0
        • A Offline
          A Offline
          andre
          wrote on last edited by
          #4

          I have found that custom QItemDelegates can get trickyer than you'd like sometimes...

          Perhaps you could work around your issue by not using a delegate, but by using a proxy model. In that model, you could use the item roles for the font, alignment and color to achieve the look you want, and have Qt's code deal with the actual layout.

          Note that this only works if what we are seeing above are separate columns.

          1 Reply Last reply
          0
          • R Offline
            R Offline
            rule
            wrote on last edited by
            #5

            I'm already made workaround, by using overloading method of QPainter, to Draw in QRect, instead of x,y.
            I have left the sizeHint() in original state, and reimplement paint().
            The code now is:
            @
            painter->drawText(option.rect.adjusted(2,0,fmTF.width(timeLeft),0),timeLeft);
            @

            1 Reply Last reply
            0

            • Login

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • Users
            • Groups
            • Search
            • Get Qt Extensions
            • Unsolved