【发布时间】:2020-01-05 01:27:54
【问题描述】:
有一个QTableWidgets,其中包含一个表列表和两个附加类Combocells 和Groupcells。它们显示为QTabWidget。我想单击并突出显示表格中的一行或单元格,然后 Combocells 和 Groupcells 两个类根据表格行刷新并加载到 QTabWidget 中。
文件的结构。
- Main.py
- Tablecells.py
- Combocells.py
- Groupcells.py
可视化
更新:
我已更新代码、信号和槽捕获行号并发送。 尽管通过单击单元格
Data and Value选项卡添加到 tabwidgets 和 删除,选择row 0@。我没有得到的是row 1是 选中,Data and Value选项卡添加到小部件,进行一些更改combobox和Qlineedit在 Data 和 Value 中查看,当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 中的一些特殊脚本来完成任务。我很感激任何帮助。谢谢。
【问题讨论】:
-
抱歉,看了好几遍没看懂你要更新什么?例如:如果您单击索引为 1 的行的单元格,您希望在
Value选项卡中看到什么? -
@S.Nick。我想看到的是,当单击索引为 1 的行的单元格时,数据和值选项卡会记住值,例如类 3 的组合值更改为 0.30,在选择第 2 行的单元格、组合框中的值和数据中的其他值和值选项卡刷新并重置例如组合值到 0.10。希望我已经解释清楚了。
标签: python python-3.x pyqt5 qtablewidget qtabwidget