简体   繁体   中英

How to change background color of selected or clicked cell in a pyqt5 QTableWidget?

I am new to pyqt. I am trying to change the background color of selected cell in a QTableWidget. Is it possible to change color when clicked/selected and return to default color when not selected.

Also the property cellClicked is returning only row number. How to get both row and column number?

from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtWidgets import QApplication, QWidget, QTableWidget, QTableWidgetItem, QVBoxLayout


class Ui_Form(object):
    def setupUi(self, Form):
        Form.setObjectName("Form")
        Form.resize(1124, 801)
        self.tableWidget = QtWidgets.QTableWidget(Form)
        self.tableWidget.setGeometry(QtCore.QRect(30, 40, 1000, 600))
        self.tableWidget.setRowCount(15)
        self.tableWidget.setColumnCount(6)
        self.tableWidget.setAlternatingRowColors(True)
        self.tableWidget.setObjectName("tableWidget")
        for i, col_name in enumerate(['symbol', 'step', 'factor', 'initial_lot', 'max_streak', 'status']):
            self.tableWidget.setHorizontalHeaderItem(i, QTableWidgetItem(col_name))
        self.tableWidget.verticalHeader().setVisible(False)
        self.tableWidget.verticalHeader().setMinimumSectionSize(20)
        self.tableWidget.horizontalHeader().setStyleSheet(
            "QHeaderView::section{"
            "border-bottom: 1px solid #4a4848;"
            "background-color:white;"
        "}")

        self.tableWidget.cellClicked.connect(lambda x: self.print_selected(x))
        self.retranslateUi(Form)
        QtCore.QMetaObject.connectSlotsByName(Form)

    def retranslateUi(self, Form):
        _translate = QtCore.QCoreApplication.translate
        Form.setWindowTitle(_translate("Form", "Form"))

    def print_selected(self, s):
        print(s)


if __name__ == "__main__":
    import sys
    app = QtWidgets.QApplication(sys.argv)
    Form = QtWidgets.QWidget()
    ui = Ui_Form()
    ui.setupUi(Form)
    Form.show()
    sys.exit(app.exec_())

python 3.7.6, pyqt5

use QPalette.Highlight

pallete = self.tableWidget.palette()
hightlight_brush = pallete.brush(QPalette.Highlight)
hightlight_brush.setColor(QColor('red'))
pallete.setBrush(QPalette.Highlight, hightlight_brush)
self.tableWidget.setPalette(pallete)

cellClicked signal passes both row, and column:

void QTableWidget::cellClicked(int row, int column)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM