Results 1 to 7 of 7

Thread: listview with custom delegate moves text bit out of place [pic and code example]

  1. #1
    Join Date
    Apr 2015
    Posts
    20
    Thanks
    7
    Qt products
    Platforms
    Unix/X11

    Default listview with custom delegate moves text bit out of place [pic and code example]



    I first asked on stackoverflow - link

    But no responses, few solutions I found out seems to be band aids that get ripped when I try to do more stuff with it.
    I add icons and suddenly I have issues across various desktop environments. And moving text/items up and down by pixes makes it out of place in different DE...
    All the while everything is nice, pretty and perfect when custom delegate for html tags is not in use.

    heres the test code seen in the gif, theres bit more story on stackoverflow, with also pyqt5 and pyside versions.
    Comment out the 39th line to use the default delegate.

    Qt Code:
    1. from PyQt4.QtCore import *
    2. from PyQt4.QtGui import *
    3. import sys
    4.  
    5. class HTMLDelegate(QStyledItemDelegate):
    6. def __init__(self, parent=None):
    7. super(HTMLDelegate, self).__init__(parent)
    8. self.doc = QTextDocument(self)
    9.  
    10. def paint(self, painter, option, index):
    11. painter.save()
    12.  
    13. options = QStyleOptionViewItemV4(option)
    14. self.initStyleOption(options, index)
    15.  
    16. self.doc.setHtml(options.text)
    17. options.text = ""
    18.  
    19. style = QApplication.style() if options.widget is None \
    20. else options.widget.style()
    21. style.drawControl(QStyle.CE_ItemViewItem, options, painter)
    22.  
    23. ctx = QAbstractTextDocumentLayout.PaintContext()
    24.  
    25. if option.state & QStyle.State_Selected:
    26. ctx.palette.setColor(QPalette.Text, option.palette.color(
    27. QPalette.Active, QPalette.HighlightedText))
    28.  
    29. textRect = style.subElementRect(QStyle.SE_ItemViewItemText, options)
    30. painter.translate(textRect.topLeft())
    31. self.doc.documentLayout().draw(painter, ctx)
    32.  
    33. painter.restore()
    34.  
    35. if __name__ == '__main__':
    36. app = QApplication(sys.argv)
    37. data = ['1','2','3','4','5','6','7','8','9']
    38. main_list = QListView()
    39. main_list.setItemDelegate(HTMLDelegate())
    40. main_list.setModel(QStringListModel(data))
    41. main_list.show()
    42. sys.exit(app.exec_())
    To copy to clipboard, switch view to plain text mode 


    One thing that might help me get to the point of this, is if I could find the code of the default delegate?
    See how it is done, try some stuff... assuming it is somewhere declared in the similar format as how we are suppose to do the custom ones... but as a noobie I am having a hard time finding it

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: listview with custom delegate moves text bit out of place [pic and code example]

    You need to reimplement sizeHint() for the delegate to compensate for a different size of the drawn text.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  3. #3
    Join Date
    Apr 2015
    Posts
    20
    Thanks
    7
    Qt products
    Platforms
    Unix/X11

    Default Re: listview with custom delegate moves text bit out of place [pic and code example]

    Thanks, I used this as size hint, in the code above where I have self.doc defined in the init

    Qt Code:
    1. def sizeHint(self, option, index):
    2. return QSize(self.doc.idealWidth(), self.doc.size().height())
    To copy to clipboard, switch view to plain text mode 

    it fixes the issue in all DEs I tried (i3, ubuntu, gnome, xfce, cinnamon) except KDE, pic shows in KDE



    but as you said, it is solving this by enlarges the height of the row



    It makes me feel like I am losing control, I have to adjust row height for text to be centered correctly instead of the text adjusting itself to the row?

    I also tried to play a bit with statically setting return values of sizeHint, but since different DEs expect different height ranging from 22-28, it seems I cant just set stuff the same everywhere as I want it.

    complete test-case code with sizehint

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: listview with custom delegate moves text bit out of place [pic and code example]

    Quote Originally Posted by DoTheEvo View Post
    It makes me feel like I am losing control, I have to adjust row height for text to be centered correctly instead of the text adjusting itself to the row?
    You are not telling the text to adjust itself to anything.

    I also tried to play a bit with statically setting return values of sizeHint, but since different DEs expect different height ranging from 22-28, it seems I cant just set stuff the same everywhere as I want it.
    Would look really silly with a default DE font size set to 40pt or so. You can use QFontMetrics though.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  5. #5
    Join Date
    Apr 2015
    Posts
    20
    Thanks
    7
    Qt products
    Platforms
    Unix/X11

    Default Re: listview with custom delegate moves text bit out of place [pic and code example]

    Quote Originally Posted by wysota View Post
    You are not telling the text to adjust itself to anything.
    Nope, but I would love to, assuming its relative positioning and not absolute with textRect.adjust(-1, -4, 0, 0)

    Is there any way to fix KDE? Or to lower the items height without text going all crazy?

  6. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: listview with custom delegate moves text bit out of place [pic and code example]

    If you have a font size which takes n pixels to draw a single line, there is no way you are going to fit it into < n pixels. Thus assuming you have a row of n pixels, you need to find a font which will fit into that.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  7. #7
    Join Date
    Apr 2015
    Posts
    20
    Thanks
    7
    Qt products
    Platforms
    Unix/X11

    Default Re: listview with custom delegate moves text bit out of place [pic and code example]

    thanks, will digg some more I guess

Similar Threads

  1. Replies: 5
    Last Post: 6th April 2015, 09:14
  2. Replies: 1
    Last Post: 23rd June 2012, 13:23
  3. example code of table view with a custom delegate?
    By Dreamerzz in forum Qt Programming
    Replies: 1
    Last Post: 22nd June 2011, 16:33
  4. Custom Delegate and Text Wrap
    By enlightened_j in forum Newbie
    Replies: 2
    Last Post: 17th September 2010, 06:21
  5. Custom Model? Custom View? Custom Delegate?
    By Doug Broadwell in forum Newbie
    Replies: 4
    Last Post: 11th February 2010, 20:23

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.