PDA

View Full Version : pyqt4 - dynamically populating a tableWidget with comboBoxs



stephen76
28th July 2010, 18:26
Hi
I am trying to hook up a spin box to create rows in a table and automatically populate them with comboboxes .. I have manged to use the signal/slot mechanism to create the extra rows of the table (from the sigbnal of the spinbox) and the first row has the correct comboboxes (created on startup)- but I cannot figure out how to create and populate the new rows with new combo boxes.
The following is the code ...



# -*- coding: utf-8 -*-

# Form implementation generated from reading ui file 'table6.ui'
#
# Created: Tue Jul 27 16:31:53 2010
# by: PyQt4 UI code generator 4.7.3
#
# WARNING! All changes made in this file will be lost!

from PyQt4 import QtCore, QtGui
import functools

class Ui_Form(object):
def setupUi(self, Form):
Form.setObjectName("Form")
Form.resize(465, 531)
self.tableWidget = QtGui.QTableWidget(Form)
self.tableWidget.setGeometry(QtCore.QRect(20, 70, 351, 201))
self.tableWidget.setRowCount(1)
self.tableWidget.setColumnCount(3)
self.tableWidget.setObjectName("tableWidget")
self.tableWidget.setColumnCount(3)
self.tableWidget.setRowCount(1)
item = QtGui.QTableWidgetItem()
self.tableWidget.setHorizontalHeaderItem(0, item)
item = QtGui.QTableWidgetItem()
self.tableWidget.setHorizontalHeaderItem(1, item)
item = QtGui.QTableWidgetItem()
self.tableWidget.setHorizontalHeaderItem(2, item)
self.comboBox = QtGui.QComboBox(Form)
self.comboBox.setGeometry(QtCore.QRect(30, 480, 61, 21))
self.comboBox.setMaxVisibleItems(2)
self.comboBox.setMaxCount(2)
self.comboBox.setObjectName("comboBox")
self.comboBox.addItem("")
self.comboBox.addItem("")
self.comboBox_2 = QtGui.QComboBox(Form)
self.comboBox_2.setGeometry(QtCore.QRect(150, 480, 121, 21))
self.comboBox_2.setMaxVisibleItems(2)
self.comboBox_2.setMaxCount(2)
self.comboBox_2.setObjectName("comboBox_2")
self.comboBox_2.addItem("")
self.comboBox_2.addItem("")
self.tableWidget.setCellWidget(0,1,self.comboBox)
self.tableWidget.setCellWidget(0,2,self.comboBox_2 )
self.tableWidget.setColumnWidth(2,120)

self.pushButton = QtGui.QPushButton(Form)
self.pushButton.setGeometry(QtCore.QRect(300, 20, 75, 25))
self.pushButton.setObjectName("pushButton")
self.spinBox = QtGui.QSpinBox(Form)
self.spinBox.setGeometry(QtCore.QRect(200, 20, 53, 22))
self.spinBox.setProperty("value", 1)
self.spinBox.setObjectName("spinBox")
self.label = QtGui.QLabel(Form)
self.label.setGeometry(QtCore.QRect(20, 20, 141, 16))
self.label.setObjectName("label")
self.pushButton_2 = QtGui.QPushButton(Form)
self.pushButton_2.setGeometry(QtCore.QRect(20, 300, 91, 25))
self.pushButton_2.setObjectName("pushButton_2")


self.retranslateUi(Form)
self.comboBox.setCurrentIndex(0)
self.comboBox_2.setCurrentIndex(0)
QtCore.QObject.connect(self.spinBox, QtCore.SIGNAL("valueChanged(int)"), self.createMyRow)




#QtCore.QMetaObject.connectSlotsByName(Form)

