Home>

I have it created by clicking a button in an iteration of a row in a tableQTableWidgetand these lines also createQComboboxwith the values ​​"Sold" and "Available".

When selected by the user in anyQComboboxvalues ​​"Available", a window appears, and I try, when the user in the window clicked "OK", then the value inQComboboxreturned back to "Sold".

I know how to set the value if when creatingQComboboxgive an explicit name to an instance of the class (QCombobox.setCurentIndex ()).
But, I do not understand how to turnQComboboxwhich is created in iteration without explicitly specifying a name, inQTableWidget...

Here is my code. In methodshow_message ()on button click i want to return valueQComboboxback to Sold.

import sys
from PyQt5 import QtWidgets
from PyQt5.QtWidgets import QApplication, QWidget, \
QVBoxLayout, QMessageBox
class MyWindow (QtWidgets.QMainWindow):
    def __init __ (self, parent= None):
        super () .__ init __ ()
        self.centralWidget= QWidget ()
        self.setCentralWidget (self.centralWidget)
        self.row_count= 1
        self.table_index= 0
        self.tableWidget= QtWidgets.QTableWidget ()
        self.tableWidget.setRowCount (0)
        self.tableWidget.setColumnCount (2)
        self.button= QtWidgets.QPushButton ('Add Table Rows')
        self.button_1= QtWidgets.QPushButton ('Output cell value')
        self.layout= QVBoxLayout (self.centralWidget)
        self.layout.addWidget (self.tableWidget)
        self.layout.addWidget (self.button)
        self.button.clicked.connect (self.add_cells)
    def add_cells (self):
        row= self.tableWidget.rowCount ()
        self.tableWidget.setRowCount (self.row_count)
        self.tableWidget.setItem (self.table_index, 0, QtWidgets.QTableWidgetItem (f'Nokia {self.table_index} '))
        combobox= QtWidgets.QComboBox ()
        combobox.addItem ('Sold Out')
        combobox.addItem ('In Stock')
        combobox.currentTextChanged.connect (lambda text, row= row: self.show_message (text, row))
        self.tableWidget.setCellWidget (self.table_index, 1, combobox)
        self.row_count += 1
        self.table_index += 1
    def show_message (self, text, row): # a popup window that accepts the text of the button and the line number and outputs it to the message
        if text== 'Available':
            msg= QMessageBox ()
            msg.setInformativeText (f 'Return the value in row {row} back?')
            msg.exec_ ()
if __name__== '__main__':
  app= QApplication (sys.argv)
  w= MyWindow ()
  w.show ()
  sys.exit (app.exec_ ())
  • Answer # 1

    I saw your task like this:

    import sys
    from PyQt5 import QtWidgets
    from PyQt5.QtWidgets import QApplication, QWidget, \
        QVBoxLayout, QMessageBox
    class MyWindow (QtWidgets.QMainWindow):
        def __init __ (self, parent= None):
            super () .__ init __ ()
            self.centralWidget= QWidget ()
            self.setCentralWidget (self.centralWidget)
            self.row_count= 1
            self.table_index= 0
            self.tableWidget= QtWidgets.QTableWidget ()
            self.tableWidget.setRowCount (0)
            self.tableWidget.setColumnCount (2)
            self.button= QtWidgets.QPushButton ('Add Table Rows')
            self.button_1= QtWidgets.QPushButton ('Output cell value')
            self.layout= QVBoxLayout (self.centralWidget)
            self.layout.addWidget (self.tableWidget)
            self.layout.addWidget (self.button)
            self.button.clicked.connect (self.add_cells)
        def add_cells (self):
            row= self.tableWidget.rowCount ()
            self.tableWidget.setRowCount (self.row_count)
            self.tableWidget.setItem (
                self.table_index,
                0,
                QtWidgets.QTableWidgetItem (f'Nokia {self.table_index} ')
            )
            combobox= QtWidgets.QComboBox ()
            combobox.addItem ('In stock:') # +++
            combobox.addItem (f'Nokia_ {self.table_index} _1 ') # +++
            combobox.addItem (f'Nokia_ {self.table_index} _2 ') # +++
            combobox.addItem (f'Nokia_ {self.table_index} _3 ') # +++
            combobox.currentTextChanged.connect (
                lambda text, row= row, cb= combobox: self.show_message (text, row, cb))
    # +++ ^^^^^^^^^^^^ ^^
            self.tableWidget.setCellWidget (self.table_index, 1, combobox)
            self.row_count += 1
            self.table_index += 1
        # popup window that accepts button text, line number
        # and cb prints to the message
    # +++ vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv
    # +++ vv
        def show_message (self, text, row, cb):
            if text!= 'Available:' and text!= 'Sold out': # +++
                msg= QMessageBox ()
                msg.setInformativeText (f'Do you want to buy {text}? ') # +++
                msg.addButton (QMessageBox.Yes) # +++
                msg.addButton (QMessageBox.No) # +++
                bttn= msg.exec_ () # +++ bttn
                if bttn!= QMessageBox.Yes:
                    return
                row= cb.model (). rowCount ()
                for i in range (cb.model (). rowCount ()):
                    t= cb.model (). item (i, 0) .text ()
                    if t== text:
                        cb.model (). item (i, 0) .setText ('Sold Out')
    # +++ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Set
    if __name__== '__main__':
      app= QApplication (sys.argv)
      w= MyWindow ()
      w.show ()
      sys.exit (app.exec_ ())
    

    When I wrote this question, I did not doubt your answer, thanks to you I learned so much. Thanks!

    catharsis2021-09-29 06:48:50