Pyqt passing parameters to a form based on user ac

2019-06-02 04:54发布

问题:

I have a form and a popup form as given below (partial code):

import sys
import subprocess
import os
from PyQt4 import QtCore, QtGui
from ui_auto import Ui_Form
from popup_ui import Ui_Form as fm

class MyPopupForm(QtGui.QWidget):
    def __init__(self, parent=None):
        QtGui.QWidget.__init__(self, parent)
        self.ui = fm()
        self.ui.setupUi(self)
class MyForm(QtGui.QMainWindow):
    def __init__(self, parent=None):
        QtGui.QWidget.__init__(self, parent)
        self.ui = Ui_Form()
        self.ui.setupUi(self)
    def when_pressed(self):
         self.mypopup=MyPopupForm()
         self.mypopup.show()
    def when_stopped(self):
         self.mypopup=MyPopupForm()
         self.mypopup.show()

Myform is my main form and MyPopupForm is the popup form. I need to do in such a way that, when I press a button it will print some string and display that string. When I press another button i have to invoke the same form but with different string. How could I do that (I used Qtdesigner to create UI)

MyPopupForm code in python:

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

# Form implementation generated from reading ui file 'popup.ui'
#
# Created: Sun Jan  8 11:18:43 2012
#      by: PyQt4 UI code generator 4.7.3
#
# 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(207, 170)
        self.pushButton = QtGui.QPushButton(Form)
        self.pushButton.setGeometry(QtCore.QRect(60, 120, 92, 27))
        self.pushButton.setObjectName("pushButton")
        self.label = QtGui.QLabel(Form)
        self.label.setGeometry(QtCore.QRect(58, 30, 81, 41))
        #self.label.setText("")
        self.label.setObjectName("label")

        self.retranslateUi(Form)
        QtCore.QObject.connect(self.pushButton, QtCore.SIGNAL("clicked()"), Form.close)
        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", "OK", None, QtGui.QApplication.UnicodeUTF8))

回答1:

The simplest way is to add a parameter to the __init__ method of the class MyPopupForm

def __init__(self, string_to_be_passed=None, parent=None):

and then when you call it with

self.mypopup=MyPopupForm("value_to_display")

using the string_to_be_passed in the __init__ method to display the value.

Another method is to add a method to the class MyPopupForm to set the string to display and then

self.mypopup=MyPopupForm()
self.mypopup.setValueToDisplay("value")
self.mypopup.show()

with the setValueToDisplay() that display the string where needed.



回答2:

When compiling the python module with pyuic4, I think it is best to use the -w option, which will create a much simpler ui class without all the setupUi nonsense.

Doing it that way also means the class has exactly the name you gave it in Designer (rather than the nasty mangled version with the Ui_ prefix), and it's also much simpler to subclass it if you need to.

Below, I've included a ui file with a simple Dialog class for showing a message. There's also a script that demonstrates how to use it.

Save the ui file as dialog.ui, and do:

pyuic4 -w dialog.ui > dialog.py

to compile the python module. Then run the script from the same directory.

UI File:

<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
 <class>Dialog</class>
 <widget class="QDialog" name="Dialog">
  <property name="geometry">
   <rect>
    <x>0</x>
    <y>0</y>
    <width>400</width>
    <height>125</height>
   </rect>
  </property>
  <property name="windowTitle">
   <string>Dialog</string>
  </property>
  <layout class="QVBoxLayout" name="verticalLayout">
   <item>
    <widget class="QLabel" name="labelMessage">
     <property name="frameShape">
      <enum>QFrame::StyledPanel</enum>
     </property>
     <property name="text">
      <string/>
     </property>
     <property name="alignment">
      <set>Qt::AlignCenter</set>
     </property>
    </widget>
   </item>
   <item>
    <widget class="QPushButton" name="buttonClose">
     <property name="text">
      <string>Close</string>
     </property>
    </widget>
   </item>
  </layout>
 </widget>
 <resources/>
 <connections>
  <connection>
   <sender>buttonClose</sender>
   <signal>clicked()</signal>
   <receiver>Dialog</receiver>
   <slot>reject()</slot>
   <hints>
    <hint type="sourcelabel">
     <x>194</x>
     <y>107</y>
    </hint>
    <hint type="destinationlabel">
     <x>402</x>
     <y>88</y>
    </hint>
   </hints>
  </connection>
 </connections>
</ui>

Script:

from PyQt4 import QtGui, QtCore
from dialog import Dialog

class Window(QtGui.QWidget):
    def __init__(self):
        QtGui.QWidget.__init__(self)
        self.buttonOne = QtGui.QPushButton('Test One', self)
        self.buttonOne.clicked.connect(self.handleButtonOne)
        self.buttonTwo = QtGui.QPushButton('Test Two', self)
        self.buttonTwo.clicked.connect(self.handleButtonTwo)
        layout = QtGui.QHBoxLayout(self)
        layout.addWidget(self.buttonOne)
        layout.addWidget(self.buttonTwo)

    def showDialog(self, message):
        dialog = Dialog(self)
        dialog.setAttribute(QtCore.Qt.WA_DeleteOnClose)
        dialog.labelMessage.setText(message)
        dialog.show()

    def handleButtonOne(self):
        self.showDialog('This is the message for Button One')

    def handleButtonTwo(self):
        self.showDialog('This is the message for Button Two')

if __name__ == '__main__':

    import sys
    app = QtGui.QApplication(sys.argv)
    window = Window()
    window.show()
    sys.exit(app.exec_())


标签: qt pyqt pyqt4