728x90

PyQt에서 플랫폼 독립적인(Platform Independent) 스타일 적용하는 방법 (Fusion)

들어가며

  • 윈도우와 맥에서 표시되는 PyQt 프로그램의 모습은 다르다. 그 이유는 플랫폼에 영향을 받기 때문이다.
  • 이렇게 플랫폼에 영향을 받지 않고, OS에 상관없이 동일하게 PyQt 프로그램이 표현되도록 하는 방법이 있다.

 

방법

  • PyQt에서는 플랫폼 독립적(Platform Independent)스타일 플러그인인 @Fusion@을 제공한다.
  • 이 스타일 플러그인을 이용하여 스타일을 적용시키면 OS에 상관 없이 동일한 UI가 표시될 수 있도록 할 수 있다.
  • 이 스타일 플러그인을 사용하기 위해서는 다음과 같이 @QStyleFactory@ 모듈을 불러와야 한다.
from PyQt5.QtGui import QStyleFactory

 

QApplication에 스타일 설정하기

QApplication.setStyle(QStyleFactory.create("fusion"))

 

환경 변수나 명령줄 인수를 사용하여 스타일 설정하기

  • 다음과 같이 환경 변수나 명령줄 인수를 사용하여 스타일을 설정할 수 있다.
QApplication.setStyle("fusion")

 

예제 코드

from PyQt5.QtWidgets import QApplication, QMainWindow, QTabWidget
from PyQt5.QtGui import QStyleFactory
import sys

class MyMainWindow(QMainWindow):
    def __init__(self):
        super().__init__()
        self.tab_widget = QTabWidget()
        self.setCentralWidget(self.tab_widget)
        self.tab_widget.setStyleSheet("background-color: #F0F0F0;")
        
        # 사용할 스타일을 "fusion"으로 설정합니다.
        QApplication.setStyle(QStyleFactory.create("fusion"))
        # 또는 환경 변수나 명령줄 인수를 사용하여 스타일을 설정할 수 있습니다.
        # QApplication.setStyle("fusion")

if __name__ == "__main__":
    app = QApplication(sys.argv)
    main_window = MyMainWindow()
    main_window.show()
    sys.exit(app.exec_())

 

 

 

참고 사이트

 

Fusion Style | Qt Quick Controls 6.5.2

 

doc.qt.io

 

PyQT5 different view on OS and Linux

I created a python program that uses PyQT5, but looking at the displayed windows on Mac OS X and Linux then they differentiate quite a bit. Here is the MAC OS X window And here is the Linux vers...

stackoverflow.com

 

728x90