2017-12-19 76 views
0

項目を選択したときに、選択色を変えたいと思います。しかし、QTableWidget::item:selected{ background-color: }は、選択されたアイテムが1つだけの場合にのみ機能します。そうでない場合、選択されたアイテムの選択色はすべて同じになります。それで、すべての商品に個別の選択色を付ける方法はありますか?PyQt5でQTableWidgetの各項目の選択色を設定するには

from PyQt5 import QtCore, QtGui, QtWidgets 

class Ui_MainWindow(object): 
    def setupUi(self, MainWindow): 
     self.MainWindow=MainWindow 
     self.MainWindow.resize(300, 100) 
     self.centralwidget = QtWidgets.QWidget(self.MainWindow) 
     self.MainWindow.setCentralWidget(self.centralwidget) 
     """table """ 
     self.tableWidget = QtWidgets.QTableWidget(self.centralwidget) 
     self.tableWidget.insertRow(0) 
     self.tableWidget.setColumnCount(2) 

     self.tableWidget.setItem(0,0,QtWidgets.QTableWidgetItem("red")) 
     self.tableWidget.setItem(0,1,QtWidgets.QTableWidgetItem("blue")) 
     self.tableWidget.itemSelectionChanged.connect(self.ChangeSelectionColor) 
     QtCore.QMetaObject.connectSlotsByName(MainWindow) 
    def ChangeSelectionColor(self): 
     try: 
      for item in self.tableWidget.selectedItems(): 
       col=item.column() 
      self.tableWidget.setStyleSheet("QTableWidget::item:selected{ background-color: %s }"%color_list[col]) 
     except UnboundLocalError: 
      pass 
if __name__ == "__main__": 
    import sys 
    color_list=['red','blue'] 
    app = QtWidgets.QApplication(sys.argv) 
    MainWindow = QtWidgets.QMainWindow() 
    ui = Ui_MainWindow() 
    ui.setupUi(MainWindow) 
    MainWindow.show() 
    sys.exit(app.exec_()) 

enter image description here

enter image description here

一つの選択が良い作品。

enter image description here

複数選択だけで選択したすべての項目に色を適用します。私は、一度選択された左側のものを赤色にしたい。

+0

色はどのように変わるのですか? – eyllanesc

+0

@eyllanesc 'main'で定義された' color_list'です。 column = iの場合、その選択色は 'color_list [i]'でなければなりません。 – user6456568

+0

私はあなたが私の質問を理解していないと思う、私はあなたが2行3列のテーブルを持って、どのような色になると言う、各セルは、それが選択されている必要がありますか? – eyllanesc

答えて

1

qssは多くの制限があるため適切ではないため、デリゲート(この場合はQStyledItemDelegateを継承するクラス)を実装することが適切です。しかし、その前に、私たちはQTableWidgetItemのsetDataメソッドを通じて色情報を保存する必要があります。

it = QTableWidgetItem("some_text") 
it.setData(Qt.UserRole, some_color) 

その後QStyledItemDelegateのpaintメソッドが上書きされ、選択色を変更する:

class ColorDelegate(QStyledItemDelegate): 
    def paint(self, painter, option, index): 
     color = index.data(Qt.UserRole) 
     option.palette.setColor(QPalette.Highlight, color) 
     QStyledItemDelegate.paint(self, painter, option, index) 

はその後、デリゲートが確立されます:

your_qtablewidget.setItemDelegate(ColorDelegate()) 

完全な例私は以下のことを示しています。

from PyQt5.QtWidgets import QApplication, QStyledItemDelegate, QTableWidget, QTableWidgetItem, QStyle 
from PyQt5.QtGui import QColor, QPalette 
from PyQt5.QtCore import qrand, Qt 

class ColorDelegate(QStyledItemDelegate): 
    def paint(self, painter, option, index): 
     color = index.data(Qt.UserRole) 
     option.palette.setColor(QPalette.Highlight, color) 
     QStyledItemDelegate.paint(self, painter, option, index) 


def fun(n_rows, n_columns): 
    return [[QColor(qrand() % 256, qrand() % 256, qrand() % 256) for i in range(n_rows)] for j in range(n_columns)] 

if __name__ == '__main__': 
    import sys 

    app = QApplication(sys.argv) 
    n_rows, n_columns = 10, 10 
    colors = fun(n_rows, n_columns) 
    w = QTableWidget() 
    w.setColumnCount(n_columns) 
    w.setRowCount(n_columns) 
    for i in range(w.rowCount()): 
     for j in range(w.columnCount()): 
      it = QTableWidgetItem("{}-{}".format(i, j)) 
      it.setData(Qt.UserRole, colors[i][j]) 
      w.setItem(i, j, it) 
    w.setItemDelegate(ColorDelegate()) 
    w.show() 
    sys.exit(app.exec_()) 
+0

ありがとうございました。あなたは最高です! – user6456568

+0

私はまだ混乱しています。選択したフォントは自動的に白に変わります。これを避け、フォントの色を変えないでください。 – user6456568

関連する問題