根据 QTableWidget 的突出显示行加载不同的 QWidget [英] Loading different QWidgets dependant on highlighting row of QTableWidget

查看:87
本文介绍了根据 QTableWidget 的突出显示行加载不同的 QWidget的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有一个QTableWidgets,其中包含一个表格列表和两个额外的类CombocellsGroupcells.它们用 QTabWidget 显示.我想单击并突出显示表格中的一行或单元格,然后两个类 CombocellsGroupcells 根据行刷新并加载到 QTabWidget桌子.

Having a QTableWidgets with a list of tables and two additional classes Combocells and Groupcells. They are shown with QTabWidget. I want to click and highlight a row or cell in the table then the two classes Combocells and Groupcells refreshs and loads into QTabWidget according to row of table.

文件结构.

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

可视化

更新:

我已经更新了代码、信号和槽捕获行号并发送.尽管通过单击单元格 Data and Value 选项卡添加到 tabwidgets 和删除,当 row 0 被选中时.我不明白的是当 row 1 是选择,以及 Data 和 Value 选项卡添加到小部件,在comboboxQlineedit 在 Data 和 Value 中查看,当 row 2 为点击后,Data 和 Value 真的会从一开始就添加到 tabwidget 中.我注意到它没有更新.我试过 addTabinsertTab使用 self.update(),仍然没有真正以我想要的方式进行处理做.

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')

我想根据的数量相应地加载数据和值选项卡.例如,当用户单击并突出显示表中的某一行时,类将被加载,并且该行的 Data 和 Values 中的任何更改都应保留,当用户突出显示另一行时,会出现新的数据和值,但是如果返回上一个突出显示并单击的行或单元格,Data and Values 中的任何先前更改将再次出现.

I want to load data and values tabs accordingly to number of rows. for example when user clicks and highlights a certain row inside the table the classes are loaded and any change in Data and Values for that row should remain, when user highlight another row then new Data and Value appears, but in case of return to previous highlighted and clicked row or cell any previous changes in Data and Values would appear again.

我想打印类似的东西.

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 中的一些特殊脚本来完成任务.我很感激任何帮助.谢谢.

I really do not know how to achieve that. It might be some special scripts in PyQT5 to do the task. I appreciate any help. Thanks.

推荐答案

如果我正确理解您的问题,您希望组"和组合"选项卡记住您为特定行设置的值并在您恢复这些值时选择不同的行后再次选择该行.在这种情况下,您可以执行以下操作(GroupCells.pyComboCells.py 与以前相同)

If I understand your question correctly you want the "Group" and "Combo" tabs to remember the values you set for a specific row and restore these values when you select the row again after having selected a different row. In that case you could do something like below (GroupCells.py and ComboCells.py are the same as before)

在 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 小部件,它们的数量等于表中的行数,并将这些小部件添加到 self.Tab 如果选择了相应的行.为了做到这一点,我将将数据添加到表格的责任从 TableCells 移到了 TabWidget 并替换了 TableCells.setTableWidget()有两种方法:TableCells.addRow()TabWidget.addRow().ComboCellsGroupCells 小部件在 TabWidget.addRow 中创建并添加到 self.combosself.groupcells,分别.然后将这些小部件添加和删除到 TabWidget.rowselected_tables() 中的 self.Tab.

In this example I create a number of ComboCells and GroupCells widget equal to the number of rows in the table, and add these widgets to self.Tab if the corresponding row is selected. In order to do this I've moved the responsibility of adding the data to the table from TableCells to TabWidget and replaced TableCells.setTableWidget() with two methods: TableCells.addRow() and TabWidget.addRow(). The ComboCells and GroupCells widgets are created in TabWidget.addRow and added to self.combos and self.groupcells, respectively. These widgets are then added and removed to self.Tab in TabWidget.rowselected_tables().

这篇关于根据 QTableWidget 的突出显示行加载不同的 QWidget的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