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.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
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

1
2
3
4
5
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
  QTableWidget - Retrieve content from a cell Ninja2112 7 314 Apr-07-2025, 06:05 PM
Last Post: Ninja2112
  [PyQt] QTableWidget print problem JokerSob 8 6,750 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,106 Aug-05-2021, 03:00 PM
Last Post: JokerSob
  Displaying database info in QTableWidget thewolf 6 8,067 Apr-03-2021, 02:49 PM
Last Post: thewolf
  [PyQt] Help: check content of combobox in horizontal header of QTableWidget mart79 1 4,473 Jul-26-2020, 06:18 PM
Last Post: deanhystad
  [PyQt] Add validation (regex) to QTableWidget cells mart79 0 3,528 May-05-2020, 12:51 PM
Last Post: mart79
  PyQt5: Iterate through rows to check for a value keegan_010 2 2,602 Mar-23-2020, 11:30 PM
Last Post: keegan_010
  [PyQt] QTableWidget stylesheet error mart79 3 8,662 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,274 Jan-02-2020, 04:32 PM
Last Post: Denni
  Huge code problems (buttons(PyQt5),PyQt5 Threads, Windows etc) ZenWoR 0 3,464 Apr-06-2019, 11:15 PM
Last Post: ZenWoR

Forum Jump:

User Panel Messages

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