Important: Please read the Qt Code of Conduct -

QPainter won't draw lines on QTableWidget

  • I have a table which I defined this way.

     def setupUi(self, Dialog):
            x = 450
            y = 1394
            posX = 1005
            Dialog.resize(x, y)
            Dialog.setMinimumSize(QtCore.QSize(x, y))
            Dialog.setMaximumSize(QtCore.QSize(x, y))
            Dialog.setGeometry(QtCore.QRect(posX, 0, x, y))
            Dialog.setStyleSheet("background-color: rgb(172,216,219);") 
            self.tableWidget = QtWidgets.QTableWidget(Dialog)
            self.tableWidget.setGeometry(QtCore.QRect(0, 0, x-1, y-1))

    Everything works fine with the table but then I try to put a line on the table, which later on will be dynamic, nothing shows up. I was able to use this code in a standalone app and it worked but when I try to draw on QTableWidget nothing shows up. Thanks.

            self.painter = QtGui.QPainter(Dialog)
            self.painter.drawLine(40, 1050, 60, 1100)

  • Lifetime Qt Champion


    Where did you put that code ?
    You can't arbitrarily paint on a widget. It has to happens in it's paintEvent method.

  • Just below where i created the table and certainly not in paintEvent method. I see some examples online so will try those out. Thanks.

Log in to reply