关于qt, 系统分辨率自适应

当,Qt 界面运行起来后, 更改系统分辨率, Qt的窗体,不会自动适应,调整自身大小.
可以通过如下方法解决,此问题.

查看Q文档, QDesktopWidget 类, 当屏幕分辨率改变,会发送一个 resize(int) 信号.
这里写图片描述

示例代码如下:

#!/usr/bin/env python
# encoding: utf-8

from PyQt4.QtGui import QApplication
from PyQt4.QtGui import QPushButton
from PyQt4.QtCore import SIGNAL

class PushButton(QPushButton):
    def __init__(self,parent=None):
        super(PushButton, self).__init__(None)

        self.__desktop = QApplication.desktop()
        self.__reset_size()
        self.connect( self.__desktop,SIGNAL("resized(int)"),self.desktop_resize_test)

    def __reset_size(self):
        qRect = self.__desktop.screenGeometry()        #设备屏幕尺寸
        self.resize(qRect.width()/3, qRect.height()/3)
        self.move(qRect.width()/3, qRect.height()/3)
        print "reset size : ", self.size()

    def desktop_resize_test(self,screen):
        print  "------- desktop_resize_test -------"
        print "screen : ", screen
        desktopRect =  self.__desktop.availableGeometry() #获取可用桌面大小:
        print "可用桌面大小:", desktopRect

        screenRect =  self.__desktop.screenGeometry()   #获取设备屏幕大小
        print "设备屏幕大小:" ,screenRect
        self.__reset_size()
        print  "------------------------------"

if __name__ == '__main__':
    import sys
    app = QApplication(sys.argv)
    button = PushButton()
    button.show()
    app.exec_()

验证:
将分辨率由 原来的1440x900 => 1360x768,
xrandr -s 1360x768
然后,再由 1360x768 => 1440x900 ;
结果如图:
关于qt, 系统分辨率自适应_第1张图片

你可能感兴趣的:(pyqt,Qt)