Python Forum
PyQt5 QTableWidget duplicates rows number
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
PyQt5 QTableWidget duplicates rows number
#1
Hello, guys...

Id like to know why my table shows double number of rows. First part with data and the second in blank.
The registers come from my database.
def setupUi(self, pagMensalid,matr):
      dadosocio = ["",""] #Acrescent
      dadosocio = matr.split(",") #Acrescent
      pagMensalid.setObjectName("pagMensalid")
      pagMensalid.setWindowModality(QtCore.Qt.ApplicationModal)
      pagMensalid.resize(500, 500)
      pagMensalid.setMinimumSize(QtCore.QSize(500, 500))
      pagMensalid.setMaximumSize(QtCore.QSize(500, 500))
      self.centralwidget = QtWidgets.QWidget(pagMensalid)
      self.centralwidget.setObjectName("centralwidget")
      self.label = QtWidgets.QLabel(self.centralwidget)
      self.label.setGeometry(QtCore.QRect(100, 10, 341, 41))
      font = QtGui.QFont()
      font.setFamily("Arial")
      font.setPointSize(16)
      font.setBold(True)
      font.setWeight(75)
      self.label.setFont(font)
      self.label.setObjectName("label")
      self.label_2 = QtWidgets.QLabel(self.centralwidget)
      self.label_2.setGeometry(QtCore.QRect(40, 70, 81, 31))
      font = QtGui.QFont()
      font.setFamily("Arial")
      font.setPointSize(14)
      self.label_2.setFont(font)
      self.label_2.setObjectName("label_2")
      self.label_3 = QtWidgets.QLabel(self.centralwidget)
      self.label_3.setGeometry(QtCore.QRect(40, 120, 71, 31))
      font = QtGui.QFont()
      font.setFamily("Arial")
      font.setPointSize(14)
      self.label_3.setFont(font)
      self.label_3.setObjectName("label_3")
      self.txtmatr = QtWidgets.QTextEdit(self.centralwidget)
      self.txtmatr.setGeometry(QtCore.QRect(130, 70, 104, 31))
      font = QtGui.QFont()
      font.setFamily("Arial")
      font.setPointSize(12)
      self.txtmatr.setFont(font)
      self.txtmatr.setObjectName("txtmatr")
      self.txtnome = QtWidgets.QTextEdit(self.centralwidget)
      self.txtnome.setGeometry(QtCore.QRect(110, 120, 311, 31))
      font = QtGui.QFont()
      font.setFamily("Arial")
      font.setPointSize(12)
      self.txtnome.setFont(font)
      self.txtnome.setObjectName("txtnome")
      self.label_4 = QtWidgets.QLabel(self.centralwidget)
      self.label_4.setGeometry(QtCore.QRect(40, 180, 211, 16))
      font = QtGui.QFont()
      font.setFamily("Arial")
      font.setPointSize(14)
      font.setBold(True)
      font.setWeight(75)
      self.label_4.setFont(font)
      self.label_4.setObjectName("label_4")
      self.tblparc = QtWidgets.QTableWidget(self.centralwidget)
      self.tblparc.setGeometry(QtCore.QRect(40, 210, 441, 192))
      self.tblparc.setStyleSheet("border-color: rgb(0, 0, 0);")
      self.tblparc.setObjectName("tblparc")
      self.tblparc.setColumnCount(3)
      self.tblparc.setRowCount(0)
      item = QtWidgets.QTableWidgetItem()
      font = QtGui.QFont()
      font.setFamily("Arial")
      font.setPointSize(12)
      font.setBold(True)
      font.setWeight(75)
      item.setFont(font)
      item.setBackground(QtGui.QColor(255, 255, 0))
      self.tblparc.setHorizontalHeaderItem(0, item)
      item = QtWidgets.QTableWidgetItem()
      font = QtGui.QFont()
      font.setFamily("Arial")
      font.setPointSize(12)
      font.setBold(True)
      font.setWeight(75)
      item.setFont(font)
      item.setBackground(QtGui.QColor(255, 255, 0))
      self.tblparc.setHorizontalHeaderItem(1, item)
      item = QtWidgets.QTableWidgetItem()
      font = QtGui.QFont()
      font.setFamily("Arial")
      font.setPointSize(12)
      font.setBold(True)
      font.setWeight(75)
      item.setFont(font)
      item.setBackground(QtGui.QColor(255, 255, 0))
      self.tblparc.setHorizontalHeaderItem(2, item)
      self.label_5 = QtWidgets.QLabel(self.centralwidget)
      self.label_5.setGeometry(QtCore.QRect(40, 440, 500, 600))
      self.label_5.setMinimumSize(QtCore.QSize(500, 600))
      self.label_5.setMaximumSize(QtCore.QSize(500, 600))
      font = QtGui.QFont()
      font.setFamily("Arial")
      font.setPointSize(14)
      font.setBold(False)
      font.setWeight(50)
      self.label_5.setFont(font)
      self.label_5.setObjectName("label_5")
      self.label_6 = QtWidgets.QLabel(self.centralwidget)
      self.label_6.setGeometry(QtCore.QRect(40, 420, 81, 20))
      font = QtGui.QFont()
      font.setFamily("Arial")
      font.setPointSize(14)
      font.setBold(True)
      font.setWeight(75)
      self.label_6.setFont(font)
      self.label_6.setObjectName("label_6")
      self.txttotal = QtWidgets.QTextEdit(self.centralwidget)
      self.txttotal.setGeometry(QtCore.QRect(120, 410, 104, 31))
      font = QtGui.QFont()
      font.setFamily("Arial")
      font.setPointSize(12)
      font.setBold(True)
      font.setWeight(75)
      self.txttotal.setFont(font)
      self.txttotal.setObjectName("txttotal")
      self.btnsair = QtWidgets.QPushButton(self.centralwidget)
      self.btnsair.setGeometry(QtCore.QRect(250, 460, 75, 23))
      font = QtGui.QFont()
      font.setFamily("Arial")
      font.setPointSize(14)
      font.setBold(True)
      font.setWeight(75)
      self.btnsair.setFont(font)
      self.btnsair.setStyleSheet("background-color: rgb(170, 170, 255);")
      self.btnsair.setObjectName("btnsair")
      self.btnquitar = QtWidgets.QPushButton(self.centralwidget)
      self.btnquitar.setGeometry(QtCore.QRect(150, 460, 75, 23))
      font = QtGui.QFont()
      font.setFamily("Arial")
      font.setPointSize(14)
      font.setBold(True)
      font.setWeight(75)
      self.btnquitar.setFont(font)
      self.btnquitar.setStyleSheet("background-color: rgb(170, 170, 255);")
      self.btnquitar.setObjectName("btnquitar")
      pagMensalid.setCentralWidget(self.centralwidget)
      self.statusbar = QtWidgets.QStatusBar(pagMensalid)
      self.statusbar.setObjectName("statusbar")
      pagMensalid.setStatusBar(self.statusbar)
      self.txtmatr.setReadOnly(True) 
      self.txtnome.setReadOnly(True)
      self.txttotal.setReadOnly(True)

      self.btnquitar.clicked.connect(lambda: self.consultarParcelas(dadosocio[0]))
      self.txtmatr.setText(dadosocio[0])
      self.txtnome.setText(dadosocio[1])
      self.bdAeje = sqlite3.connect('bdAeje.db')
      cursor = self.bdAeje.cursor()
      print("SELECT codpag,ref,valor FROM PAGAMENTO WHERE matr = '"+str(dadosocio[0])+"' AND quitado='Nao'")
      cursor.execute("SELECT codpag,ref,valor FROM PAGAMENTO WHERE matr='"+str(dadosocio[0])+"' AND quitado='Nao'")
      registros = cursor.fetchall() 
      print("Total registros"+str(len(registros)))
      self.tblparc.clearContents()
      totlinhas =  self.tblparc.setRowCount(len(registros))
      self.bdAeje.close
      vltotal = 0
      contador = 0

      for row in range(len(registros)): #len(registros)
        campos = registros.__getitem__(row)
        self.tblparc.insertRow(row) #rowPosition
        self.tblparc.setItem(row, 0, QtWidgets.QTableWidgetItem(str(campos[1])))#rowPosition
        QtWidgets.QTableWidgetItem(str(campos[1])).setTextAlignment(4)
        self.tblparc.setItem(row, 1, QtWidgets.QTableWidgetItem(str(campos[2])))
        QtWidgets.QTableWidgetItem(str(campos[2])).setTextAlignment(4) 
        checkbox_item = QTableWidgetItem()
        checkbox_item.setFlags(checkbox_item.flags() | Qt.ItemIsUserCheckable)  # Permitir que o item seja um checkbox
        checkbox_item.setCheckState(Qt.Unchecked) #Qt.Unchecked
        self.tblparc.setItem(row,2,checkbox_item)
        vltotal = vltotal + campos[2]
        contador = contador + 1
        
      self.txttotal.setText("R$ "+str(vltotal)+",00")
      self.retranslateUi(pagMensalid)
      QtCore.QMetaObject.connectSlotsByName(pagMensalid)
