具有一个QTableWidgets
,其中包含表列表以及两个附加类Combocells
和Groupcells
。它们显示为QTabWidget
。我想单击并突出显示表中的行或单元格,然后根据表的行刷新两个类Combocells
和Groupcells
并加载到QTabWidget
中。
文件的结构。
- Main.py
- Tablecells.py
- Combocells.py
- Groupcells.py
可视化
更新:
我已经更新了代码,信号和插槽捕获行号并发送了。 尽管点击了单元格,
Data and Value
选项卡仍添加到选项卡中, 选择row 0
时删除。我没有得到的是当row 1
是 选中,Data and Value
标签会添加到小部件中,combobox
为Qlineedit
和row 2
在数据和值中以查看 点击后,Data and Value
确实会从一开始就真正添加到Tabwidget中。 我注意到它没有更新。我已经尝试过addTab
和insertTab
使用self.update()
时,仍然无法以我想要的方式进行处理 做。有人知道如何解决此问题吗? 查看这段有问题的脚本。
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
的数量加载数据和值标签。例如,当用户单击并突出显示表中的某一行时,将加载类,并且应保留该行在Data and Values
中所做的任何更改;当用户突出显示另一行时,则会显示新的数据和值,但是如果返回到先前突出显示并单击的行或单元格,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中的一些特殊脚本来完成任务。感谢您的帮助。谢谢。
答案 0 :(得分:1)
如果我正确理解了您的问题,则希望“组”和“组合”选项卡记住您为特定行设置的值,并在选择其他行后再次选择该行时恢复这些值。在这种情况下,您可以执行以下操作(GroupCells.py
和ComboCells.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)
在此示例中,我创建了数量ComboCells
和GroupCells
的小部件,它们等于表中的行数,如果选择了相应的行,则将这些小部件添加到self.Tab
。为此,我将将数据添加到表中的职责从TableCells
移到了TabWidget
,并用两种方法替换了TableCells.setTableWidget()
:TableCells.addRow()
和{{1 }}。 TabWidget.addRow()
和ComboCells
小部件在GroupCells
中创建,并分别添加到TabWidget.addRow
和self.combos
中。然后将这些小部件添加到self.groupcells
中的self.Tab
中并从中删除。