Results 1 to 8 of 8

Thread: pyqt4 - dynamically populating a tableWidget with comboBoxs

  1. #1
    Join Date
    Jul 2010
    Posts
    7
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default pyqt4 - dynamically populating a tableWidget with comboBoxs

    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 ...

    Qt Code:
    1. # -*- coding: utf-8 -*-
    2.  
    3. # Form implementation generated from reading ui file 'table6.ui'
    4. #
    5. # Created: Tue Jul 27 16:31:53 2010
    6. # by: PyQt4 UI code generator 4.7.3
    7. #
    8. # WARNING! All changes made in this file will be lost!
    9.  
    10. from PyQt4 import QtCore, QtGui
    11. import functools
    12.  
    13. class Ui_Form(object):
    14. def setupUi(self, Form):
    15. Form.setObjectName("Form")
    16. Form.resize(465, 531)
    17. self.tableWidget = QtGui.QTableWidget(Form)
    18. self.tableWidget.setGeometry(QtCore.QRect(20, 70, 351, 201))
    19. self.tableWidget.setRowCount(1)
    20. self.tableWidget.setColumnCount(3)
    21. self.tableWidget.setObjectName("tableWidget")
    22. self.tableWidget.setColumnCount(3)
    23. self.tableWidget.setRowCount(1)
    24. item = QtGui.QTableWidgetItem()
    25. self.tableWidget.setHorizontalHeaderItem(0, item)
    26. item = QtGui.QTableWidgetItem()
    27. self.tableWidget.setHorizontalHeaderItem(1, item)
    28. item = QtGui.QTableWidgetItem()
    29. self.tableWidget.setHorizontalHeaderItem(2, item)
    30. self.comboBox = QtGui.QComboBox(Form)
    31. self.comboBox.setGeometry(QtCore.QRect(30, 480, 61, 21))
    32. self.comboBox.setMaxVisibleItems(2)
    33. self.comboBox.setMaxCount(2)
    34. self.comboBox.setObjectName("comboBox")
    35. self.comboBox.addItem("")
    36. self.comboBox.addItem("")
    37. self.comboBox_2 = QtGui.QComboBox(Form)
    38. self.comboBox_2.setGeometry(QtCore.QRect(150, 480, 121, 21))
    39. self.comboBox_2.setMaxVisibleItems(2)
    40. self.comboBox_2.setMaxCount(2)
    41. self.comboBox_2.setObjectName("comboBox_2")
    42. self.comboBox_2.addItem("")
    43. self.comboBox_2.addItem("")
    44. self.tableWidget.setCellWidget(0,1,self.comboBox)
    45. self.tableWidget.setCellWidget(0,2,self.comboBox_2)
    46. self.tableWidget.setColumnWidth(2,120)
    47.  
    48. self.pushButton = QtGui.QPushButton(Form)
    49. self.pushButton.setGeometry(QtCore.QRect(300, 20, 75, 25))
    50. self.pushButton.setObjectName("pushButton")
    51. self.spinBox = QtGui.QSpinBox(Form)
    52. self.spinBox.setGeometry(QtCore.QRect(200, 20, 53, 22))
    53. self.spinBox.setProperty("value", 1)
    54. self.spinBox.setObjectName("spinBox")
    55. self.label = QtGui.QLabel(Form)
    56. self.label.setGeometry(QtCore.QRect(20, 20, 141, 16))
    57. self.label.setObjectName("label")
    58. self.pushButton_2 = QtGui.QPushButton(Form)
    59. self.pushButton_2.setGeometry(QtCore.QRect(20, 300, 91, 25))
    60. self.pushButton_2.setObjectName("pushButton_2")
    61.  
    62.  
    63. self.retranslateUi(Form)
    64. self.comboBox.setCurrentIndex(0)
    65. self.comboBox_2.setCurrentIndex(0)
    66. QtCore.QObject.connect(self.spinBox, QtCore.SIGNAL("valueChanged(int)"), self.createMyRow)
    67.  
    68.  
    69.  
    70.  
    71. #QtCore.QMetaObject.connectSlotsByName(Form)
    72.  
    73. def retranslateUi(self, Form):
    74. Form.setWindowTitle(QtGui.QApplication.translate("Form", "Add Passes", None, QtGui.QApplication.UnicodeUTF8))
    75. self.tableWidget.horizontalHeaderItem(0).setText(QtGui.QApplication.translate("Form", "Name", None, QtGui.QApplication.UnicodeUTF8))
    76. self.tableWidget.horizontalHeaderItem(1).setText(QtGui.QApplication.translate("Form", "Type", None, QtGui.QApplication.UnicodeUTF8))
    77. self.tableWidget.horizontalHeaderItem(2).setText(QtGui.QApplication.translate("Form", "Surface", None, QtGui.QApplication.UnicodeUTF8))
    78. self.comboBox.setItemText(0, QtGui.QApplication.translate("Form", "color", None, QtGui.QApplication.UnicodeUTF8))
    79. self.comboBox.setItemText(1, QtGui.QApplication.translate("Form", "bw", None, QtGui.QApplication.UnicodeUTF8))
    80. self.comboBox_2.setItemText(0, QtGui.QApplication.translate("Form", "closest surface", None, QtGui.QApplication.UnicodeUTF8))
    81. self.comboBox_2.setItemText(1, QtGui.QApplication.translate("Form", "opacity filtering", None, QtGui.QApplication.UnicodeUTF8))
    82. self.pushButton.setText(QtGui.QApplication.translate("Form", "Passes", None, QtGui.QApplication.UnicodeUTF8))
    83. self.label.setText(QtGui.QApplication.translate("Form", "How many passes", None, QtGui.QApplication.UnicodeUTF8))
    84. self.pushButton_2.setText(QtGui.QApplication.translate("Form", "Add Passes", None, QtGui.QApplication.UnicodeUTF8))
    85.  
    86. def createMyRow(self, rows):
    87. howManyRows = self.tableWidget.rowCount()
    88. diff = rows - howManyRows
    89. if diff > 0:
    90. start = howManyRows
    91. while rows > start:
    92. print(start)
    93. self.tableWidget.insertRow(start)
    94. self.comboBoxNew = QtGui.QComboBox(Form)
    95. start = start +1
    To copy to clipboard, switch view to plain text mode 

    I am also running this to start the gui..
    Qt Code:
    1. #!/usr/bin/python -d
    2.  
    3. import sys
    4. from PyQt4 import QtCore, QtGui
    5. from table8 import Ui_Form
    6.  
    7. # Lars set this path to where you have my scripts!
    8. sys.path += ['/jobs/generic/shots/lightDome/qtDesigner']
    9.  
    10. class MyForm(QtGui.QMainWindow):
    11. def __init__(self, parent=None):
    12. QtGui.QWidget.__init__(self, parent)
    13. self.ui = Ui_Form()
    14. self.ui.setupUi(self)
    15. #QtCore.QObject.connect(self.ui.pushButton, QtCore.SIGNAL("clicked()"), self.ui.textEdit.clear )
    16. #QtCore.QObject.connect(self.ui.lineEdit, QtCore.SIGNAL("returnPressed()"), self.add_entry)
    17.  
    18. if __name__ == "__main__":
    19. app = QtGui.QApplication(sys.argv)
    20. myapp = MyForm()
    21. myapp.show()
    22. sys.exit(app.exec_())
    To copy to clipboard, switch view to plain text mode 

    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

  2. #2
    Join Date
    Feb 2008
    Posts
    491
    Thanks
    12
    Thanked 142 Times in 135 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11

    Default Re: pyqt4 - dynamically populating a tableWidget with comboBoxs

    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:
    Qt Code:
    1. self.tableWidget.setCellWidget(start,1,self.comboBoxNew)
    To copy to clipboard, switch view to plain text mode 
    By the way, take heed of the warning at the top of the file.

  3. #3
    Join Date
    Jul 2010
    Posts
    7
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: pyqt4 - dynamically populating a tableWidget with comboBoxs

    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

  4. #4
    Join Date
    Feb 2008
    Posts
    491
    Thanks
    12
    Thanked 142 Times in 135 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11

    Default Re: pyqt4 - dynamically populating a tableWidget with comboBoxs

    Hi Stephen,
    Quote Originally Posted by stephen76 View Post
    Thanks for the help
    I think you meant "thanks for nothing".

    Anyway, try creating the combobox without a parent. Like this:
    Qt Code:
    1. self.comboBoxNew = QtGui.QComboBox()
    To copy to clipboard, switch view to plain text mode 
    Last edited by norobro; 29th July 2010 at 14:26.

  5. #5
    Join Date
    Jul 2010
    Posts
    7
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: pyqt4 - dynamically populating a tableWidget with comboBoxs

    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
    Qt Code:
    1. def createMyRow(self, rows):
    2. howManyRows = self.tableWidget.rowCount()
    3. diff = rows - howManyRows
    4. if diff > 0:
    5. start = howManyRows
    6. while rows > start:
    7. print(start)
    8. self.tableWidget.insertRow(start)
    9. self.comboBoxNew = QtGui.QComboBox()
    10. self.comboBoxNew.setItemText(0, QtGui.QApplication.translate("Form", "color", None, QtGui.QApplication.UnicodeUTF8))
    11. self.comboBoxNew.setItemText(1, QtGui.QApplication.translate("Form", "bw", None, QtGui.QApplication.UnicodeUTF8))
    12. self.tableWidget.setCellWidget(start,1,self.comboBoxNew)
    13. start = start +1
    To copy to clipboard, switch view to plain text mode 
    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

  6. #6
    Join Date
    Jul 2010
    Posts
    7
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: pyqt4 - dynamically populating a tableWidget with comboBoxs

    sorry I figured it out ....I was being stupid.
    Qt Code:
    1. def createMyRow(self, rows):
    2. howManyRows = self.tableWidget.rowCount()
    3. diff = rows - howManyRows
    4. if diff > 0:
    5. start = howManyRows
    6. while rows > start:
    7. print(start)
    8. self.tableWidget.insertRow(start)
    9. self.comboBoxNew = QtGui.QComboBox()
    10. self.comboBoxNew.setGeometry(QtCore.QRect(30, 480, 61, 21))
    11. self.comboBoxNew.setMaxVisibleItems(2)
    12. self.comboBoxNew.setMaxCount(2)
    13. self.comboBoxNew.setObjectName("comboBox")
    14. self.comboBoxNew.addItem("")
    15. self.comboBoxNew.addItem("")
    16. self.comboBoxNew.setItemText(0, QtGui.QApplication.translate("Form", "color", None, QtGui.QApplication.UnicodeUTF8))
    17. self.comboBoxNew.setItemText(1, QtGui.QApplication.translate("Form", "bw", None, QtGui.QApplication.UnicodeUTF8))
    18. self.tableWidget.setCellWidget(start,1,self.comboBoxNew)
    19. #self.tableWidget.setCellWidget(start,2,self.newComb2)
    20. start = start +1
    To copy to clipboard, switch view to plain text mode 

  7. #7
    Join Date
    Feb 2008
    Posts
    491
    Thanks
    12
    Thanked 142 Times in 135 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11

    Default Re: pyqt4 - dynamically populating a tableWidget with comboBoxs

    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:
    Qt Code:
    1. #!/usr/bin/python -d
    2. # -*- coding: utf-8 -*-
    3.  
    4. import sys
    5. from PyQt4 import QtCore, QtGui
    6. from table8 import Ui_Form
    7.  
    8. # Lars set this path to where you have my scripts!
    9. sys.path += ['/jobs/generic/shots/lightDome/qtDesigner']
    10.  
    11. class MyForm(QtGui.QMainWindow):
    12. def __init__(self, parent=None):
    13. QtGui.QWidget.__init__(self, parent)
    14. self.ui = Ui_Form()
    15. self.ui.setupUi(self)
    16. QtCore.QObject.connect(self.ui.spinBox, QtCore.SIGNAL("valueChanged(int)"), self.createMyRow)
    17. def createMyRow(self, rows):
    18. howManyRows = self.ui.tableWidget.rowCount()
    19. diff = rows - howManyRows
    20. if diff > 0:
    21. start = howManyRows
    22. while rows > start:
    23. print(start)
    24. self.ui.tableWidget.insertRow(start)
    25. self.comboBoxNew = QtGui.QComboBox()
    26. self.comboBoxNew.setGeometry(QtCore.QRect(30, 480, 61, 21))
    27. self.comboBoxNew.setMaxVisibleItems(2)
    28. self.comboBoxNew.setMaxCount(2)
    29. self.comboBoxNew.setObjectName("comboBox")
    30. self.comboBoxNew.addItem("")
    31. self.comboBoxNew.addItem("")
    32. self.comboBoxNew.setItemText(0, QtGui.QApplication.translate("Form", "color", None, QtGui.QApplication.UnicodeUTF8))
    33. self.comboBoxNew.setItemText(1, QtGui.QApplication.translate("Form", "bw", None, QtGui.QApplication.UnicodeUTF8))
    34. self.ui.tableWidget.setCellWidget(start,1,self.comboBoxNew)
    35. start = start +1
    36.  
    37. if __name__ == "__main__":
    38. app = QtGui.QApplication(sys.argv)
    39. myapp = MyForm()
    40. myapp.show()
    41. sys.exit(app.exec_())
    To copy to clipboard, switch view to plain text mode 

  8. #8
    Join Date
    Jul 2010
    Posts
    7
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: pyqt4 - dynamically populating a tableWidget with comboBoxs

    Hey thanks for the advice ... I have moved my custom code.
    Cheers!
    stephen

Similar Threads

  1. TableWidget
    By matulik in forum Newbie
    Replies: 6
    Last Post: 2nd May 2010, 18:47
  2. Replies: 3
    Last Post: 8th December 2009, 10:49
  3. Error when populating lazily QListView
    By Bakuriu in forum Qt Programming
    Replies: 2
    Last Post: 29th October 2009, 11:56
  4. QTableView populating question
    By onefootswill in forum Newbie
    Replies: 8
    Last Post: 7th August 2008, 22:29
  5. Replies: 2
    Last Post: 23rd November 2006, 11:33

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.