QT设计师中促进的小部件没有实例化儿童

发布于 2025-01-28 15:13:05 字数 4468 浏览 2 评论 0原文

问题

我在QT设计师中宣传了一个小部件,并且它奏效了,问题是当我运行程序时,它并没有显示我在QT Designer中创建的这个小部件的孩子。

因此

,为了显示一个可以产生问题的代码,我创建了一个测试项目,我可以在其中说明问题,

请考虑QT Designer中创建的以下UI:

”考虑以下在qt

如您所见,它包含一个带有盒装的中央widget,两个孩子,两个孩子,一个标签和一个Qtabwidget。

现在运行程序时,一切都很好:

” alt =“现在我运行程序时,一切都很好”>

布局现在被打破:

“现在是损坏的”

code

form.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="CentralWidgetPromotion" name="centralwidget">
   <widget class="QWidget" name="verticalLayoutWidget">
    <property name="geometry">
     <rect>
      <x>9</x>
      <y>19</y>
      <width>771</width>
      <height>531</height>
     </rect>
    </property>
    <layout class="QVBoxLayout" name="verticalLayout">
     <item>
      <widget class="QLabel" name="label">
       <property name="styleSheet">
        <string notr="true">background-color: rgb(220, 138, 221);</string>
       </property>
       <property name="text">
        <string>This is a test</string>
       </property>
      </widget>
     </item>
     <item>
      <widget class="QTabWidget" name="tabWidget">
       <property name="sizePolicy">
        <sizepolicy hsizetype="Minimum" vsizetype="Minimum">
         <horstretch>0</horstretch>
         <verstretch>0</verstretch>
        </sizepolicy>
       </property>
       <property name="currentIndex">
        <number>0</number>
       </property>
       <widget class="QWidget" name="tab">
        <attribute name="title">
         <string>This is tab 2</string>
        </attribute>
       </widget>
       <widget class="QWidget" name="tab_2">
        <attribute name="title">
         <string>This is tab1</string>
        </attribute>
       </widget>
      </widget>
     </item>
    </layout>
   </widget>
  </widget>
  <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>
 <customwidgets>
  <customwidget>
   <class>CentralWidgetPromotion</class>
   <extends>QWidget</extends>
   <header>central_widget_promotion.h</header>
   <container>1</container>
  </customwidget>
 </customwidgets>
 <resources/>
 <connections/>
</ui>

mainwindow.py(qmainwindow)

# This Python file uses the following encoding: utf-8
import os
from pathlib import Path
import sys

from PyQt6.QtWidgets import QApplication, QMainWindow
from PyQt6 import uic

class MainWindow(QMainWindow):

    def __init__(self):
        super().__init__()
        self.loadUi()

    def loadUi(self):
        path = os.fspath(Path(__file__).resolve().parent / "form.ui")
        uic.loadUi(path,self)

if __name__ == "__main__":

    qt_app = QApplication(sys.argv)
    widget = MainWindow()
    widget.show()
    sys.exit(qt_app.exec())

小部件,我在其中宣传了中央景点。

from PyQt6.QtWidgets import QWidget

class CentralWidgetPromotion(QWidget):

    def __init__(self, parent=None):
        super().__init__(parent)

        print("Promotion working")

centralwidgetPromotion.py py py我希望我宣传的 我的问题很清楚,如果还有任何问题,请询问。

Problem

I promoted a widget in Qt Designer and it worked, the problem is that when I run the program, it doesn't show the children of this widget that I created in Qt Designer.

Example

So in order to show a code that can produce the problem I created a test project where I can demonstrate the problem

Consider the following UI created in Qt Designer:

Consider the following UI created in Qt Designer

As you can see, it contains a centralWidget, with a BoxLayout, and two children, a label and a QTabWidget.

When I run the program now, everything is fine:

When I run the program now, everything is fine

But when I promote the CentralWidget to a custom Widget:

But when I promote the CentralWidget to a custom Widget

The layout is now broken:

The layout is now broken

Code

form.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="CentralWidgetPromotion" name="centralwidget">
   <widget class="QWidget" name="verticalLayoutWidget">
    <property name="geometry">
     <rect>
      <x>9</x>
      <y>19</y>
      <width>771</width>
      <height>531</height>
     </rect>
    </property>
    <layout class="QVBoxLayout" name="verticalLayout">
     <item>
      <widget class="QLabel" name="label">
       <property name="styleSheet">
        <string notr="true">background-color: rgb(220, 138, 221);</string>
       </property>
       <property name="text">
        <string>This is a test</string>
       </property>
      </widget>
     </item>
     <item>
      <widget class="QTabWidget" name="tabWidget">
       <property name="sizePolicy">
        <sizepolicy hsizetype="Minimum" vsizetype="Minimum">
         <horstretch>0</horstretch>
         <verstretch>0</verstretch>
        </sizepolicy>
       </property>
       <property name="currentIndex">
        <number>0</number>
       </property>
       <widget class="QWidget" name="tab">
        <attribute name="title">
         <string>This is tab 2</string>
        </attribute>
       </widget>
       <widget class="QWidget" name="tab_2">
        <attribute name="title">
         <string>This is tab1</string>
        </attribute>
       </widget>
      </widget>
     </item>
    </layout>
   </widget>
  </widget>
  <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>
 <customwidgets>
  <customwidget>
   <class>CentralWidgetPromotion</class>
   <extends>QWidget</extends>
   <header>central_widget_promotion.h</header>
   <container>1</container>
  </customwidget>
 </customwidgets>
 <resources/>
 <connections/>
