使用 qt-designer 和 pyqt 创建简单的表单

发布于 2024-12-01 10:59:43 字数 5586 浏览 2 评论 0原文

我正在尝试在 pyqt 中运行我的第一个应用程序。当我在设计器中进行预览时,我的表单看起来不错:

http://imageshack .us/photo/my-images/171/screenshotuw.png/

但是如果我从我的脚本中显示它,我得到:

http://imageshack.us/photo/my-images/268/screenshot1hwn.png/

终端中的信息: QLayout:尝试将 QLayout“” 添加到 MyForm“Form”,它已经有布局

问题是,我的设计有什么问题?

由 pyuic4 生成的文件:

# -*- coding: utf-8 -*-

# Form implementation generated from reading ui file 'gui.ui'
#
# Created: Tue Aug 23 11:17:30 2011
#      by: PyQt4 UI code generator 4.7.2
#
# WARNING! All changes made in this file will be lost!

from PyQt4 import QtCore, QtGui

class Ui_Form(object):
    def setupUi(self, Form):
    Form.setObjectName("Form")
    Form.resize(464, 409)
    self.verticalLayout_2 = QtGui.QVBoxLayout(Form)
    self.verticalLayout_2.setObjectName("verticalLayout_2")
    self.horizontalLayout = QtGui.QHBoxLayout()
    self.horizontalLayout.setObjectName("horizontalLayout")
    self.listView = QtGui.QListView(Form)
    self.listView.setObjectName("listView")
    self.horizontalLayout.addWidget(self.listView)
    self.verticalLayout = QtGui.QVBoxLayout()
    self.verticalLayout.setObjectName("verticalLayout")
    self.pushButton = QtGui.QPushButton(Form)
    self.pushButton.setObjectName("pushButton")
    self.verticalLayout.addWidget(self.pushButton)
    self.pushButton_2 = QtGui.QPushButton(Form)
    self.pushButton_2.setObjectName("pushButton_2")
    self.verticalLayout.addWidget(self.pushButton_2)
    spacerItem = QtGui.QSpacerItem(20, 40, QtGui.QSizePolicy.Minimum, QtGui.QSizePolicy.Expanding)
    self.verticalLayout.addItem(spacerItem)
    self.pushButton_3 = QtGui.QPushButton(Form)
    self.pushButton_3.setObjectName("pushButton_3")
    self.verticalLayout.addWidget(self.pushButton_3)
    self.horizontalLayout.addLayout(self.verticalLayout)
    self.verticalLayout_2.addLayout(self.horizontalLayout)
    self.textBrowser = QtGui.QTextBrowser(Form)
    self.textBrowser.setObjectName("textBrowser")
    self.verticalLayout_2.addWidget(self.textBrowser)

    self.retranslateUi(Form)
    QtCore.QMetaObject.connectSlotsByName(Form)

    def retranslateUi(self, Form):
    Form.setWindowTitle(QtGui.QApplication.translate("Form", "Form", None, QtGui.QApplication.UnicodeUTF8))
    self.pushButton.setText(QtGui.QApplication.translate("Form", "PushButton", None, QtGui.QApplication.UnicodeUTF8))
    self.pushButton_2.setText(QtGui.QApplication.translate("Form", "PushButton", None, QtGui.QApplication.UnicodeUTF8))
    self.pushButton_3.setText(QtGui.QApplication.translate("Form", "PushButton", None, QtGui.QApplication.UnicodeUTF8))

以及我正在使用它的脚本: 导入系统 from PyQt4 import QtCore, QtGui

    from gen import Ui_Form


    class MyForm(QtGui.QMainWindow):
        def __init__(self, parent=None):
            QtGui.QWidget.__init__(self, parent)
            self.ui = Ui_Form()
            self.ui.setupUi(self)
        def execute_event(self):
            pass
        def execute_all_event(self):
            pass
        def reload_event(self):
            pass

    if __name__ == "__main__":
        app = QtGui.QApplication(sys.argv)
        myapp = MyForm()
        myapp.show()
        sys.exit(app.exec_())

我使用设计器创建了表单,并得到了 gui.ui 文件:

<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
 <class>Form</class>
 <widget class="QWidget" name="Form">
  <property name="geometry">
   <rect>
    <x>0</x>
    <y>0</y>
    <width>464</width>
    <height>409</height>
   </rect>
  </property>
  <property name="windowTitle">
   <string>Form</string>
  </property>
  <layout class="QVBoxLayout" name="verticalLayout_2">
   <item>
    <layout class="QHBoxLayout" name="horizontalLayout">
     <item>
      <widget class="QListView" name="listView"/>
     </item>
     <item>
      <layout class="QVBoxLayout" name="verticalLayout">
       <item>
    <widget class="QPushButton" name="pushButton">
     <property name="text">
      <string>PushButton</string>
     </property>
    </widget>
       </item>
       <item>
    <widget class="QPushButton" name="pushButton_2">
     <property name="text">
      <string>PushButton</string>
     </property>
    </widget>
       </item>
       <item>
    <spacer name="verticalSpacer">
     <property name="orientation">
      <enum>Qt::Vertical</enum>
     </property>
     <property name="sizeHint" stdset="0">
      <size>
       <width>20</width>
       <height>40</height>
      </size>
     </property>
    </spacer>
       </item>
       <item>
    <widget class="QPushButton" name="pushButton_3">
     <property name="text">
      <string>PushButton</string>
     </property>
    </widget>
       </item>
      </layout>
     </item>
    </layout>
   </item>
   <item>
    <widget class="QTextBrowser" name="textBrowser"/>
   </item>
  </layout>
 </widget>
 <resources/>
 <connections/>
</ui>

I'm trying to run my first application in pyqt. My form looks fine when I'm doing preview in designer:

