根据 QTableWidget 的高亮行加载不同的 QTWidgets

Loading different QWidgets dependant on highlighting row of QTableWidget

有一个 QTableWidgets 和 table 列表以及另外两个 类 CombocellsGroupcells。它们显示为 QTabWidget。我想单击并突出显示 table 中的一行或单元格,然后 类 CombocellsGroupcells 刷新并根据 QTabWidget 行加载到 QTabWidget =98=]。

文件结构。

  • Main.py
  • Tablecells.py
  • Combocells.py
  • Groupcells.py

可视化

更新:

I have updated the code, signal and slot catch row number and send it. Despite by clicking on cells Data and Value tabs add to tabwidgets and removes, when row 0 is selected. what I do not get is when row 1 is selected, well Data and Value tabs add to widget, make some changes in combobox and Qlineedit inside Data and Value to see, when row 2 is clicked, would Data and Value really add to tabwidget from beginning. I notice it does not update. I have tried with addTab and insertTab with self.update(), still does not really process in a way I want to do.

Could anyone know how to fix this issue? Review this piece of script which has problem.

class Tabwidget(QtWidgets.QWidget):
    def __init__(self, parent=None):
        super( Tabwidget, self).__init__() 
-----------------------
   @QtCore.pyqtSlot(int) 
   def rowselected_tables(self, row):
        print('Row {} is selected.'.format(row))
        if row > 0:
            self.update()
            #self.Tab.addTab( self.Combo, 'Data')
            #self.Tab.addTab( self.Group, 'Values')
            self.Tab.insertTab( 1, self.Combo, 'Data')
            self.Tab.insertTab( 2, self.Group, 'Values')

我想根据 rows 的数量加载数据和值选项卡。例如,当用户单击并突出显示 table 中的某一行时,将加载 类 并且该行的 Data and Values 中的任何更改都应保留,当用户突出显示另一行时,然后是新的数据和值会出现,但如果 return 到先前突出显示和单击的行或单元格,Data and Values 中的任何先前更改将再次出现。

我想打印类似的东西。

Row x is highligted: Data : ( L6, 0,10) and Value: (10)

更新:

Main.py

import sys
from PyQt5 import QtCore, QtWidgets, QtGui
from Combocells import Combocells 
from Groupcells import Groupcells 
from Tablecells import Tablecells 

class Tabwidget(QtWidgets.QWidget):
    def __init__(self, parent=None):
        super( Tabwidget, self).__init__() 
        self.sizeHint()
        Tab = QtWidgets.QTabWidget()

        self.Table = Tablecells()
        self.Combo = Combocells()
        self.Group = Groupcells()

        Tab.addTab( self.Table, 'Tables')
        #Tab.addTab( self.Combo, 'Data')
        #Tab.addTab( self.Group, 'Values')

        self.Table.rownumber.connect(self.rowselected_tables)


        self.Tab.setFont(QtGui.QFont("Georgia",9,QtGui.QFont.Normal))

        vboxlayout = QtWidgets.QVBoxLayout()
        vboxlayout.addWidget(self.Tab)
        self.setLayout(vboxlayout)

    @QtCore.pyqtSlot(int) 
    def rowselected_tables(self, row):
        print('Row {} is selected.'.format(row))
        if row > 0:
            self.update()
            #self.Tab.addTab( self.Combo, 'Data')
            #self.Tab.addTab( self.Group, 'Values')
            self.Tab.insertTab( 1, self.Combo, 'Data')
            self.Tab.insertTab( 2, self.Group, 'Values')

        else:
            for n in [2,1]:
                self.Tab.removeTab( n )

if __name__ == '__main__':
    app = QtWidgets.QApplication(sys.argv)
    w = Tabwidget()
    w.show()
    sys.exit(app.exec_())

更新:

Tablecells.py

from PyQt5 import QtCore, QtGui, QtWidgets
import sys

class Tablecells(QtWidgets.QWidget):
    data = [("1", "Login",       "1", "test_login_s"), 
        ("2", "Logout",      "1", "test_logout_s"), 
        ("3", "User > Edit", "1", "test_user_edit_s")]
    rownumber = QtCore.pyqtSignal(int)

    def __init__(self, parent=None):
        super(Tablecells, self).__init__(parent)
        self.setFont(QtGui.QFont("Georgia",9,QtGui.QFont.Normal))
        self.tableWidget = QtWidgets.QTableWidget(0, 4)
        self.tableWidget.setHorizontalHeaderLabels(["Id", "Test name", "Owner", "Type"])
        self.tableWidget.cellClicked.connect(self.cellClick)
        self.setTableWidget()
        self.getrow()

        self.lay = QtWidgets.QHBoxLayout(self)
        self.lay.addWidget(self.tableWidget)

    def setTableWidget(self):    
        for r, (_id, _name, _owner, _type) in enumerate(self.data):
            it_id     = QtWidgets.QTableWidgetItem(_id)
            it_name   = QtWidgets.QTableWidgetItem(_name)
            it_owner  = QtWidgets.QTableWidgetItem(_owner)
            it_type = QtWidgets.QTableWidgetItem(_type)

            self.tableWidget.insertRow(self.tableWidget.rowCount())
            for c, item in enumerate((it_id, it_name, it_owner, it_type)):
                self.tableWidget.setItem(r, c, item)

    def cellClick(self, row, column):
        self.row = row
        self.column = column     
        print(self.row , self.column)
        self.rownumber.emit(self.row)

    def getrow(self):
        it = self.tableWidget.currentRow()
        print(it)