</ui>

mainwindow.py (QMainWindow)

# This Python file uses the following encoding: utf-8
import os
from pathlib import Path
import sys

from PyQt6.QtWidgets import QApplication, QMainWindow
from PyQt6 import uic

class MainWindow(QMainWindow):

    def __init__(self):
        super().__init__()
        self.loadUi()

    def loadUi(self):
        path = os.fspath(Path(__file__).resolve().parent / "form.ui")
        uic.loadUi(path,self)

if __name__ == "__main__":

    qt_app = QApplication(sys.argv)
    widget = MainWindow()
    widget.show()
    sys.exit(qt_app.exec())

CentralWidgetPromotion.py the widget in which I promoted the centralWidget

from PyQt6.QtWidgets import QWidget

class CentralWidgetPromotion(QWidget):

    def __init__(self, parent=None):
        super().__init__(parent)

        print("Promotion working")

I hope I made my problem clear, if there is still any problem, please ask.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

沉溺在你眼里的海 2025-02-04 15:13:05

据我所知,它可能是UIC中的“错误”;通过查看uiparser.py,我找到了这一行:

        elif isinstance(topwidget, QtWidgets.QMainWindow):
            if type(widget) == QtWidgets.QWidget:
                topwidget.setCentralWidget(widget)

既然type()==&lt; class&gt;仅与确切的类匹配,而不是继承,则该比较将是false:当您的晋升类的确从qwidget继承时,它是 a qwidget type ,但是centralwidget -promotion一个。

假设您确定中央小部件的对象名称,您可以围绕它进行工作:

    def loadUi(self):
        path = os.fspath(Path(__file__).resolve().parent / "form.ui")
        uic.loadUi(path, self)

        if not self.centralWidget() and hasattr(self, 'centralwidget'):
            self.setCentralWidget(self.centralwidget)

考虑到促进中央小部件通常几乎没有意义:促销通常旨在扩展特定的“最终”小部件,并且由于中央小部件主要是一个基本容器,您可能要做的大部分可以轻松地在您已经使用的QMainWindow子类中实现。

请注意,您不应该通过从小部件框中拖动它来“添加”中央小部件的布局,但是 set 一:现在,该布局只是在窗口,如果调整它的大小,它的内容将不会与之调整大小(请务必检查对象树中的那个小红色symbor,这意味着相对小部件没有布局集合)。删除该垂直布局(右键单击其任何小部件,然后从“布置”子菜单中选择“断开布局”),然后右键单击窗口的空区域,然后从“相对菜单”中选择布局。阅读如何正确使用设计师中的布局经理

From what I can see, it could be a "bug" in uic; by looking at uiparser.py, I found this line:

        elif isinstance(topwidget, QtWidgets.QMainWindow):
            if type(widget) == QtWidgets.QWidget:
                topwidget.setCentralWidget(widget)

Since type() == <class> only matches the exact class, and not the inheritance, the result of that comparison will be False: while your promoted class does inherit from QWidget, it is not a QWidget type, but a CentralWidgetPromotion one.

Supposing you know for sure the object name of the central widget, you can work around it with this:

    def loadUi(self):
        path = os.fspath(Path(__file__).resolve().parent / "form.ui")
        uic.loadUi(path, self)

        if not self.centralWidget() and hasattr(self, 'centralwidget'):
            self.setCentralWidget(self.centralwidget)

Consider that there's usually very little point in promoting the central widget: promotion is normally intended to extend specific "final" widgets, and since the central widget is mainly a basic container, most of what you would probably do can be easily implemented in the QMainWindow subclass you're already using.

Note that you are not supposed to "add" a layout to the central widget by dragging it from the widget box, but set one: right now, that layout is just "floating" in the window, and if you resize it, its contents won't be resized along with it (always check for that small red ???? symbol in the object tree, which means that the relative widget has no layout set). Remove that vertical layout (right click on any of its widget, and select "Break layout" from the "Lay out" sub menu), then right click on an empty area of the window and select the layout from the relative menu. Read how to properly use layout managers in Designer

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文