Issue
I have a Table in Pyqt5 with Combobox. My Combobox have 2 values: True or False, I want to Change Color of Combobox in 2 different Color: when value True -> Green, when value False -> Red. I tried to Change Color with Stylesheet but it can only change the whole Combobox into either Red or Green like below:
how can I Change Combobox in 2 different Colors which depends on their values?
My Code is below:
# -*- coding: utf-8 -*-
"""
Created on Tue Oct 27 09:21:24 2020
@author: actnmk
"""
import sys
from PyQt5.QtWidgets import (QWidget, QLabel, QLineEdit, QTextEdit, QGridLayout, QApplication)
import pandas as pd
import numpy as np
import PyQt5
from PyQt5.QtWidgets import QVBoxLayout, QPushButton, QGroupBox, QHBoxLayout, QMainWindow, QApplication, QLineEdit, QFileDialog, QTableWidget,QTableWidgetItem, QTableView, QStyledItemDelegate
from PyQt5 import QtCore, QtGui, QtWidgets
import os
from PyQt5.QtWidgets import (QWidget, QPushButton, QHBoxLayout, QVBoxLayout, QApplication)
from PyQt5.QtGui import QIcon
import re
def dataframe():
lst = [['tom', 'reacher', 'True'], ['krish', 'pete', 'True'],
['nick', 'wilson', 'True'], ['juli', 'williams', 'True']]
df = pd.DataFrame(lst, columns =['FName', 'LName', 'Student?'], dtype = float)
return df
class Delegate(QtWidgets.QItemDelegate):
def __init__(self, owner, choices):
super().__init__(owner)
self.items = choices
def createEditor(self, parent, option, index):
self.editor = QtWidgets.QComboBox(parent)
self.editor.currentIndexChanged.connect(self.commit_editor)
self.editor.addItems(self.items)
return self.editor
def paint(self, painter, option, index):
value = index.data(QtCore.Qt.DisplayRole)
style = QtWidgets.QApplication.style()
opt = QtWidgets.QStyleOptionComboBox()
opt.text = str(value)
opt.rect = option.rect
style.drawComplexControl(QtWidgets.QStyle.CC_ComboBox, opt, painter)
QtWidgets.QItemDelegate.paint(self, painter, option, index)
def commit_editor(self): ####test
editor = self.sender()
self.commitData.emit(editor)
def setEditorData(self, editor, index):
value = index.data(QtCore.Qt.DisplayRole)
num = self.items.index(value)
editor.setCurrentIndex(num)
def setModelData(self, editor, model, index):
value = editor.currentText()
model.setData(index, value, QtCore.Qt.EditRole)
def updateEditorGeometry(self, editor, option, index):
editor.setGeometry(option.rect)
class PandasModel(QtCore.QAbstractTableModel):
def __init__(self, data, parent=None):
QtCore.QAbstractTableModel.__init__(self, parent)
self._data = data
def rowCount(self, parent=None):
return self._data.shape[0]
def columnCount(self, parent=None):
return self._data.shape[2]
def flags(self, index):
return QtCore.Qt.ItemIsEditable | QtCore.Qt.ItemIsEnabled | QtCore.Qt.ItemIsSelectable
def data(self, index, role=QtCore.Qt.DisplayRole):
if index.isValid():
if role == QtCore.Qt.DisplayRole:
return str(self._data.iloc[index.row(), index.column()])
return None
def headerData(self, col, orientation, role):
if orientation == QtCore.Qt.Horizontal and role == QtCore.Qt.DisplayRole:
return self._data.columns[col]
return None
def setData(self, index, value, role=QtCore.Qt.EditRole):
self._data.iloc[index.row(), index.column()] = value
return True
class MyWindow(QWidget):
def __init__(self):
super().__init__()
self.setGeometry(300, 200 ,600, 400)
self.setWindowTitle('Test')
self.initUI()
def show_data(self):
choices = ['True', 'False']
self.model = PandasModel(dataframe())
self.table_data.setModel(self.model)
self.table_data.setItemDelegateForColumn(2, Delegate(self,choices))
##make combo boxes editable with a single-click:
for row in range(5):
self.table_data.openPersistentEditor(self.model.index(row, 2))
def print_data(self):
print(self.table_data.model()._data)
def initUI(self):
welcom = QLabel('Welcome to my app!')
self.btn_print_data = QPushButton('print data')
self.btn_print_data.clicked.connect(self.print_data) ##test
self.btn_show_table = QPushButton('show data')
self.btn_show_table.clicked.connect(self.show_data)
self.table_data = QTableView()
#self.table_result = QTableView()
hbox1 = QHBoxLayout()
hbox1.addWidget(welcom)
vbox2 = QVBoxLayout()
vbox2.addWidget(self.btn_show_table)
vbox2.addWidget(self.btn_print_data) ####test
vbox3 = QVBoxLayout()
vbox3.addWidget(self.table_data)
#vbox3.addWidget(self.table_result)
hbox2 = QHBoxLayout()
hbox2.addLayout(vbox2)
hbox2.addLayout(vbox3)
vbox1 = QVBoxLayout()
vbox1.addLayout(hbox1)
vbox1.addLayout(hbox2)
self.setLayout(vbox1)
self.show()
style = '''
QComboBox
{
background-color : lightgreen;
}
'''
if __name__ == '__main__':
app = QApplication(sys.argv)
app.setStyleSheet(style)
ex = MyWindow()
sys.exit(app.exec_())
Solution
Note: There is an error in the code, you must change to:
def columnCount(self, parent=None):
return self._data.shape[1]
On the other hand, the solution is to change the Qt StyleSheet of each combobox depending on the selection:
def commit_editor(self):
editor = self.sender()
color = QtGui.QColor()
if editor.currentText() == "True":
color = QtGui.QColor("lightgreen")
elif editor.currentText() == "False":
color = QtGui.QColor("red")
qss = """QComboBox{background-color : %s;}""" % (color.name(),)
editor.setStyleSheet(qss)
self.commitData.emit(editor)
Answered By - eyllanesc
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.