def retranslateUi(self, Form):
Form.setWindowTitle(QtGui.QApplication.translate("Form", "Add Passes", None, QtGui.QApplication.UnicodeUTF8))
self.tableWidget.horizontalHeaderItem(0).setText(Q tGui.QApplication.translate("Form", "Name", None, QtGui.QApplication.UnicodeUTF8))
self.tableWidget.horizontalHeaderItem(1).setText(Q tGui.QApplication.translate("Form", "Type", None, QtGui.QApplication.UnicodeUTF8))
self.tableWidget.horizontalHeaderItem(2).setText(Q tGui.QApplication.translate("Form", "Surface", None, QtGui.QApplication.UnicodeUTF8))
self.comboBox.setItemText(0, QtGui.QApplication.translate("Form", "color", None, QtGui.QApplication.UnicodeUTF8))
self.comboBox.setItemText(1, QtGui.QApplication.translate("Form", "bw", None, QtGui.QApplication.UnicodeUTF8))
self.comboBox_2.setItemText(0, QtGui.QApplication.translate("Form", "closest surface", None, QtGui.QApplication.UnicodeUTF8))
self.comboBox_2.setItemText(1, QtGui.QApplication.translate("Form", "opacity filtering", None, QtGui.QApplication.UnicodeUTF8))
self.pushButton.setText(QtGui.QApplication.transla te("Form", "Passes", None, QtGui.QApplication.UnicodeUTF8))
self.label.setText(QtGui.QApplication.translate("Form", "How many passes", None, QtGui.QApplication.UnicodeUTF8))
self.pushButton_2.setText(QtGui.QApplication.trans late("Form", "Add Passes", None, QtGui.QApplication.UnicodeUTF8))

def createMyRow(self, rows):
howManyRows = self.tableWidget.rowCount()
diff = rows - howManyRows
if diff > 0:
start = howManyRows
while rows > start:
print(start)
self.tableWidget.insertRow(start)
self.comboBoxNew = QtGui.QComboBox(Form)
start = start +1


I am also running this to start the gui..


#!/usr/bin/python -d

import sys
from PyQt4 import QtCore, QtGui
from table8 import Ui_Form

# Lars set this path to where you have my scripts!
sys.path += ['/jobs/generic/shots/lightDome/qtDesigner']

class MyForm(QtGui.QMainWindow):
def __init__(self, parent=None):
QtGui.QWidget.__init__(self, parent)
self.ui = Ui_Form()
self.ui.setupUi(self)
#QtCore.QObject.connect(self.ui.pushButton, QtCore.SIGNAL("clicked()"), self.ui.textEdit.clear )
#QtCore.QObject.connect(self.ui.lineEdit, QtCore.SIGNAL("returnPressed()"), self.add_entry)

if __name__ == "__main__":
app = QtGui.QApplication(sys.argv)
myapp = MyForm()
myapp.show()
sys.exit(app.exec_())


I guess
self.comboBoxNew = QtGui.QComboBox(Form)
in createMyRow(self, rows):
doesn't know about Form but I don' know how to get around this
Thanks in advance
stephen

norobro
28th July 2010, 20:34
Let me start off by saying that I am not a Pythoner.

Take a look a the generated code and compare it to your function createMyRow(self, rows). You create a new combobox but you don't add it to the tableWidget. So adding the following line should put the new combobox in the second cell of your new row:

self.tableWidget.setCellWidget(start,1,self.comboB oxNew)
By the way, take heed of the warning at the top of the file.

stephen76
29th July 2010, 10:21
Thanks for the help ... but the first problem is in line 94 (in function createMyRow()
self.comboBoxNew = QtGui.QComboBox(Form)
this line doesn't work ... I think it is because Form is not passed to my function createMyRow()
I am not sure what do do here ... I tried adding/passing Form to createMyRow() like so ..

def createMyRow(self, Form, rows):

I am not sure if this is even the right thing to do ... but it breaks ... my signal slot call on line 66

QtCore.QObject.connect(self.spinBox, QtCore.SIGNAL("valueChanged(int)"), self.createMyRow)

Thanks in advance
stephen

norobro
29th July 2010, 14:11
Hi Stephen,
Thanks for the help I think you meant "thanks for nothing".;)

Anyway, try creating the combobox without a parent. Like this:
self.comboBoxNew = QtGui.QComboBox()

stephen76
30th July 2010, 17:23
great that worked!
but I can't figure out how to add the parameters/options on the combo boxes I have added...
I can see the the initial combo boxes are set up by ...
def retranslateUi(self, Form):

but if I put the commands in the


