import sys
from PyQt5 import QtCore as qtc
from PyQt5 import QtWidgets as qtw
from PyQt5 import QtGui as qtg
import os.path
import pandas as pd
class a(qtw.QStyledItemDelegate):
def createEditor(self, parent, option, index):
line_edit = qtw.QLineEdit(parent)
line_edit.setMaxLength(3)
return line_edit
class ColorDelegate(qtw.QStyledItemDelegate):
def initStyleOption(self, option, index):
super().initStyleOption(option, index)
if option.text.strip(): # condition
option.backgroundBrush = qtg.QColor("red")
class MainWindow(qtw.QMainWindow):
def __init__(self):
super().__init__()
##Main framwork
self.createUI()
def createUI(self):
self.resize(qtc.QSize(1200, 800))
base_widget = qtw.QWidget()
base_widget.setLayout(qtw.QHBoxLayout())
notebook = qtw.QVBoxLayout()
base_widget.layout().addLayout(notebook)
self.file_list = qtw.QVBoxLayout()
notebook.setSpacing(10)
notebook.setContentsMargins(0, 0, 0, 0)
base_widget.layout().addLayout(self.file_list)
spacerItem = qtw.QSpacerItem(20, 245, qtw.QSizePolicy.Minimum, qtw.QSizePolicy.Expanding)
base_widget.layout().addItem(spacerItem)
self.setCentralWidget(base_widget)
# self.Detailbar = qtw.QHBoxLayout()
self.Statusbar = qtw.QTableView()
self.stausbar_model = qtg.QStandardItemModel(self)
self.Statusbar.setModel(self.stausbar_model)
self.Statusbar.setFixedHeight(70)
self.Statusbar.setEditTriggers(qtw.QAbstractItemView.NoEditTriggers)
notebook.addWidget(self.Statusbar)
# CREATE THE TABLE
self.tableWidget = qtw.QTableView(self) # SELECTING THE VIEW
self.tableWidget.setGeometry(0, 0, 100, 100)
self.model = qtg.QStandardItemModel(self)
self.model.setHorizontalHeaderLabels(['ID', 'Category', 'Time','Comment']) # SELECTING THE MODEL - FRAMEWORK THAT HANDLES QUERIES AND EDITS
self.tableWidget.setModel(self.model) # SETTING THE MODEL
self.tableWidget.setSelectionBehavior(qtw.QAbstractItemView.SelectRows)
self.Size = QTableWidgetDisabledItem(self.tableWidget)
self.tableWidget.setItemDelegateForColumn(0, self.Size)
self.tableWidget.setItemDelegateForColumn(1, self.Size)
self.tableWidget.setItemDelegateForColumn(2, self.Size)
self.tableWidget.doubleClicked.connect(self.on_click)
delegate = ColorDelegate(self.tableWidget)
# self.tableWidget.setItemDelegateForColumn(3, delegate)
self.tableWidget.setItemDelegate(delegate)
self.file_list.addWidget(self.tableWidget)
def populate(self):
# GENERATE A 4x10 GRID OF RANDOM NUMBERS.
# VALUES WILL CONTAIN A LIST OF INT.
# MODEL ONLY ACCEPTS STRINGS - MUST CONVERT.
csv_fname = os.path.splitext(self.fname)[0]
edited_csv_fname = csv_fname + ".csv"
self.data = pd.read_csv(edited_csv_fname)
values = []
for i in range(len(self.data.index)):
sub_values = []
for j in range(len(self.data.columns)):
value = self.data.iloc[i, j]
sub_values.append(value)
values.append(sub_values)
for value in values:
row = []
for item in value:
cell = qtg.QStandardItem(str(item))
row.append(cell)
self.model.appendRow(row)
qd = []
for k in range(len(self.data.index)):
qd.append(qtg.QStandardItem(str('')))
self.stausbar_model.appendRow(qd)
def flags(self, index):
flags = super(self.__class__, self).flags(index)
flags |= qtc.Qt.ItemIsEditable
flags |= qtc.Qt.ItemIsSelectable
flags |= qtc.Qt.ItemIsEnabled
flags |= qtc.Qt.ItemIsDragEnabled
flags |= qtc.Qt.ItemIsDropEnabled
return flags
class QTableWidgetDisabledItem(qtw.QItemDelegate):
def __init__(self, parent):
qtw.QItemDelegate.__init__(self, parent)
def createEditor(self, parent, option, index):
item = qtw.QLineEdit(parent)
item.setReadOnly(True)
#item.setEnabled(False)
return item
def setEditorData(self, editor, index):
editor.blockSignals(True)
editor.setText(index.model().data(index))
editor.blockSignals(False)
def setModelData(self, editor, model, index):
model.setData(index, editor.text())
if __name__ == '__main__':
app = qtw.QApplication(sys.argv) #it's required to save a referance to MainWindow
mw = MainWindow()
mw.show()
sys.exit(app.exec_())
#if it goes out of scope ,it will be destroyed
我有两个QTableview窗口小部件,第一个QtableView窗口小部件具有26行和4列,第四列仅可编辑,并且和,第二个Qtableviw窗口小部件具有1行和26列,如果我在第4列的第一个Qtablview中更改了背景颜色,则在第二个Qtableview中尊重背景颜色会改变。
在详细信息中,第一个Qtableview小部件具有26行和4列,第4个仅可编辑,而第二个QTableview小部件具有1行和26列,如果我在第一个QTableview中的单元格索引(10,3)中填充任何tex,则单元格颜色将改变,然后在第二个QTableview背景颜色中的单元格索引(0,10)中的相应值将更改。
最佳答案
您必须检测第二个QTableView的相应项目的更改,获取该项目的行,以便能够设置第一个QTableView的相应项目的背景色。
import sys
from PyQt5 import QtCore as qtc
from PyQt5 import QtWidgets as qtw
from PyQt5 import QtGui as qtg
class QTableWidgetDisabledItem(qtw.QItemDelegate):
def createEditor(self, parent, option, index):
return
class MainWindow(qtw.QMainWindow):
def __init__(self, parent=None):
super().__init__(parent)
self.createUI()
def createUI(self):
self.Statusbar = qtw.QTableView(
editTriggers=qtw.QAbstractItemView.NoEditTriggers
)
self.statusbar_model = qtg.QStandardItemModel(self)
self.Statusbar.setModel(self.statusbar_model)
self.Statusbar.setFixedHeight(70)
self.tableWidget = qtw.QTableView(
selectionBehavior=qtw.QAbstractItemView.SelectRows
)
self.tableWidget.setGeometry(0, 0, 100, 100)
self.model = qtg.QStandardItemModel(self)
self.model.setHorizontalHeaderLabels(["ID", "Category", "Time", "Comment"])
self.tableWidget.setModel(self.model)
for i in range(3):
delegate = QTableWidgetDisabledItem(self.tableWidget)
self.tableWidget.setItemDelegateForColumn(i, delegate)
self.model.itemChanged.connect(self.on_itemChanged)
self.resize(qtc.QSize(1200, 800))
base_widget = qtw.QWidget()
self.setCentralWidget(base_widget)
notebook = qtw.QVBoxLayout()
notebook.setSpacing(10)
notebook.setContentsMargins(0, 0, 0, 0)
notebook.addWidget(self.Statusbar, alignment=qtc.Qt.AlignTop)
lay = qtw.QHBoxLayout(base_widget)
lay.addLayout(notebook)
self.file_list = qtw.QVBoxLayout()
self.file_list.addWidget(self.tableWidget)
lay.addLayout(self.file_list)
self.populate()
def populate(self):
# emulate populate
self.model.setRowCount(26)
self.statusbar_model.setRowCount(1)
self.statusbar_model.setColumnCount(self.model.rowCount())
@qtc.pyqtSlot("QStandardItem*")
def on_itemChanged(self, item):
if item.column() == 3:
it = self.statusbar_model.item(0, item.row())
if it is None:
it = qtg.QStandardItem()
self.statusbar_model.setItem(0, item.row(), it)
brush = (
qtg.QBrush(qtg.QColor("red")) if item.text().strip() else qtg.QBrush()
)
it.setBackground(brush)
if __name__ == "__main__":
app = qtw.QApplication(sys.argv)
mw = MainWindow()
mw.show()
sys.exit(app.exec_())
关于python - 如何连接两个QTableview小部件?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/58831981/