問題描述
我有這個(gè)我還不能解決的任務(wù).使用 PyQt 和 Qt Creator.
我想將在 QT Creator 中創(chuàng)建的自定義小部件嵌入到另一個(gè) QMainWindow 中.
1) 我做的步驟:
在 QT creator 中創(chuàng)建一個(gè) Widget 文件:
2) 將其保存為 *.ui 并應(yīng)用此行將其轉(zhuǎn)換為 *.py 文件:
pyuic5 gen_settings.ui -o gen_settings.py
3) 打開它,看到它以
開頭從 PyQt5 導(dǎo)入 QtCore、QtGui、QtWidgets類 Ui_gen_settings(對(duì)象):def setupUi(self, gen_settings):gen_settings.setObjectName("gen_settings")
4) 當(dāng)然會(huì)導(dǎo)致函數(shù)調(diào)用:
TypeError:參數(shù)不匹配任何重載調(diào)用:addWidget(self, QWidget): 參數(shù) 1 具有意外類型函數(shù)"
當(dāng)我在另一個(gè) QMainWindow 文件中調(diào)用它時(shí):
類 Ui_MainWindow(object):def setupUi(self, MainWindow, My_Custom_widget):MainWindow.setObjectName("MainWindow")self.gridLayout.addWidget(My_Custom_widget, 1, 4, 1, 1)
任何想法如何解決它?
首先我推薦你閱讀
圖片中,Widget容器是左上角的,現(xiàn)在我們將其替換為Gen_Settings,所以我們必須
- 將出現(xiàn)以下對(duì)話框并填寫如圖所示的字段(我假設(shè) gen_settings_ui.py 和 gen_settings.py 與當(dāng)前 .ui 位于同一文件夾中)
- 您按下添加按鈕,然后按下升級(jí)按鈕.
<小時(shí)>
然后你用 pyuic 將 .ui 轉(zhuǎn)換為 .py ,你會(huì)得到以下結(jié)果:
從 PyQt5 導(dǎo)入 QtCore、QtGui、QtWidgets類 Ui_MainWindow(對(duì)象):def setupUi(self, MainWindow):MainWindow.setObjectName("MainWindow")# ...self.widget = Gen_Settings(self.centralwidget)self.widget.setObjectName("widget")self.gridLayout.addWidget(self.widget, 0, 0, 1, 1)# ...從 gen_settings 導(dǎo)入 Gen_Settings
I have this task that I couldn't solve yet. Working with PyQt and Qt Creator.
I want to embed a custom created widget created in QT Creator into another QMainWindow.
1) Steps I do:
Create a Widget file in QT creator:
2) Save it as *.ui and apply this line to convert it to a *.py file:
pyuic5 gen_settings.ui -o gen_settings.py
3) Open it and see that it starts with
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_gen_settings(object):
def setupUi(self, gen_settings):
gen_settings.setObjectName("gen_settings")
4) Which results in function call of course:
TypeError: arguments did not match any overloaded call:
addWidget(self, QWidget): argument 1 has unexpected type 'function'
when I call it in another QMainWindow file:
class Ui_MainWindow(object):
def setupUi(self, MainWindow, My_Custom_widget):
MainWindow.setObjectName("MainWindow")
self.gridLayout.addWidget(My_Custom_widget, 1, 4, 1, 1)
Any ideas how to solve it?
First of all I recommend you read the PyQt docs referring to Qt Designer.
Going to the problem, Qt Designer does not provide a widget but a class that serves as an interface to a widget, and that can be seen in his statement:
class Ui_gen_settings(object):
# ...
The class inherits from object and not from QWidget, QDialog, QMainWindow, etc.
In the docs that indicate initially it is recommended to create a widget and use the interface provided by Qt Designer. For this it is correct to use pyuic but I will change the gen_settings.py to gen_settings_ui.py so that the change is understood.
pyuic5 gen_settings.ui -o gen_settings_ui.py
So now we create a file called gen_settings.py that contains the widget and use the interface.
gen_settings.py
from gen_settings_ui import Ui_gen_settings
from PyQt5 import QtWidgets
class Gen_Settings(QtWidgets.QWidget, Ui_gen_settings):
def __init__(self, parent=None):
super(Gen_Settings, self).__init__(parent)
self.setupUi(self)
Then when you create the .ui corresponding to Ui_MainWindow you add a QWidget that is an empty container.
In the image, the Widget container is the one in the upper left, and now we will replace it with Gen_Settings, so we must promote it using the following procedure:
- Right click on the widget container and select the
Promote To ...
option.
- The following Dialog will appear and fill in the fields as shown in the image (I'm assuming that gen_settings_ui.py and gen_settings.py are in the same folder as the current .ui)
- You press the Add button and then the Promote button.
Then you convert the .ui to .py with pyuic and you will get the following:
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
# ...
self.widget = Gen_Settings(self.centralwidget)
self.widget.setObjectName("widget")
self.gridLayout.addWidget(self.widget, 0, 0, 1, 1)
# ...
from gen_settings import Gen_Settings
這篇關(guān)于創(chuàng)建要嵌入到 QMainWindow 中的小部件的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!