PyQt5 Method for Font Size Adaptive Resolution

  • 2021-06-28 13:15:35
  • OfStack

Recently, we encountered a phenomenon that running good software on a higher resolution computer will result in incomplete font display and blocked by controls.Specific reasons can be queried online, where solutions will be recorded.

Two methods are documented here, and High_can be supported if the Qt version used is after 5.6.0DPI, the property can be set by application;If not, you can set an adaptive font for your application.


 if __name__ == "__main__":
  v_compare = QVersionNumber(5,6,0)
  v_current,_ = QVersionNumber.fromString(QT_VERSION_STR) # Get Current Qt Edition 
  if QVersionNumber.compare(v_current,v_compare) >=0 :
    QApplication.setAttribute(Qt.AA_EnableHighDpiScaling)  #Qt from 5.6.0 Start, support High-DPI
    app = QApplication(sys.argv)  #
  else:
    app = QApplication(sys.argv)
    font = QFont(" Song Style ")
    pointsize = font.pointSize()
    font.setPixelSize(pointsize*90/72)
    app.setFont(font)
  mymainwin = Mymainwindow()
  mymainwin.show()
  sys.exit(app.exec())

Note: QApplication.setAttribute (Qt.AA_EnableHighDpiScaling) was before the QApplication project was created.


Related articles: