Problem with editing QTableView

Python und das Qt-Toolkit, erstellen von GUIs mittels des Qt-Designers.
Antworten
TimoSan
User
Beiträge: 3
Registriert: Freitag 3. November 2006, 14:44

Hi all,

First of all: sorry for posting in English... if you have problems with the language, just ignore this post and it will be my own disadvantage ;)

I have a probably quite stupid question concerning QTableView:
I constructed a tableview and filled it with values. Unfortunately, as soon as I'm doubleclicking on some of the cells (1 double click still works, more double clicks don't), I get a segmentation fault and my gui crashes.
When I'm omitting the "setViewport(QWidget)"-part, I don't have the problem anymore. But the viewport is not set anymore as well ;)

So I'd be glad to either see, what the problem is or get an alternative to setViewport.
I tried to cut down my code a little to make it more clearly.

self._parent.addTab(self.balanceViewSBtab,'SBtab-file')
menu = QtGui.QWidget(self.balanceViewSBtab)
menu.setAutoFillBackground(1)

self.BalanceViewSBtabLayout = QtGui.QGridLayout(menu)

nameLabel = QtGui.QLabel(self.balanceViewSBtab)
nameLabel.setText(''+str(self.fileName)+'')
self.BalanceViewSBtabLayout.addWidget(nameLabel,0,0)

self.tableView = QtGui.QTableView(self.balanceViewSBtab)
self.tableViewWidget = QtGui.QTableWidget(self.tableView)

self.tableViewWidget.setRowCount(len(self.rows)-1)
self.tableViewWidget.setColumnCount(len(self.columnNames))
self.tableViewWidget.setHorizontalHeaderLabels(self.columnNames)

self.tableViewWidget.resizeColumnsToContents()

#setting the rowheight of the header
self.tableViewWidget.setRowHeight(0,20)

#putting the values into the table
self.rowcounter = 0
for rowcount,row in enumerate(self.rows):
values = row.split('t')
for columncount,value in enumerate(values):
single = QtGui.QTableWidgetItem(value)
self.tableViewWidget.setItem(rowcount,columncount,single)
self.tableViewWidget.setRowHeight(rowcount,20)
self.rowcounter += 1

self.tableView.setViewport(self.tableViewWidget)
self.tableView.setEnabled(1)
self.tableView.show()

I know, it is not the whole code and some variables are not explained. But I'm quite positive, that the mistake lies somewhere around here and especially in the use of QTableView and setViewPort. Probably something has to be set enabled or something like that. Sorry, I'm getting started to PyQt and sending the whole code would be incredibly hard work. Moreover I hope someone knows that problem and can solve it "blindly"... ;)

I'm looking forward to any hints...

Thanks!
franzf
User
Beiträge: 78
Registriert: Samstag 29. August 2009, 10:21

First of all, use "code=py"-Tags, when posting Pythopn-code ;)

Then the problem:
There is a complete misunderstanding of the Usage of QTableView and QTableWidget!

QTableView is Model-Based. That means you create a model and tell the TableView, to use this.

Code: Alles auswählen

view = QTableView()
model = QDirModel()
view.setModel(model)
QTableWidget is Item-Based. That means, you create QTableWidgetItems and append them in your TableWidget. The Usage is exactly the way you do it right now.

You have to decide wether to use TableWidget or TableView.
If you want to keep your Item-Based Setup of the data, just leave the QTableView out of your Code!
This will look like this:

Code: Alles auswählen

self.tableViewWidget = QtGui.QTableWidget(self) 

self.tableViewWidget.setRowCount(len(self.rows)-1) 
self.tableViewWidget.setColumnCount(len(self.columnNames)) 
self.tableViewWidget.setHorizontalHeaderLabels(self.columnNames) 

# your Code for setting up the widget

self.tableViewWidget.setItem(rowcount,columncount,single) 
self.tableViewWidget.setRowHeight(rowcount,20) 
self.rowcounter += 1

self.tableViewWidget.show()
And be told there is another Constructor for QTableWidget, taking num of rows and cols ;)

Code: Alles auswählen

self.tableViewWidget = QtGui.QTableWidget(len(self.rows)-1, len(self.columnNames), self)
And why do you take len(self.rows)-1? I think you want to be able to add all rows!
Antworten