if __name__ == '__main__':
    app = QtWidgets.QApplication(sys.argv)
    ex = Tablecells()
    ex.show()
    sys.exit(app.exec_())

Combocells.py

from PyQt5 import QtCore, QtGui, QtWidgets
class Combocells(QtWidgets.QWidget):
    def __init__(self, parent=None):
        super(Combocells, self).__init__(parent)
        self.setFont(QtGui.QFont("Helvetica", 10, QtGui.QFont.Normal, italic=False))

        self.combo_exclass = QtWidgets.QComboBox()
        self.combo_exclass.addItems([" Type 1       "," Type 2       "," Type 3       "," Type 4       "," Type 5       "])

        self.combo_lclass = QtWidgets.QComboBox()
        self.combo_lclass.addItems(["L2","L4","L6","L8"])

        self.combo_vct = QtWidgets.QComboBox()

        self.combo_vct.addItems(["0.10","0.20","0.30","0.40",
                                        "0.50","0.60","0.70"])

        self.combo_in = QtWidgets.QComboBox()
        self.combo_in.addItems(["Class1","Class2","Class3"])        

        self.tbox = QtWidgets.QHBoxLayout()
        self.exclass = QtWidgets.QLabel("Class1: ")
        self.tbox.addWidget(self.exclass)
        self.tbox.addWidget(self.combo_exclass)


        self.mtbox = QtWidgets.QHBoxLayout()
        self.lclass = QtWidgets.QLabel("Class2: ")

        self.mtbox.addWidget(self.lclass)
        self.mtbox.addWidget(self.combo_lclass)


        self.mbbox = QtWidgets.QHBoxLayout()
        self.vct = QtWidgets.QLabel("Class3: ")
        self.mbbox.addWidget(self.vct)
        self.mbbox.addWidget(self.combo_vct)

        self.bbox = QtWidgets.QHBoxLayout()
        self.inl = QtWidgets.QLabel("Class4: ")
        self.bbox.addWidget(self.inl)
        self.bbox.addWidget(self.combo_in)

        self.grid = QtWidgets.QGridLayout()
        self.grid.addLayout(self.tbox, 0, 0, 1, 2)
        self.grid.addLayout(self.mtbox, 1, 0)
        self.grid.addLayout(self.mbbox, 2, 0)
        self.grid.addLayout(self.bbox, 3, 0)

        Environment_Group = QtWidgets.QGroupBox()
        Environment_Group.setTitle("&Group2")
        Environment_Group.setLayout(self.grid)

        vlay = QtWidgets.QVBoxLayout(self)
        vlay.addWidget(Environment_Group)

if __name__ == '__main__':
    import sys
    app = QtWidgets.QApplication(sys.argv)
    w = Combocells()
    w.show()
    sys.exit(app.exec_())

Groupcells.py

from PyQt5 import QtCore, QtGui, QtWidgets
class Groupcells(QtWidgets.QWidget):
    def __init__(self, parent=None):
        super(Groupcells, self).__init__(parent)
        self.setFont(QtGui.QFont("Helvetica", 10, QtGui.QFont.Normal, italic=False))      

        self.c_lay = QtWidgets.QHBoxLayout()
        fctd = "One\n\nTwo\n\nThree"
        con_strength = QtWidgets.QLabel(fctd)
        self.value = QtWidgets.QLineEdit('Test')
        self.c_lay.addWidget(con_strength)
        self.c_lay.addWidget(self.value, alignment=QtCore.Qt.AlignRight)


        self.combo = QtWidgets.QComboBox()
        self.combo.addItems(["10","12","14","16"])

        self.hbox = QtWidgets.QHBoxLayout()
        self.con = QtWidgets.QLabel("Number: ")
        self.hbox.addWidget(self.con)
        self.hbox.addWidget(self.combo)

        self.vlay = QtWidgets.QVBoxLayout()
        self.vlay.addLayout(self.hbox)
        self.vlay.addLayout(self.c_lay)
        self.vlay.addStretch()

        Concrete_Group = QtWidgets.QGroupBox()
        Concrete_Group.setTitle("&GroupA")
        Concrete_Group.setLayout(self.vlay)

        lay = QtWidgets.QVBoxLayout(self)
        lay.addWidget(Concrete_Group)

        self.comth = ["10","12","14","16"]
        self.combo.activated.connect(self.setdatastrength)

    @QtCore.pyqtSlot(int)
    def setdatastrength(self, index):
        value = self.comth[index]
        self.display_data(value)


    def display_data(self, value):
        try:
            f = value
            f_value = "{}"
            self.value.setText(f_value.format(f))

        except ValueError:
            print("Error")

