*.ui
文件的修改:Windows下安装pyqt5-tools
,用QT designer打开
- 在设计的GUI界面选中Widget后在Object Inspector中可以看到相应的项目也被选中,右键弹出菜单选Promote Widgest
- 添加对应的类名(如RangeSlider)
- 添加头文件名称(若引用的为Python脚本,则删掉后缀,如
range_slider
),这样相当于from range_slider import RangeSlider
- 使Promote生效
- 此时在
*.ui
中修改的界面元素的widge class应该已经改为了”RangeSlider”
- 并且在
</widget>
和<resources/>
之间增加了
<customwidgets>
<customwidget>
<class>RangeSlider</class>
<extends>QSlider</extends>
<header>range_slider</header>
</customwidget>
</customwidgets>
改变窗体大小事件 §
- 参考Detect resizing in Widget-window resized signal,代码示例
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.setWindowTitle("MainWindow")
MainWindow.resize(200, 200)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
MainWindow.setCentralWidget(self.centralwidget)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
class Window(QtWidgets.QMainWindow):
resized = QtCore.pyqtSignal()
def __init__(self, parent=None):
super(Window, self).__init__(parent=parent)
ui = Ui_MainWindow()
ui.setupUi(self)
self.resized.connect(self.onWindowSizeChanged)
def resizeEvent(self, event):
self.resized.emit()
return super(Window, self).resizeEvent(event)
def onWindowSizeChanged(self):
print("the size of the window has been changed!")
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
w = Window()
w.show()
sys.exit(app.exec_())
- 注意
resized
是类属性,不能放在__init__
中定义,否则会报AttributeError: 'PyQt5.QtCore.pyqtSignal' object has no attribute 'connect'
的错误