Qt 提供了很多关于获取窗体位置及显示区域大小的函数
代码:
1 from PyQt4.QtGui import * 2 from PyQt4.QtCore import * 3 import sys 4 5 class Geometry(QDialog): 6 7 def __init__(self, parent=None): 8 super(Geometry, self).__init__(parent) 9 10 self.setWindowTitle("Geometry") 11 12 label1 = QLabel("x0:") 13 label2 = QLabel("y0:") 14 label3 = QLabel("frameGeometry():") 15 label4 = QLabel("pos():") 16 label5 = QLabel("geometry():") 17 label6 = QLabel("width():") 18 label7 = QLabel("height():") 19 label8 = QLabel("rect():") 20 label9 = QLabel("size():") 21 22 self.xLabel = QLabel() 23 self.yLabel = QLabel() 24 self.frameGeoLabel = QLabel() 25 self.posLabel = QLabel() 26 self.geoLabel = QLabel() 27 self.widthLabel = QLabel() 28 self.heightLabel = QLabel() 29 self.rectLabel = QLabel() 30 self.sizeLabel = QLabel() 31 32 layout = QGridLayout() 33 layout.addWidget(label1, 0, 0) 34 layout.addWidget(self.xLabel, 0, 1) 35 layout.addWidget(label2, 1, 0) 36 layout.addWidget(self.yLabel, 1, 1) 37 layout.addWidget(label3, 2, 0) 38 layout.addWidget(self.frameGeoLabel, 2, 1) 39 layout.addWidget(label4, 3, 0) 40 layout.addWidget(self.posLabel, 3, 1) 41 layout.addWidget(label5, 4, 0) 42 layout.addWidget(self.geoLabel, 4, 1) 43 layout.addWidget(label6, 5, 0) 44 layout.addWidget(self.widthLabel, 5, 1) 45 layout.addWidget(label7, 6, 0) 46 layout.addWidget(self.heightLabel, 6, 1) 47 layout.addWidget(label8, 7, 0) 48 layout.addWidget(self.rectLabel, 7, 1) 49 layout.addWidget(label9, 8, 0) 50 layout.addWidget(self.sizeLabel, 8, 1) 51 52 self.setLayout(layout) 53 54 self.updateLabel() 55 #重定义 QWidget 的 moveEvent()和 resizeEvent()函数,分别响应对话框的移动事件和调整大小事件, 56 # 使得窗体在被移动或窗体大小发生改变时,能同步更新各函数结果的显示 57 def moveEvent(self, event): 58 self.updateLabel() 59 60 def resizeEvent(self, Event): 61 self.updateLabel() 62 # updateLabel()函数负责调用各个位置函数获得结果并显示 63 def updateLabel(self): 64 65 temp = QString() 66 67 self.xLabel.setText(temp.setNum(self.x())) 68 self.yLabel.setText(temp.setNum(self.y())) 69 self.frameGeoLabel.setText(temp.setNum(self.frameGeometry().x())+","+ 70 temp.setNum(self.frameGeometry().y())+","+ 71 temp.setNum(self.frameGeometry().width())+","+ 72 temp.setNum(self.frameGeometry().height())) 73 self.posLabel.setText(temp.setNum(self.pos().x())+","+ 74 temp.setNum(self.pos().y())) 75 self.geoLabel.setText(temp.setNum(self.geometry().x())+","+ 76 temp.setNum(self.geometry().y())+","+ 77 temp.setNum(self.geometry().width())+","+ 78 temp.setNum(self.geometry().height())) 79 self.widthLabel.setText(temp.setNum(self.width())) 80 self.heightLabel.setText((temp.setNum(self.height()))) 81 self.rectLabel.setText(temp.setNum(self.rect().x())+","+ 82 temp.setNum(self.rect().y())+","+ 83 temp.setNum(self.rect().width())+","+ 84 temp.setNum(self.rect().height())) 85 self.sizeLabel.setText(temp.setNum(self.size().width())+","+ 86 temp.setNum(self.size().height())) 87 88 89 app = QApplication(sys.argv) 90 form = Geometry() 91 form.show() 92 app.exec_()
时间: 2024-10-21 19:56:53