Reply
#2
Is it better if you comment on the lines with "setTextAlignment"?

Better make the alignment in a function

    def setTableAlignment(self):
        for x in range(self.tblparc.rowCount()):
            self.tblparc.item(x, 0).setTextAlignment(Qt.AlignLeft | Qt.AlignVCenter)
            self.tblparc.item(x, 1).setTextAlignment(Qt.AlignRight | Qt.AlignVCenter)
            self.tblparc.item(x, 2).setTextAlignment(Qt.AlignRight | Qt.AlignVCenter)
Reply
#3
Thanks for you reply...but does it really has relashionship with the renderization of the number of the lines ?
Reply
#4
Did you try it?
Reply
#5
Yes...but got the same result relationed with alignment.
After sometime, suddenly the code started to work well. Now the table started to renderize without empty lines.
I use VS Code. Could it be due the cache ?
Reply
#6
If you write code to a file you need to save the file before running. There may be ways to run a file in VSCode without saving. How do you run your code? I've never used a designer, so there could be an issue with that.
Reply
#7
I always save my code after edition. Set my 'main' file tab, and on the right top corner 'Run Python File'..
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  [PyQt] QTableWidget print problem JokerSob 8 6,705 Jan-28-2022, 06:08 PM
Last Post: Axel_Erfurt
  [PyQt] How do I display the DB table I will choose from the QComboBox in QTableWidget JokerSob 2 3,092 Aug-05-2021, 03:00 PM