if __name__ == '__main__':
    import sys
    app = QtWidgets.QApplication(sys.argv)
    w = Groupcells()
    w.show()
    sys.exit(app.exec_()) 

我真的不知道如何实现。它可能是 PyQT5 中的一些特殊脚本来完成任务。我感谢任何帮助。谢谢。

如果我正确理解你的问题,你希望 "Group" 和 "Combo" 选项卡记住你为特定行设置的值,并在你再次 select 该行时恢复这些值在 select 编辑了另一行之后。在这种情况下,您可以执行以下操作(GroupCells.pyComboCells.py 与以前相同)

在Main.py

class Tabwidget(QtWidgets.QWidget):
    def __init__(self, parent=None):
        super( Tabwidget, self).__init__()
        self.sizeHint()
        self.Tab = QtWidgets.QTabWidget()

        self.data = [("1", "Login", "1", "test_login_s"),
                ("2", "Logout", "1", "test_logout_s"),
                ("3", "User > Edit", "1", "test_user_edit_s")]

        self.combos = []
        self.groups = []
        self.Table = Tablecells()
        for row in self.data:
            self.addRow(row)

        self.Tab.addTab( self.Table, 'Tables')
        self.Table.rownumber.connect(self.rowselected_tables)
        self.Tab.setFont(QtGui.QFont("Georgia",9,QtGui.QFont.Normal))

        vboxlayout = QtWidgets.QVBoxLayout()
        vboxlayout.addWidget(self.Tab)
        self.setLayout(vboxlayout)

    def addRow(self, data):
        self.Table.addRow(data)
        self.combos.append(Combocells())
        self.groups.append(Groupcells())

    @QtCore.pyqtSlot(int)
    def rowselected_tables(self, row):
        print('Row {} is selected.'.format(row))
        while self.Tab.count() > 1:
            self.Tab.removeTab(self.Tab.count()-1)
        self.Tab.addTab(self.combos[row], 'Combo')
        self.Tab.addTab(self.groups[row], 'Group')

在TableCells.py

class Tablecells(QtWidgets.QWidget):
    rownumber = QtCore.pyqtSignal(int)

    def __init__(self, parent=None):
        super(Tablecells, self).__init__(parent)
        self.setFont(QtGui.QFont("Georgia",9,QtGui.QFont.Normal))
        self.tableWidget = QtWidgets.QTableWidget(0, 4)
        self.tableWidget.setHorizontalHeaderLabels(["Id", "Test name", "Owner", "Type"])
        self.tableWidget.cellClicked.connect(self.cellClick)
        self.getrow()

        self.lay = QtWidgets.QHBoxLayout(self)
        self.lay.addWidget(self.tableWidget)

    def addRow(self, data):
        _id, _name, _owner, _type = data
        it_id     = QtWidgets.QTableWidgetItem(_id)
        it_name   = QtWidgets.QTableWidgetItem(_name)
        it_owner  = QtWidgets.QTableWidgetItem(_owner)
        it_type = QtWidgets.QTableWidgetItem(_type)

        self.tableWidget.insertRow(self.tableWidget.rowCount())
        for c, item in enumerate((it_id, it_name, it_owner, it_type)):
            self.tableWidget.setItem(self.tableWidget.rowCount()-1, c, item)

    def cellClick(self, row, column):
        self.row = row
        self.column = column
        print(self.row , self.column)
        self.rownumber.emit(self.row)

    def getrow(self):
        it = self.tableWidget.currentRow()
        print(it)

在此示例中,我创建了一些 ComboCellsGroupCells 小部件,这些小部件等于 table 中的行数,并将这些小部件添加到 self.Tab 如果相应的行是 selected。为了做到这一点,我将向 table 添加数据的责任从 TableCells 转移到了 TabWidget,并用两种方法替换了 TableCells.setTableWidget()TableCells.addRow()TabWidget.addRow()ComboCellsGroupCells 小部件分别在 TabWidget.addRow 中创建并添加到 self.combosself.groupcells。然后将这些小部件添加和删除到 TabWidget.rowselected_tables() 中的 self.Tab