簡體   English   中英

如何在 Pyqt5 中設置 QTableWidget 的樣式

[英]How to style a QTableWidget in Pyqt5

我目前正在為我的 GUI 應用程序設計樣式,但我在設置 QTableWidget 的樣式時遇到了一些問題,因為正如您在圖片中看到的那樣,我的 QTableWidget 的左上角有一個小白色區域,我希望它是淺藍色的rest 表。

圖形用戶界面圖片

這是我到目前為止的樣式代碼:

def start_ui():
    app = QtWidgets.QApplication(sys.argv)
    style = """
        QFrame{
            color: blue;
            background: lightgreen;
            font-weight: bold;
        }
        QLabel{
            color: blue;
            font-weight: bold;
        }
        QLineEdit{
            color: blue;
            border-style: solid;
            border: 2px solid black;
            background: lightblue;
            font-weight: bold;
        }
        QPushButton{
            border-style: solid;
            border: 2px solid black;
            color: darkblue;
            background: lightblue;
            font-weight: bold;
        }
        QTableWidget{
            background: lightblue;
        }
    """
    app.setStyleSheet(style)
    win = ModbusLoggerWindow()
    win.show()
    sys.exit(app.exec_())

然后我在 class 中有這三行,在那里我創建了我的 QTableWidget 和這些行 styles 我的垂直標題和水平標題:

    stylesheetHeader = "::section{background-color: lightblue}"
    self.tableWidget.horizontalHeader().setStyleSheet(stylesheetHeader)
    self.tableWidget.verticalHeader().setStyleSheet(stylesheetHeader)

左上角的這個元素不是 QHeaderView 的一部分,而是 QAbstractButton 所以一個可能的解決方案是將樣式表直接應用於按鈕:

button = self.tableWidget.findChild(QAbstractButton)
button.setStyleSheet("background-color: lightblue")

為了避免使用 findChild,可以通過 QTableWidget 進行設置:

stylesheet = """
QHeaderView::section{background-color: lightblue}
QAbstractButton{background-color: lightblue}
"""
self.tableWidget.setStyleSheet(stylesheet)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM