如何连接两个 QTableview Widgets?
how to connect two QTableview Widgets?
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 列,第四列只能编辑和和,第二个 Qtableview 小部件有 1 行和 26 列,如果我在第 4 列的第一个 Qtablview 中更改背景颜色会改变,并且在第二个 Qtableview 背景颜色会改变。
在详细信息中,第一个 Qtableview 小部件有 26 行和 4 列,第 4 个只能编辑,第二个 QTableview 小部件有 1 行和 26 列,如果我在第一个 QTableview 中的单元格索引 (10,3) 中填充任何 tex 然后单元格颜色将发生变化,然后在第二个 QTableview 背景颜色中的单元格索引 (0,10) 将发生变化。
你必须检测到第二个QTableView对应item的变化,得到那个item的行才能设置第一个QTableView对应item的背景色。
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_())
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 列,第四列只能编辑和和,第二个 Qtableview 小部件有 1 行和 26 列,如果我在第 4 列的第一个 Qtablview 中更改背景颜色会改变,并且在第二个 Qtableview 背景颜色会改变。
在详细信息中,第一个 Qtableview 小部件有 26 行和 4 列,第 4 个只能编辑,第二个 QTableview 小部件有 1 行和 26 列,如果我在第一个 QTableview 中的单元格索引 (10,3) 中填充任何 tex 然后单元格颜色将发生变化,然后在第二个 QTableview 背景颜色中的单元格索引 (0,10) 将发生变化。
你必须检测到第二个QTableView对应item的变化,得到那个item的行才能设置第一个QTableView对应item的背景色。
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_())