【问题标题】:Call functions in PyDev Eclipse in form generated by Qt Designer以 Qt Designer 生成的形式调用 PyDev Eclipse 中的函数
【发布时间】:2013-06-04 19:13:59
【问题描述】:

python 版本 3.3.2

ide:安装了 PyQt 的 PyDev Eclipse

我在 Qt Designer 中设计了一个 gui,保存了 .ui 文件并使用以下命令将其转换为 python 代码:

pyuic4 -x DeA.ui -o DeA.py

Qt Designer xml 输出:

<?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>485</width>
    <height>374</height>
   </rect>
  </property>
  <property name="windowTitle">
   <string>MainWindow</string>
  </property>
  <widget class="QWidget" name="centralwidget">
   <widget class="QPushButton" name="btn_add">
    <property name="geometry">
     <rect>
      <x>10</x>
      <y>10</y>
      <width>121</width>
      <height>41</height>
     </rect>
    </property>
    <property name="text">
     <string>Add item</string>
    </property>
   </widget>
   <widget class="QListView" name="lst">
    <property name="geometry">
     <rect>
      <x>10</x>
      <y>60</y>
      <width>461</width>
      <height>301</height>
     </rect>
    </property>
   </widget>
  </widget>
 </widget>
 <resources/>
 <connections/>
</ui>

pyuic4 最终 python gui 输出:

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

# Form implementation generated from reading ui file 'DeA.ui'
#
# Created: Tue Jun  4 03:30:45 2013
#      by: PyQt4 UI code generator 4.10.1
#
# WARNING! All changes made in this file will be lost!

from PyQt4 import QtCore, QtGui

try:
    _fromUtf8 = QtCore.QString.fromUtf8
except AttributeError:
    def _fromUtf8(s):
        return s

try:
    _encoding = QtGui.QApplication.UnicodeUTF8
    def _translate(context, text, disambig):
        return QtGui.QApplication.translate(context, text, disambig, _encoding)
except AttributeError:
    def _translate(context, text, disambig):
        return QtGui.QApplication.translate(context, text, disambig)

class Ui_MainWindow(object):
    def setupUi(self, MainWindow):
        MainWindow.setObjectName(_fromUtf8("MainWindow"))
        MainWindow.resize(485, 374)
        self.centralwidget = QtGui.QWidget(MainWindow)
        self.centralwidget.setObjectName(_fromUtf8("centralwidget"))
        self.btn_add = QtGui.QPushButton(self.centralwidget)
        self.btn_add.setGeometry(QtCore.QRect(10, 10, 121, 41))
        self.btn_add.setObjectName(_fromUtf8("btn_add"))
        self.lst = QtGui.QListView(self.centralwidget)
        self.lst.setGeometry(QtCore.QRect(10, 60, 461, 301))
        self.lst.setObjectName(_fromUtf8("lst"))
        MainWindow.setCentralWidget(self.centralwidget)

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

    def retranslateUi(self, MainWindow):
        MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow", None))
        self.btn_add.setText(_translate("MainWindow", "Add item", None))


if __name__ == "__main__":
    import sys
    app = QtGui.QApplication(sys.argv)
    MainWindow = QtGui.QMainWindow()
    ui = Ui_MainWindow()
    ui.setupUi(MainWindow)
    MainWindow.show()
    sys.exit(app.exec_())

然后我将新创建的 python 文件(“DeA.py”)的内容复制并粘贴到我在 PyDev Eclipse 中的 python 文件中,然后点击运行,gui 显示就好了;

我的问题:

我可以在 PyDev Eclipse 脚本窗格的哪个位置(不使用 Qt Designer 中的插槽)添加我自己的 python 函数,以便当我点击“添加项目”按钮时,它会在 ListView 中添加 100 个项目?

我发现了这个问题:Linking a qtDesigner .ui file to python/pyqt? 关于将 .ui 文件链接到最终项目并在运行时构建 gui;但我无法理解;请帮忙;我究竟应该在我的 PyDev Eclipse 脚本窗格中添加什么来链接 .ui 文件并在运行时构建它? (假设.ui文件路径为:D:\DeA.ui)

【问题讨论】:

    标签: python pyqt4 qt-designer


    【解决方案1】:

    您不必将 .ui 文件实际转换为 .py 即可使用它, 你可以做这样的事情..

    免得说我的 .ui 文件是

    “C:/mydesign.ui”

    from PyQt4  import QtGui
    from PyQt4  import QtCore
    from PyQt4  import uic
    import sys
    
    FORM_1, BASE_1 = uic.loadUiType(r"C:/mydesign.ui")
    
    APP = QtGui.QApplication(sys.argv)
    
    class MyApp(FORM_1, BASE_1):
        def __init__(self, parent=None):
            super(MyApp, self).__init__(parent)
            self.setupUi(self)
    
            self.connect(self.btn_add, QtCore.SIGNAL("released()"), self.do_something)
    
        def do_something(self):
            print "working"
    
    
    FORM = MyApp()
    FORM.show()
    APP.exec_()
    

    【讨论】:

      猜你喜欢
      • 2013-04-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-10-18
      • 2011-01-11
      相关资源
      最近更新 更多