Reputation: 55
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
I have two QTableview Widgets, first QtableView widgets have 26 row and 4 column and fourth column is only editable and and , second Qtableviw widgets have 1 row and 26 column, If i chnanged in first Qtablview in 4th column the background colur would change and with respect in second Qtableview Background color would change.
In Details first Qtableview widgets have 26 row and 4 column and 4th one is only editable and second QTableview widgets have 1 row and 26 column,if i fill any tex in cell index (10,3) in first QTableview then cell colur will changes and with respective then in cell index (0,10) in second QTableview background colur would changed.
Upvotes: 1
Views: 469
Reputation: 244282
You have to detect the change of the corresponding item of the second QTableView, get the row of that item to be able to set the background color of the corresponding item of the first 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_())
Upvotes: 1