def createMyRow(self, rows):
howManyRows = self.tableWidget.rowCount()
diff = rows - howManyRows
if diff > 0:
start = howManyRows
while rows > start:
print(start)
self.tableWidget.insertRow(start)
self.comboBoxNew = QtGui.QComboBox()
self.comboBoxNew.setItemText(0, QtGui.QApplication.translate("Form", "color", None, QtGui.QApplication.UnicodeUTF8))
self.comboBoxNew.setItemText(1, QtGui.QApplication.translate("Form", "bw", None, QtGui.QApplication.UnicodeUTF8))
self.tableWidget.setCellWidget(start,1,self.comboB oxNew)
start = start +1

The options don't show...
Not quite sure how to add the options to the user generated combos generated by the user (spinbox) so the they show in the gui...
Do I need to create another retranslateUi?
Thanks in advance!
s

stephen76
30th July 2010, 17:58
sorry I figured it out ....I was being stupid.


def createMyRow(self, rows):
howManyRows = self.tableWidget.rowCount()
diff = rows - howManyRows
if diff > 0:
start = howManyRows
while rows > start:
print(start)
self.tableWidget.insertRow(start)
self.comboBoxNew = QtGui.QComboBox()
self.comboBoxNew.setGeometry(QtCore.QRect(30, 480, 61, 21))
self.comboBoxNew.setMaxVisibleItems(2)
self.comboBoxNew.setMaxCount(2)
self.comboBoxNew.setObjectName("comboBox")
self.comboBoxNew.addItem("")
self.comboBoxNew.addItem("")
self.comboBoxNew.setItemText(0, QtGui.QApplication.translate("Form", "color", None, QtGui.QApplication.UnicodeUTF8))
self.comboBoxNew.setItemText(1, QtGui.QApplication.translate("Form", "bw", None, QtGui.QApplication.UnicodeUTF8))
self.tableWidget.setCellWidget(start,1,self.comboB oxNew)
#self.tableWidget.setCellWidget(start,2,self.newCo mb2)
start = start +1

norobro
31st July 2010, 00:58
I'm glad that you got it working.

About that warning at the top of the Ui_Form file: you really should consider putting your custom code in the file containing your MyForm class. If, in the future, you decide to make changes to your form you will have to re-key (or copy from backup) your custom code into the new file generated by pyuic4. All you have to do to access the widgets on the form is preface the widget name with “ui.”.

Something like this:
#!/usr/bin/python -d
# -*- coding: utf-8 -*-

import sys
from PyQt4 import QtCore, QtGui
from table8 import Ui_Form

# Lars set this path to where you have my scripts!
sys.path += ['/jobs/generic/shots/lightDome/qtDesigner']

class MyForm(QtGui.QMainWindow):
def __init__(self, parent=None):
QtGui.QWidget.__init__(self, parent)
self.ui = Ui_Form()
self.ui.setupUi(self)
QtCore.QObject.connect(self.ui.spinBox, QtCore.SIGNAL("valueChanged(int)"), self.createMyRow)
def createMyRow(self, rows):
howManyRows = self.ui.tableWidget.rowCount()
diff = rows - howManyRows
if diff > 0:
start = howManyRows
while rows > start:
print(start)
self.ui.tableWidget.insertRow(start)
self.comboBoxNew = QtGui.QComboBox()
self.comboBoxNew.setGeometry(QtCore.QRect(30, 480, 61, 21))
self.comboBoxNew.setMaxVisibleItems(2)
self.comboBoxNew.setMaxCount(2)
self.comboBoxNew.setObjectName("comboBox")
self.comboBoxNew.addItem("")
self.comboBoxNew.addItem("")
self.comboBoxNew.setItemText(0, QtGui.QApplication.translate("Form", "color", None, QtGui.QApplication.UnicodeUTF8))
self.comboBoxNew.setItemText(1, QtGui.QApplication.translate("Form", "bw", None, QtGui.QApplication.UnicodeUTF8))
self.ui.tableWidget.setCellWidget(start,1,self.com boBoxNew)
start = start +1

if __name__ == "__main__":
app = QtGui.QApplication(sys.argv)
myapp = MyForm()
myapp.show()
sys.exit(app.exec_())

stephen76
2nd August 2010, 12:28
Hey thanks for the advice ... I have moved my custom code.
Cheers!
stephen