<tfoot id='mVtID'></tfoot><legend id='mVtID'><style id='mVtID'><dir id='mVtID'><q id='mVtID'></q></dir></style></legend>
  • <small id='mVtID'></small><noframes id='mVtID'>

  • <i id='mVtID'><tr id='mVtID'><dt id='mVtID'><q id='mVtID'><span id='mVtID'><b id='mVtID'><form id='mVtID'><ins id='mVtID'></ins><ul id='mVtID'></ul><sub id='mVtID'></sub></form><legend id='mVtID'></legend><bdo id='mVtID'><pre id='mVtID'><center id='mVtID'></center></pre></bdo></b><th id='mVtID'></th></span></q></dt></tr></i><div id='mVtID'><tfoot id='mVtID'></tfoot><dl id='mVtID'><fieldset id='mVtID'></fieldset></dl></div>
      <bdo id='mVtID'></bdo><ul id='mVtID'></ul>

      1. 创建要嵌入到 QMainWindow 中的小部件

        时间:2023-08-05

              <bdo id='irJSr'></bdo><ul id='irJSr'></ul>

              <tfoot id='irJSr'></tfoot>
              <legend id='irJSr'><style id='irJSr'><dir id='irJSr'><q id='irJSr'></q></dir></style></legend>

                <tbody id='irJSr'></tbody>
              <i id='irJSr'><tr id='irJSr'><dt id='irJSr'><q id='irJSr'><span id='irJSr'><b id='irJSr'><form id='irJSr'><ins id='irJSr'></ins><ul id='irJSr'></ul><sub id='irJSr'></sub></form><legend id='irJSr'></legend><bdo id='irJSr'><pre id='irJSr'><center id='irJSr'></center></pre></bdo></b><th id='irJSr'></th></span></q></dt></tr></i><div id='irJSr'><tfoot id='irJSr'></tfoot><dl id='irJSr'><fieldset id='irJSr'></fieldset></dl></div>

              <small id='irJSr'></small><noframes id='irJSr'>

                1. 本文介绍了创建要嵌入到 QMainWindow 中的小部件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  我有这个我还不能解决的任务.使用 PyQt 和 Qt Creator.

                  我想将在 QT Creator 中创建的自定义小部件嵌入到另一个 QMainWindow 中.

                  1) 我做的步骤:

                  在 QT creator 中创建一个 Widget 文件:

                  2) 将其保存为 *.ui 并应用此行将其转换为 *.py 文件:

                  pyuic5 gen_settings.ui -o gen_settings.py

                  3) 打开它,看到它以

                  开头

                  从 PyQt5 导入 QtCore、QtGui、QtWidgets类 Ui_gen_settings(对象):def setupUi(self, gen_settings):gen_settings.setObjectName("gen_settings")

                  4) 当然会导致函数调用:

                  TypeError:参数不匹配任何重载调用:addWidget(self, QWidget): 参数 1 具有意外类型函数"

                  当我在另一个 QMainWindow 文件中调用它时:

                  类 Ui_MainWindow(object):def setupUi(self, MainWindow, My_Custom_widget):MainWindow.setObjectName("MainWindow")self.gridLayout.addWidget(My_Custom_widget, 1, 4, 1, 1)

                  任何想法如何解决它?

                  解决方案

                  首先我推荐你阅读

                  图片中,Widget容器是左上角的,现在我们将其替换为Gen_Settings,所以我们必须

                  1. 将出现以下对话框并填写如图所示的字段(我假设 gen_settings_ui.py 和 gen_settings.py 与当前 .ui 位于同一文件夹中)

                  1. 您按下添加按钮,然后按下升级按钮.

                  <小时>

                  然后你用 pyuic 将 .ui 转换为 .py ,你会得到以下结果:

                  从 PyQt5 导入 QtCore、QtGui、QtWidgets类 Ui_MainWindow(对象):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 导入 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:

                  1. Right click on the widget container and select the Promote To ... option.

                  1. 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)

                  1. 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
                  

                  这篇关于创建要嵌入到 QMainWindow 中的小部件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:QtWidgets.QFileDialog.getOpenFileName 返回一个元组 下一篇:如何在树视图中显示父目录?

                  相关文章

                  最新文章

                  <tfoot id='8qW2y'></tfoot>

                2. <small id='8qW2y'></small><noframes id='8qW2y'>

                  1. <i id='8qW2y'><tr id='8qW2y'><dt id='8qW2y'><q id='8qW2y'><span id='8qW2y'><b id='8qW2y'><form id='8qW2y'><ins id='8qW2y'></ins><ul id='8qW2y'></ul><sub id='8qW2y'></sub></form><legend id='8qW2y'></legend><bdo id='8qW2y'><pre id='8qW2y'><center id='8qW2y'></center></pre></bdo></b><th id='8qW2y'></th></span></q></dt></tr></i><div id='8qW2y'><tfoot id='8qW2y'></tfoot><dl id='8qW2y'><fieldset id='8qW2y'></fieldset></dl></div>
                      <legend id='8qW2y'><style id='8qW2y'><dir id='8qW2y'><q id='8qW2y'></q></dir></style></legend>
                        <bdo id='8qW2y'></bdo><ul id='8qW2y'></ul>