PyQt widget communication

253 views
Skip to first unread message

iski....@gmail.com

unread,
Feb 12, 2016, 2:57:23 PM2/12/16
to Python Programming for Autodesk Maya
I have two widget: The first (Form1) have a combobox. If I using this combobox that is show on console. (#This is good) But I would like using this variable on second widget (Form2), but I don't show it. (#This is bad) How can I use this "text" variable on my Form2 widget?

import sys

from functools import partial
from PyQt4.QtGui import *
from PyQt4.QtCore import *
from PyQt4 import QtGui, QtCore
from math import sqrt
from time import gmtime, strftime

text = ""

class Form1(QWidget):
showForm2Signal = pyqtSignal()

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

self.comboBox = QtGui.QComboBox(self)
self.comboBox.addItem("Text 1")
self.comboBox.addItem("Text 2")
self.comboBox.addItem("Text 3")
self.comboBox.addItem("Text 4")
self.comboBox.addItem("Text 5")
self.comboBox.addItem("Text 6")
self.comboBox.addItem("Text 7")
self.comboBox.move(20, 20)
self.comboBox.resize(360,30)
self.comboBox.currentIndexChanged.connect(self.selectionchange)

layout = QVBoxLayout(self)

ok_button = QtGui.QPushButton("OK", self)
ok_button.resize(ok_button.minimumSizeHint())
ok_button.move(0,340)
ok_button.resize(400,60)
ok_button.setStyleSheet("color: #25373D; background-color: #71BA51; font-size: 16pt; font-weight: bold;")

ok_button.clicked.connect(self.showForm2Signal.emit)

def selectionchange(self,i):
text = self.comboBox.currentText()
#This is good
print text

class Form2(QWidget):
showForm1Signal = pyqtSignal()

def __init__(self, parent=None):
super(Form2, self).__init__(parent)
#This is bad
print text
self.backButton = QPushButton("Back", self)
self.backButton.clicked.connect(self.showForm1Signal.emit)


class MainWidget(QWidget):
def __init__(self, parent=None):
super(MainWidget, self).__init__(parent)
self.stack = QStackedWidget()
layout = QVBoxLayout(self)
layout.addWidget(self.stack)
layout.setContentsMargins(0, 0, 0, 0)
self.setGeometry(0, 0, 400, 400)
self.setWindowTitle("TEST")
self.form1 = Form1(self)
self.form2 = Form2(self)
self.stack.addWidget(self.form1)
self.stack.addWidget(self.form2)

self.form1.showForm2Signal.connect(partial(self.stack.setCurrentWidget,self.form2))
self.form2.showForm1Signal.connect(partial(self.stack.setCurrentWidget,self.form1))
self.stack.setCurrentWidget(self.form1)
if __name__ == '__main__':
app = QApplication(sys.argv)
w = MainWidget()
w.show()
app.exec_()
sys.exit()

Justin Israel

unread,
Feb 12, 2016, 3:30:16 PM2/12/16
to Python Programming for Autodesk Maya

Hi,

In the future can you please use a code sharing url of some type, for long code? Either gist or a pastebin type site would be helpful.

In this setup, you definitely want to keep a separation between the different forms, and you are doing this so far. You want the ability for forms to be reordered or extended and not have them no about each other. It's good that you just wire them up in the main window.

For form values like your text value, what you may want to do is to share a context object between all your forms, when you instantiate them. This would all you to define the data you expect each form to contribute to and all forms can access the state of the data. It could be as simple as a dictionary.

That being said, you may want to look into QWizard
http://doc.qt.io/qt-4.8/qwizard.html

It does exactly what you are after, by allowing you to define pages, validation, and form properties that can be checked across all pages. It handles checking if your page reports that it validates before letting you proceed to another page.

And since I can never resist chucking in some random code commentary...
When connecting a signal to another signal, leave off the "emit" part:
self.backButton.clicked.connect(self.showForm1Signal)

Qt is a lot smarter when you show it you are connecting a signal to another signal and can handle mismatched parameter signatures.

Also I would recommend not importing * from the Qt modules. It pollutes the current module with a massive amount of objects.

Justin


--
You received this message because you are subscribed to the Google Groups "Python Programming for Autodesk Maya" group.
To unsubscribe from this group and stop receiving emails from it, send an email to python_inside_m...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/python_inside_maya/a7bf6ef8-6dfd-45cf-9c8b-7b64f21ff6f8%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Reply all
Reply to author
Forward
0 new messages