Skip to main content
added 6 characters in body
Source Link
Ben W
  • 23.1k
  • 3
  • 19
  • 43

Try changing the following 2 lines, passing count as the column index instead of count+1: E.g.

self.dlg.tableWidget.setHorizontalHeaderItem(count,header)
self.dlg.lineEdit_4.setText(self.dlg.tableWidget.horizontalHeaderItem(count).text())

Because when you get your column count, if your table has, for example 3 columns, the index of the last column would be 2 (since indexing begins at 0). After you add another column, the index of the new column will be 3, which is your original count value.

You can see that in this minimal example:

tw = QTableWidget(3, 3)
items = [['r1c1', 'r1c2', 'r1c3'],
         ['r2c1', 'r2c2', 'r2c3'],
         ['r3c1', 'r3c2', 'r3c3']]
         
row = 0
for item in items:
    col = 0
    for cell in item:
        tw.setItem(row, col, QTableWidgetItem(items[row][col]))
        col += 1
    row+=1

for i in range(tw.columnCount()):
    tw.setHorizontalHeaderItem(i, QTableWidgetItem(f'Column {i+1}'))

count = tw.columnCount()
tw.setColumnCount(count+1)
header= QTableWidgetItem(f'Column {count+1}')
tw.setHorizontalHeaderItem(count, header)
    
tw.show()

print(count)
print(tw.horizontalHeaderItem(count).text())

When run in the QGIS Python console, the result is:

enter image description here

Try changing the following 2 lines, passing count as the column index instead of count+1:

self.dlg.tableWidget.setHorizontalHeaderItem(count,header)
self.dlg.lineEdit_4.setText(self.dlg.tableWidget.horizontalHeaderItem(count).text())

Because when you get your column count, if your table has, for example 3 columns, the index of the last column would be 2 (since indexing begins at 0). After you add another column, the index of the new column will be 3, which is your original count value.

You can see that in this minimal example:

tw = QTableWidget(3, 3)
items = [['r1c1', 'r1c2', 'r1c3'],
         ['r2c1', 'r2c2', 'r2c3'],
         ['r3c1', 'r3c2', 'r3c3']]
         
row = 0
for item in items:
    col = 0
    for cell in item:
        tw.setItem(row, col, QTableWidgetItem(items[row][col]))
        col += 1
    row+=1

for i in range(tw.columnCount()):
    tw.setHorizontalHeaderItem(i, QTableWidgetItem(f'Column {i+1}'))

count = tw.columnCount()
tw.setColumnCount(count+1)
header= QTableWidgetItem(f'Column {count+1}')
tw.setHorizontalHeaderItem(count, header)
    
tw.show()

print(count)
print(tw.horizontalHeaderItem(count).text())

When run in the QGIS Python console, the result is:

enter image description here

Try changing the following 2 lines, passing count as the column index instead of count+1: E.g.

self.dlg.tableWidget.setHorizontalHeaderItem(count,header)
self.dlg.lineEdit_4.setText(self.dlg.tableWidget.horizontalHeaderItem(count).text())

Because when you get your column count, if your table has, for example 3 columns, the index of the last column would be 2 (since indexing begins at 0). After you add another column, the index of the new column will be 3, which is your original count value.

You can see that in this minimal example:

tw = QTableWidget(3, 3)
items = [['r1c1', 'r1c2', 'r1c3'],
         ['r2c1', 'r2c2', 'r2c3'],
         ['r3c1', 'r3c2', 'r3c3']]
         
row = 0
for item in items:
    col = 0
    for cell in item:
        tw.setItem(row, col, QTableWidgetItem(items[row][col]))
        col += 1
    row+=1

for i in range(tw.columnCount()):
    tw.setHorizontalHeaderItem(i, QTableWidgetItem(f'Column {i+1}'))

count = tw.columnCount()
tw.setColumnCount(count+1)
header= QTableWidgetItem(f'Column {count+1}')
tw.setHorizontalHeaderItem(count, header)
    
tw.show()

print(count)
print(tw.horizontalHeaderItem(count).text())

When run in the QGIS Python console, the result is:

enter image description here

Source Link
Ben W
  • 23.1k
  • 3
  • 19
  • 43

Try changing the following 2 lines, passing count as the column index instead of count+1:

self.dlg.tableWidget.setHorizontalHeaderItem(count,header)
self.dlg.lineEdit_4.setText(self.dlg.tableWidget.horizontalHeaderItem(count).text())

Because when you get your column count, if your table has, for example 3 columns, the index of the last column would be 2 (since indexing begins at 0). After you add another column, the index of the new column will be 3, which is your original count value.

You can see that in this minimal example:

tw = QTableWidget(3, 3)
items = [['r1c1', 'r1c2', 'r1c3'],
         ['r2c1', 'r2c2', 'r2c3'],
         ['r3c1', 'r3c2', 'r3c3']]
         
row = 0
for item in items:
    col = 0
    for cell in item:
        tw.setItem(row, col, QTableWidgetItem(items[row][col]))
        col += 1
    row+=1

for i in range(tw.columnCount()):
    tw.setHorizontalHeaderItem(i, QTableWidgetItem(f'Column {i+1}'))

count = tw.columnCount()
tw.setColumnCount(count+1)
header= QTableWidgetItem(f'Column {count+1}')
tw.setHorizontalHeaderItem(count, header)
    
tw.show()

print(count)
print(tw.horizontalHeaderItem(count).text())

When run in the QGIS Python console, the result is:

enter image description here