我已经看了先前的问题“以编程方式访问QT Designer对象”的答案。这是我要问的问题,但是这个答案只是重新编码了窗口,而没有引用pyuic生成的模块。因此,可以说在qt-designer,其pyuic文件中创建一个空白窗口,然后操纵该窗口,从您自己的python类添加小部件等。如果有可能,有人可以给我指出一个例子吗?谢谢
我的.ui
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>mainWindow</class>
<widget class="QMainWindow" name="mainWindow">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>800</width>
<height>600</height>
</rect>
</property>
<property name="windowTitle">
<string/>
</property>
<widget class="QWidget" name="centralwidget">
<widget class="QLabel" name="label">
<property name="geometry">
<rect>
<x>80</x>
<y>60</y>
<width>161</width>
<height>51</height>
</rect>
</property>
<property name="frameShape">
<enum>QFrame::Box</enum>
</property>
<property name="lineWidth">
<number>3</number>
</property>
<property name="text">
<string>TextLabel</string>
</property>
</widget>
</widget>
<widget class="QMenuBar" name="menubar">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>800</width>
<height>26</height>
</rect>
</property>
</widget>
<widget class="QStatusBar" name="statusbar"/>
</widget>
<resources/>
<connections/>
</ui>
from mydesign import Ui_mainWindow
class mywindow(QtWidgets.QMainWindow):
def __init__(self):
super(mywindow, self).__init__()
self.ui = Ui_mainWindow()
self.ui.setupUi(self)
pixmap = QPixmap('image.jpeg')
pixmap = pixmap.scaledToHeight(500)
self.ui.label.setPixmap(pixmap)
self.resize(pixmap.width(),pixmap.height())
pixmap = pixmap.scaledToWidth(64)
#I want to add this widget to window in Ui_mainWindow - Is this possible?
#This code produces a separate SVG window.
self.ui.svgWidget = QSvgWidget('GT.svg')
self.ui.svgWidget.setGeometry(0,0,730,300)
self.ui.svgWidget.show()
app = QtWidgets.QApplication([])
application = mywindow()
application.show()
sys.exit(app.exec())
答案 0 :(得分:1)
要添加内容,您必须知道要添加到哪里,假设.ui是一个空设计:
*。ui
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>MainWindow</class>
<widget class="QMainWindow" name="MainWindow">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>800</width>
<height>600</height>
</rect>
</property>
<property name="windowTitle">
<string>MainWindow</string>
</property>
<widget class="QWidget" name="centralwidget"/>
<widget class="QMenuBar" name="menubar">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>800</width>
<height>23</height>
</rect>
</property>
</widget>
<widget class="QStatusBar" name="statusbar"/>
</widget>
<resources/>
<connections/>
</ui>
然后在pyuic的帮助下将其转换为.py:
pyuic5 foo_filename.ui -o design.py -x
考虑到上述情况,没有必要向Ui_mainWindow添加任何内容,因为它不是窗口小部件,它是用于填充窗口小部件的类,因此您应该将其添加到窗口中。在这种情况下,因为它是QMainWindow,所以必须使用centralwidget:
from PyQt5 import QtCore, QtGui, QtWidgets, QtSvg
from design import Ui_MainWindow
class MainWindow(QtWidgets.QMainWindow, Ui_MainWindow):
def __init__(self, parent=None):
super(MainWindow, self).__init__(parent)
self.setupUi(self)
self.label = QtWidgets.QLabel()
pixmap = QtGui.QPixmap("image.jpeg")
pixmap = pixmap.scaledToHeight(500)
self.label.setPixmap(pixmap)
self.svgWidget = QtSvg.QSvgWidget("GT.svg")
lay = QtWidgets.QVBoxLayout(self.centralWidget())
lay.addWidget(self.label)
lay.addWidget(self.svgWidget)
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
w = MainWindow()
w.show()
sys.exit(app.exec_())
因为您提供了.ui,所以修改了解决方案。要在窗口中显示窗口小部件,它必须是窗口的子窗口或窗口中的子窗口,在这种情况下,使用的是中央窗口小部件。
from PyQt5 import QtCore, QtGui, QtWidgets, QtSvg
from design import Ui_mainWindow
class MainWindow(QtWidgets.QMainWindow, Ui_mainWindow):
def __init__(self, parent=None):
super(MainWindow, self).__init__(parent)
self.setupUi(self)
pixmap = QtGui.QPixmap("image.jpeg")
pixmap = pixmap.scaledToHeight(500)
self.label.setPixmap(pixmap)
self.svgWidget = QtSvg.QSvgWidget("GT.svg", parent=self.centralWidget())
self.svgWidget.setGeometry(0, 0, 730, 300)
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
w = MainWindow()
w.show()
sys.exit(app.exec_())