http://imageshack.us/photo/my-images/171/screenshotuw.png/

But if I'm showing it from my script I got:

http://imageshack.us/photo/my-images/268/screenshot1hwn.png/

And information in terminal:
QLayout: Attempting to add QLayout "" to MyForm "Form", which already has a layout

The question is, what is wrong with my design?

Generated file by pyuic4:

# -*- coding: utf-8 -*-

# Form implementation generated from reading ui file 'gui.ui'
#
# Created: Tue Aug 23 11:17:30 2011
#      by: PyQt4 UI code generator 4.7.2
#
# WARNING! All changes made in this file will be lost!

from PyQt4 import QtCore, QtGui

class Ui_Form(object):
    def setupUi(self, Form):
    Form.setObjectName("Form")
    Form.resize(464, 409)
    self.verticalLayout_2 = QtGui.QVBoxLayout(Form)
    self.verticalLayout_2.setObjectName("verticalLayout_2")
    self.horizontalLayout = QtGui.QHBoxLayout()
    self.horizontalLayout.setObjectName("horizontalLayout")
    self.listView = QtGui.QListView(Form)
    self.listView.setObjectName("listView")
    self.horizontalLayout.addWidget(self.listView)
    self.verticalLayout = QtGui.QVBoxLayout()
    self.verticalLayout.setObjectName("verticalLayout")
    self.pushButton = QtGui.QPushButton(Form)
    self.pushButton.setObjectName("pushButton")
    self.verticalLayout.addWidget(self.pushButton)
    self.pushButton_2 = QtGui.QPushButton(Form)
    self.pushButton_2.setObjectName("pushButton_2")
    self.verticalLayout.addWidget(self.pushButton_2)
    spacerItem = QtGui.QSpacerItem(20, 40, QtGui.QSizePolicy.Minimum, QtGui.QSizePolicy.Expanding)
    self.verticalLayout.addItem(spacerItem)
    self.pushButton_3 = QtGui.QPushButton(Form)
    self.pushButton_3.setObjectName("pushButton_3")
    self.verticalLayout.addWidget(self.pushButton_3)
    self.horizontalLayout.addLayout(self.verticalLayout)
    self.verticalLayout_2.addLayout(self.horizontalLayout)
    self.textBrowser = QtGui.QTextBrowser(Form)
    self.textBrowser.setObjectName("textBrowser")
    self.verticalLayout_2.addWidget(self.textBrowser)

    self.retranslateUi(Form)
    QtCore.QMetaObject.connectSlotsByName(Form)

    def retranslateUi(self, Form):
    Form.setWindowTitle(QtGui.QApplication.translate("Form", "Form", None, QtGui.QApplication.UnicodeUTF8))
    self.pushButton.setText(QtGui.QApplication.translate("Form", "PushButton", None, QtGui.QApplication.UnicodeUTF8))
    self.pushButton_2.setText(QtGui.QApplication.translate("Form", "PushButton", None, QtGui.QApplication.UnicodeUTF8))
    self.pushButton_3.setText(QtGui.QApplication.translate("Form", "PushButton", None, QtGui.QApplication.UnicodeUTF8))

And my script which is using it:
import sys
from PyQt4 import QtCore, QtGui

    from gen import Ui_Form


    class MyForm(QtGui.QMainWindow):
        def __init__(self, parent=None):
            QtGui.QWidget.__init__(self, parent)
            self.ui = Ui_Form()
            self.ui.setupUi(self)
        def execute_event(self):
            pass
        def execute_all_event(self):
            pass
        def reload_event(self):
            pass

    if __name__ == "__main__":
        app = QtGui.QApplication(sys.argv)
        myapp = MyForm()
        myapp.show()
        sys.exit(app.exec_())

I created form using designer and I got gui.ui file:

<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
 <class>Form</class>
 <widget class="QWidget" name="Form">
  <property name="geometry">
   <rect>
    <x>0</x>
    <y>0</y>
    <width>464</width>
    <height>409</height>
   </rect>
  </property>
  <property name="windowTitle">
   <string>Form</string>
  </property>
  <layout class="QVBoxLayout" name="verticalLayout_2">
   <item>
    <layout class="QHBoxLayout" name="horizontalLayout">
     <item>
      <widget class="QListView" name="listView"/>
     </item>
     <item>
      <layout class="QVBoxLayout" name="verticalLayout">
       <item>
    <widget class="QPushButton" name="pushButton">
     <property name="text">
      <string>PushButton</string>
     </property>
    </widget>
       </item>
       <item>
    <widget class="QPushButton" name="pushButton_2">
     <property name="text">
      <string>PushButton</string>
     </property>
    </widget>
       </item>
       <item>
    <spacer name="verticalSpacer">
     <property name="orientation">
      <enum>Qt::Vertical</enum>
     </property>
     <property name="sizeHint" stdset="0">
      <size>
       <width>20</width>
       <height>40</height>
      </size>
     </property>
    </spacer>
       </item>
       <item>
    <widget class="QPushButton" name="pushButton_3">
     <property name="text">
      <string>PushButton</string>
     </property>
    </widget>
       </item>
      </layout>
     </item>
    </layout>
   </item>
   <item>
    <widget class="QTextBrowser" name="textBrowser"/>
   </item>
  </layout>
 </widget>
 <resources/>
 <connections/>
</ui>

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

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

发布评论

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

评论(1

内心旳酸楚 2024-12-08 10:59:43

我想说的是,您已经在设计器中设计了一个QWidget,并且创建了一个QMainWindow
替换

class MyForm(QtGui.QMainWindow)

class MyForm(QtGui.QWidget)

I would say that you have designed a QWidget in the designer, and you create a QMainWindow.
Replace

class MyForm(QtGui.QMainWindow)

by

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