获取 QtableWidget 值以在另一个 QDialog 中列出

Get QtableWidget values to list in another QDialog

我正在使用 PyQt5 为 QGIS 3.16 制作一个插件。我目前使用 Qt Designer 创建了三个 Qdialog。一个主对话框和两个弹出窗口。 第一个弹出窗口包含几个 lineEdits 和 doubleSpinBoxes,填充后将数据传递到主对话框中的 QTableWidget。这工作正常,但在 调用主对话框后 添加了数据,我认为这是造成部分问题的原因。

第二个弹出窗口包含一些动态添加的组合框。我想用前面提到的 QTableWidget 的行中的项目列表填充这些组合框。但是,当我尝试创建列表来填充组合框时,它仍然是空的。我如何在 填充 QTableWidget 后参考主对话框 ,以便正确填充列表?

主对话框中的相关片段class(请注意,代码的基础是由 QGIS 插件 builder 3 创建的)

FORM_CLASS, _ = uic.loadUiType(os.path.join(
    os.path.dirname(__file__), 'dialog_base.ui'))
FORM_CLASS_TAXA, _ = uic.loadUiType(os.path.join(
    os.path.dirname(__file__), 'dialog_popup_taxa.ui'))
FORM_CLASS_VEGCOM, _ = uic.loadUiType(os.path.join(
    os.path.dirname(__file__), 'dialog_popup_vegcom.ui'))
class MainDialog(QtWidgets.QDialog, FORM_CLASS):
    def __init__(self, parent=None):
        """Constructor."""
        super(MainDialog, self).__init__(parent)
        self.setupUi(self)

        self.addNew_Taxa.clicked.connect(self.addNewTaxon)
        self.addNew_VegCom.clicked.connect(self.addNewVegCom)

    def addNewTaxon(self):
        """ Adds a new pollen taxon to the list of taxa by opening a pop-up in which the taxon short name can be given"""
        self.taxonPopup = AddTaxonPopup()
        self.taxonPopup.show()
        result = self.taxonPopup.exec_()
        # runs when apply is clicked on the add new taxon popup
        if result:
            # Get filled in values
            taxonShortName = self.taxonPopup.lineEdit_taxonShortName.text()
            # Check if entry is valid and add to table
            if taxonShortName:
                rowCount = self.tableWidget_Taxa.rowCount()
                self.tableWidget_Taxa.setRowCount(rowCount+1)
                self.tableWidget_Taxa.setItem(rowCount, 0, QTableWidgetItem(taxonShortName))
            else: 
                pass

    def addNewVegCom(self):
        """ Adds a new vegetation community to the list of communities by opening a pop-up in which a list of species
         and their percentages, as well as a new community name can be given"""
        self.vegComPopup = AddVegComPopup()
        self.vegComPopup.show()

        #add entries to table (not yet implemented)
        result = self.vegComPopup.exec_()
        if result:
            pass

弹出窗口 1 的代码(因为无需动态创建任何内容,这仅引用 .ui 文件,包含一行编辑)

class AddTaxonPopup (QtWidgets.QDialog, FORM_CLASS_TAXA):
    def __init__(self, parent=None):
        """Popup Constructor."""
        super(AddTaxonPopup, self).__init__(parent)
        self.setupUi(self)

弹出窗口 2 的代码(有点乱,因为它部分在 .ui 文件中创建,部分在代码中创建)

class AddVegComPopup (QtWidgets.QDialog, FORM_CLASS_VEGCOM):
    def __init__(self, parent=None):
        """Popup Constructor."""
        super(AddVegComPopup, self).__init__(parent)
        self.setupUi(self)
        #events
        self.pushButton_vegComAddSpecies.clicked.connect(self.addVegComTaxonRow)

        #class variable for keeping count of the number of taxa
        self.previous = 0

        #add gridlayout to scrollarea
        self.frameforscrolling = QFrame(self.scrollArea)
        self.frameforscrolling.setLayout(self.gridLayout)
        self.scrollArea.setWidget(self.frameforscrolling)

        #set locations of original .ui widgets in grid (because I couldn't get the grid to behave in Qt designer)

        self.gridLayout.addWidget(self.label_Title, 0, 0, 1, 4)
        self.gridLayout.addWidget(self.label_Name, 1, 0)
        self.gridLayout.addWidget(self.buttonBox_2, 2, 1, 1, 3)

    def addVegComTaxonRow(self):
        """ Adds a new comboBox and doubleSpinBox to be able to add a new taxon to a vegetation community"""
        #get location of start OR previous taxon comboBox

        label = QLabel('Taxon ' + str(int((self.previous * 0.5)+1)), self)
        self.comboBox = QComboBox()
        doubleSpin = QDoubleSpinBox()
        # insert the new widgets
        self.gridLayout.addWidget(label, self.previous+2, 0, 1, 4)
        self.gridLayout.addWidget(self.comboBox, self.previous+3, 0, 1, 3)
        self.gridLayout.addWidget(doubleSpin, self.previous+3, 3, 1, 2)
        self.gridLayout.addWidget(self.buttonBox_2, self.previous + 4, 0, 1, 4)
        self.previous += 2
        # Fill the comboBox HERE LIES THE PROBLEM
        self.taxonlist = []
        self.mainDialog = MainDialog()
        for taxon in range(self.mainDialog.tableWidget_Taxa.rowCount()):
            self.taxonlist.append(self.mainDialog.tableWidget_Taxa.item(taxon, 0).text())
        print(self.taxonlist)
        self.comboBox.addItems(self.taxonlist)

问题是,在 AddVegComPopup.addVegComTaxonRow 中,您正在创建 MainDialog 的新实例,并从这个新的(隐藏的)小部件而不是从显示的现有对话框中提取项目列表屏幕。解决此问题的一种方法是在 MainDialog.addNewVegCom 中初始化它时向 AddVegComPopup 对话框提供项目列表,并将此列表分配给 AddVegComPopup 中的实例变量,以便您可以每次创建新的投递箱时访问它。为此,MainDialog.addNewVegCom 需要更改为如下内容:

def addNewVegCom(self):
    """ Adds a new vegetation community to the list of communities by opening a pop-up in which a list of species
    and their percentages, as well as a new community name can be given"""

    table = self.tableWidget_Taxa
    item_lst = [table.item(row, 0).text() for row in range(table.rowCount())]
    self.vegComPopup = AddVegComPopup(item_lst)
    result = self.vegComPopup.exec_()
    if result:
        pass

并且AddVegComPopup需要根据

更新
class AddVegComPopup (QtWidgets.QDialog):

    def __init__(self, taxonlist,  parent=None):   # note extra input parameter
        """Popup Constructor."""

        ... as before ...

        self.taxonlist = taxonlist

    ....

    def addVegComTaxonRow(self):
        """ Adds a new comboBox and doubleSpinBox to be able to add a new taxon to a vegetation community"""
        # this is the same as before
        label = QLabel('Taxon ' + str(int((self.previous * 0.5)+1)), self)
        self.comboBox = QComboBox()
        doubleSpin = QDoubleSpinBox()
        # insert the new widgets
        self.gridLayout.addWidget(label, self.previous+2, 0, 1, 4)
        self.gridLayout.addWidget(self.comboBox, self.previous+3, 0, 1, 3)
        self.gridLayout.addWidget(doubleSpin, self.previous+3, 3, 1, 2)
        self.gridLayout.addWidget(self.buttonBox_2, self.previous + 4, 0, 1, 4)
        self.previous += 2
        
        # use predefined list
        self.comboBox.addItems(self.taxonlist)