0

I want to input data in my table and then have it appear in my combo box after pressing 'ok' pushbutton in pyqt. May I know how to go about doing that? Whenever I run my code it just says there is an error at self.comboBox.addItem(item). I don't know which other command to use. Here is part of my code:

def setup(self, Dialog):
                  ... 
                  ...
    self.comboBox = QtGui.QComboBox(Dialog)
    self.comboBox.setGeometry(QtCore.QRect(20, 100, 431, 22))
    self.comboBox.setObjectName(_fromUtf8("comboBox"))
    self.tableWidget = QtGui.QTableWidget(Dialog)
    self.tableWidget.setGeometry(QtCore.QRect(20, 470, 651, 71))
    self.tableWidget.setSelectionMode(QtGui.QAbstractItemView.ExtendedSelection)
    self.tableWidget.setTextElideMode(QtCore.Qt.ElideRight)
    self.tableWidget.setVerticalScrollMode(QtGui.QAbstractItemView.ScrollPerItem)
    self.tableWidget.setRowCount(1)
    self.tableWidget.setColumnCount(129)
    self.tableWidget.setObjectName(_fromUtf8("tableWidget"))      
    self.tableWidget.horizontalHeader().setVisible(True)
    self.tableWidget.horizontalHeader().setDefaultSectionSize(25)
    self.tableWidget.horizontalHeader().setMinimumSectionSize(26)
    self.tableWidget.verticalHeader().setDefaultSectionSize(25)
    self.tableWidget.verticalHeader().setHighlightSections(True)
    self.pushButton_7 = QtGui.QPushButton(Dialog)
    self.pushButton_7.setGeometry(QtCore.QRect(220, 650, 75, 23))
    self.pushButton_7.setObjectName(_fromUtf8("pushButton_7"))
    self.pushButton_7.clicked.connect(self.additem)

def retranslateUi(self, Dialog):
                  ...
                  ...
    self.tableWidget.setSortingEnabled(False)

def additem(self):
    item = self.tableWidget.item(0,0)
    self.comboBox.addItem(item)

UPDATE: The solution only works for the first box of my tablewidget. I tried to do it like this:

   def additem(self):
       while true:
           item = self.tableWidget.item(0, 0).text()
           self.comboBox.addItem(item)
           item1 = self.tableWidget.item(0, 1).text()
           self.comboBox.addItem(item1)

However, I just keep getting the error that 'NoneType' object has no attribute 'text'

UPDATE: I tried your suggestion. I tried to upload images so it would be clearer but I need 10 reputation in order to do so. Anyway, I typed this in the tablewidget: 11 22 33 44 each one in one box for four boxes. But only 11 appeared in combobox after pressing 'ok'. It doesn't work even if I typed my value on the other boxes, it only works for the first box. What I need is for them to appear as '11, 22, 33, 44' in my combo box. Would it be possible to do this? As well as for all 128 columns of my tablewidget?

Viv91
  • 45
  • 2
  • 8
  • Answer for your UPDATE: you can run example from my answer, it works, so it means there is something you are doing wrong. Please post working example as new question since your primary problem is solved – Aleksandar Jul 17 '14 at 08:53
  • Ok i will post it as a new question – Viv91 Jul 17 '14 at 09:30

1 Answers1

1

in addItem(self) item is tableWidgetItem type. What you need is QString to add as comboBox item so try to correct this line only:

item = self.tableWidget.item(0,0).text()

here is working example:

# -*- coding: utf-8 -*-
import sys
from PyQt4 import QtCore, QtGui
from PyQt4.QtCore import Qt

class Widget(QtGui.QWidget):
    def __init__(self):
        super(Widget, self).__init__()
        self.layout = QtGui.QVBoxLayout(self)

        self.comboBox = QtGui.QComboBox(self)
        self.comboBox.setGeometry(QtCore.QRect(20, 100, 431, 22))

        self.tableWidget = QtGui.QTableWidget(self)
        self.tableWidget.setGeometry(QtCore.QRect(20, 470, 651, 71))
        self.tableWidget.setSelectionMode(QtGui.QAbstractItemView.ExtendedSelection)
        self.tableWidget.setTextElideMode(QtCore.Qt.ElideRight)
        self.tableWidget.setVerticalScrollMode(QtGui.QAbstractItemView.ScrollPerItem)
        self.tableWidget.setRowCount(1)
        self.tableWidget.setColumnCount(3) 
        self.tableWidget.horizontalHeader().setVisible(True)
        self.tableWidget.horizontalHeader().setDefaultSectionSize(25)
        self.tableWidget.horizontalHeader().setMinimumSectionSize(26)
        self.tableWidget.verticalHeader().setDefaultSectionSize(25)
        self.tableWidget.verticalHeader().setHighlightSections(True)
        self.tableWidget.setItem(0, 0, QtGui.QTableWidgetItem("first"))
        self.tableWidget.setItem(0, 1, QtGui.QTableWidgetItem("second"))
        self.tableWidget.setItem(0, 2, QtGui.QTableWidgetItem("third"))
        self.tableWidget.resizeColumnsToContents()

        self.pushButton_7 = QtGui.QPushButton("Add item 0.0 to combo")
        self.pushButton_7.setGeometry(QtCore.QRect(220, 650, 75, 23))
        self.pushButton_7.clicked.connect(self.additem)

        self.layout.addWidget(self.comboBox)
        self.layout.addWidget(self.tableWidget)
        self.layout.addWidget(self.pushButton_7)

    def additem(self):
        item = self.tableWidget.item(0,0)#.text()
        self.comboBox.addItem(item)

if __name__ == '__main__':
    app = QtGui.QApplication([])
    w = Widget()
    w.show()
    sys.exit(app.exec_())

EDIT

if you want to add all cells from table row to combo box, your additem should look like this:

def additem(self):
    for i in range(0, self.tableWidget.columnCount()):
        item = self.tableWidget.item(0,i)
        if item != None:
            text = item.text()
            self.comboBox.addItem(text)

Keep in mind that if table cell is empty (item == None) nothing will be added to combo box

Aleksandar
  • 3,541
  • 4
  • 34
  • 57
  • Hi, I realised that this code only allows value to be transferred once. I want to do it so that I can change the value without closing and reopening the whole program again. Is it possible? Also, I would like to do the same to all the other boxes in tablewidget. I tried to create the same code for another 'item1' for another column (0, 1) but it only works for one of them. Any solution to overcome this? I will show the code on what I did in my post above. – Viv91 Jul 17 '14 at 07:00
  • 1
    take a lokk at **EDIT** in my answer. You keep getting error `NoneType object has no attribute text` probably because some of your table cells are empty. Also `while true` is infinite loop. – Aleksandar Jul 17 '14 at 07:33
  • It still doesnt work, I have updated my post and I will keep trying my code in the meantime. – Viv91 Jul 17 '14 at 08:05