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. pyqt delegate text wrapping sizeHint and paint
Forum Updated to NodeBB v4.3 + New Features

pyqt delegate text wrapping sizeHint and paint

Scheduled Pinned Locked Moved General and Desktop
delegatesizehintpaint
1 Posts 1 Posters 1.7k Views 1 Watching
  • 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.
  • L Offline
    L Offline
    lukeQt
    wrote on 1 Aug 2015, 22:12 last edited by lukeQt 8 Jan 2015, 22:13
    #1

    Hi Everyone,

    I have a sqlrelationaltablemodel and a subclassed delegate how do you get text wrapping to work? Can you please explain the sizeHint and the paint method?

    This is what I have so far, but no matter how much text is in the cell I only see two lines. Why is that? What am I missing?

    def sizeHint(self, option, index):
        """
        size hint is being used to control text wrap of the definition.
        All columns are allowed to text wrap, but only definition is
        believed to need text wrapping.
        """
        # set the sizeHint for all columns.
        if  index.isValid():
            fm = option.fontMetrics
            text = index.model().data(index)
            document = QtGui.QTextDocument()
            document.setDefaultFont(option.font)
            document.setPlainText(text.toString())
    
            # change cell Width, height (One can add or subtract to change
            # the relative dimension).
            #return QtCore.QSize(QtSql.QSqlRelationalDelegate.sizeHint(self, option, index).width() + 200,
            # QtSql.QSqlRelationalDelegate.sizeHint(self, option, index).height() +200)
    
            return QtCore.QSize(document.idealWidth(), QtSql.QSqlRelationalDelegate.sizeHint(self, option, index).height())
    
        # Else the index is not valid and then return the base method.
        else:
            return super(ObjDelegate, self).sizeHint(option, index)
    
    
    def paint(self, painter, option, index):
        """
        This will allow for text wrapping. The paint method will paint the text.
        """
        if index.isValid():
            # This will highlight the selected cell.
            if option.state & QtGui.QStyle.State_Selected:
                painter.fillRect(option.rect, option.palette.highlight())
    
            # Get the text from the model.
            text = index.model().data(index)
    
            # Initialize the Qtextdocument.
            document = QtGui.QTextDocument()
            document.setPlainText(text.toString())
            document.setDefaultFont(option.font)
    
            # settextwidth.
            document.setTextWidth(option.rect.width())
    
            ctx = QtGui.QAbstractTextDocumentLayout.PaintContext()
    
            # Save the painter. Saves the current painter state.
            # This is so that we can redraw it later.
            painter.save()
            painter.translate(option.rect.topLeft())
            document.documentLayout().draw(painter, ctx)
            painter.restore()
    
        else:
            return super(ObjDelegate, self).paint(painter, option, index)
    
    1 Reply Last reply
    0

    1/1

    1 Aug 2015, 22:12

    • Login

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