JLYK
JLYK

Reputation: 391

PyQt using QLineEdit as cellWidget in a QTableWidget

So I created a QTableWidget which is filled with QComboBoxes and QLineEdits. My QComboBoxes work perfectly fine, but my QLineEdits for whatever reason don't display the text properly.

self.table = QtGui.QTableWidget()  
tableItem = QtGui.QLineEdit(  )
tableItem.setText( "Testing" )
self.table.setCellWidget(row, 1, tableItem )

Now I tried testing it to see if the value resides in the table...and sure enough if I use tableItem.text() I get "Testing" back. I'm just not sure why the lineEdit displays nothing when I run the UI.

Thanks!

Upvotes: 0

Views: 5762

Answers (1)

Junuxx
Junuxx

Reputation: 14251

Here's a simple example, with the QLineEdit working just fine.

from PyQt4 import QtCore, QtGui
import sys

app = QtGui.QApplication([])
table = QtGui.QTableWidget(2,2)

tableItem = QtGui.QLineEdit()
tableItem.setText( "Testing" )
table.setCellWidget(0, 0, tableItem )

comboBox = QtGui.QComboBox()
table.setCellWidget(1,1, comboBox)

table.show()
sys.exit(app.exec_())

Basically, all I changed was giving the table a size. You can do this at its creation like I did, or with table.setRowCount().

And of course, for a table with r rows, make sure you don't insert anything at row r. It goes from 0 to r-1.

Upvotes: 3

Related Questions