Last Post: JokerSob
  Displaying database info in QTableWidget thewolf 6 8,001 Apr-03-2021, 02:49 PM
Last Post: thewolf
  [PyQt] Help: check content of combobox in horizontal header of QTableWidget mart79 1 4,452 Jul-26-2020, 06:18 PM
Last Post: deanhystad
  [PyQt] Add validation (regex) to QTableWidget cells mart79 0 3,520 May-05-2020, 12:51 PM
Last Post: mart79
  PyQt5: Iterate through rows to check for a value keegan_010 2 2,590 Mar-23-2020, 11:30 PM
Last Post: keegan_010
  [PyQt] QTableWidget stylesheet error mart79 3 8,616 Feb-26-2020, 04:54 PM
Last Post: Denni
  [PyQt] Pyqt5: How do you make a button that adds new row with data to a Qtablewidget YoshikageKira 6 9,217 Jan-02-2020, 04:32 PM
Last Post: Denni
  Huge code problems (buttons(PyQt5),PyQt5 Threads, Windows etc) ZenWoR 0 3,455 Apr-06-2019, 11:15 PM
Last Post: ZenWoR
  [PyQt] QTableWidget cell validation littleGreenDude 1 9,052 Jan-18-2019, 07:44 PM
Last Post: littleGreenDude

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020