PyQT5之設定視窗控制元件風格

星空28發表於2024-06-09

設定視窗控制元件風格

QApplication.setStyle(...)
視窗可以顯示三種風格:['windowsvista', 'Windows', 'Fusion']


import sys
from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *

# print(QStyleFactory.keys())


class WindowStyle(QWidget):
    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.resize(400, 100)
        self.setWindowTitle("設定視窗風格")

        self.styleLabel = QLabel("設定視窗風格:")
        self.styleComboBox = QComboBox()
        self.styleComboBox.addItems(QStyleFactory.keys())

        # 獲取當前視窗風格
        print(QApplication.style().objectName())
        index = self.styleComboBox.findText(QApplication.style().objectName(), Qt.MatchFixedString)

        self.styleComboBox.setCurrentIndex(index)
        self.styleComboBox.activated[str].connect(self.handleStyleChanged)

        horizontaLayout = QHBoxLayout()
        horizontaLayout.addWidget(self.styleLabel)
        horizontaLayout.addWidget(self.styleComboBox)
        self.setLayout(horizontaLayout)

    def handleStyleChanged(self, style):
        QApplication.setStyle(style)


if __name__ == "__main__":
    app = QApplication(sys.argv)

    main = WindowStyle()
    main.show()

    sys.exit(app.exec_())

相關文章