Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Qt for Python
  4. QPainter drawline doesnt show correctly after scrolling back and forth horizontally with QTableView
Qt 6.11 is out! See what's new in the release blog

QPainter drawline doesnt show correctly after scrolling back and forth horizontally with QTableView

Scheduled Pinned Locked Moved Unsolved Qt for Python
1 Posts 1 Posters 312 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.
  • J Offline
    J Offline
    JohnLim02
    wrote on last edited by
    #1

    Hello, I am new to using PYQT5,
    I am trying to create a 2D graphical polygon image using QPainter and Integrate inside QTableView using the StyledItemDeligate function. I manage to show what I want, However as u can see the arrow should only appears on the first 2 column but when I scroll next and went back again, it shows 4 arrow for 4 column instead of 2 arrow in 2 column. Below is my code. I am not sure if there is some sort of rendering issue I would need to consider when playing with scroll bar.

    from PyQt5 import QtCore, QtWidgets, QtGui
    import sys
    
    class TableModel(QtCore.QAbstractTableModel):
    
        def __init__(self, data = [[]], headers = None, parent = None):
            QtCore.QAbstractTableModel.__init__(self, parent)
            self.__data = data
    
        def rowCount(self, parent):
            return len(self.__data)
    
        def columnCount(self, parent):
            return len(self.__data[0])
    
        def data(self, index, role):
            row = index.row()
            column = index.column()
            if role == QtCore.Qt.DisplayRole:
                value = self.__data[row][column]
                return value
    
        def flags(self, index):
            return QtCore.Qt.ItemIsEnabled|QtCore.Qt.ItemIsEditable|QtCore.Qt.ItemIsSelectable
    
    class Delegate(QtWidgets.QStyledItemDelegate):
    
        def paint(self, painter, option, index):  
    
          
                # image = QtGui.QImage('open.png')
                # pixmap = QtGui.QPixmap.fromImage(image)
    
                # x = option.rect.center().x() - pixmap.rect().width() / 2
                # y = option.rect.center().y() - pixmap.rect().height() / 2
                # painter.drawPixmap(x, y, pixmap)
    
            painter.drawLine(0, 17, 124, 17)
            painter.drawLine(0, 17, 20, 35)
            painter.drawLine(0, 17, 20, 1)
            painter.drawLine(124, 17, 248, 17)
            painter.drawLine(124, 17, 150, 35)
            painter.drawLine(124, 17, 150, 1)
            painter.drawLine(248, 17, 372, 17)
            painter.drawLine(372, 17, 496, 17)
            painter.drawLine(496, 17, 620, 17)
            painter.drawLine(620, 17, 744, 17)
            
           
    
    if __name__ == '__main__':
    
        app = QtWidgets.QApplication(sys.argv)
        app.setStyle('fusion')
    
        tableView = QtWidgets.QTableView()
        tableView.setShowGrid(False)
        header = tableView.horizontalHeader()
        header. hide()
        
        # delegate1 = Delegate()
        # delegate2 = Delegate()
        dict = {}
        for i in range(1, 9):
            key = str("delegate" + str(i))
            dict[key] = Delegate()
    
        # tableView.setItemDelegateForColumn(0, Delegate())
        # tableView.setItemDelegateForColumn(1, Delegate())
        array1 = [Delegate(), Delegate(), Delegate(), Delegate(), Delegate(), Delegate(),Delegate(),Delegate()]
        for i in range(1, 9):
            tableView.setItemDelegateForColumn(i-1, array1[i -1])
        
        tableView.resize(550, 160)
        tableView.show()
    
        rowCount = 1
        columnCount = 40
        data = [
           [1,2,3,4,5,6,7,8]
        ]
    
        print(data)
    
        model = TableModel(data)
        tableView.setModel(model)
        tableView.setEditTriggers(QtWidgets.QTableWidget.NoEditTriggers)
    
        sys.exit(app.exec_())
    

    Capture1.PNG
    capture2.png

    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