PDA

View Full Version : Opening another created gui pyqt



jaybstory
17th January 2010, 02:35
Hello,

I would like to know how to open a gui after already having one gui open using pyqt? For example, I have a gui called popup and I want this gui to open another gui once I click the "ok" button. Would I need to use qprocess, qfile, or a some sort of signal slot connection to do this? Attached are the two guis I want to open. Runpopup2 is the first gui that I created and when the user clicks ok, I would like runabbs2 to open and runpopup2 to close. Thank you in advance.

aladagemre
17th January 2010, 19:06
An example portion of code from my own (click event for New User button):


def slotUserNew(self):
self.userDialog = QtGui.QDialog(self)
self.userDialog.ui = UserDialog(self) # caller=self, just if you wanted to call main dialog from the sub-dialog.
self.userDialog.ui.show()

jaybstory
17th January 2010, 23:10
An example portion of code from my own (click event for New User button):


def slotUserNew(self):
self.userDialog = QtGui.QDialog(self)
self.userDialog.ui = UserDialog(self) # caller=self, just if you wanted to call main dialog from the sub-dialog.
self.userDialog.ui.show()


Ok, so I'm trying to understand how this code works. For the first line in the function, would "userDialog" be the object name of the main window that I am trying to open?

For line 2, would "userDialog.ui" be the name of the program that I am trying to open? What is the second "UserDialog(self)?"

If this helps, the object name of the window that I am trying to open is MainWindow and the object name of the dialog that is already open is Dialog2.

Sorry for all the questions, still very new to pyqt. Thank you.

aladagemre
18th January 2010, 10:26
I'm sorry, I just pasted a code where I used Qt Designer and imported the resulting gui.py (pyuic4 gui.ui gui.py) file. So forget it if you don't use Qt Designer. If you use it, you might want to take a look at here (http://www.riverbankcomputing.co.uk/static/Docs/PyQt4/pyqt4ref.html) (13.1 Using the Generated Code)

Here's a minimal example where all the stuff is done by code:


import sys
from PyQt4.QtCore import *
from PyQt4.QtGui import *

class OtherWindow(QMainWindow):
def __init__(self):
QMainWindow.__init__(self)
layout = QHBoxLayout()
lineEdit = QLineEdit()
lineEdit.setText("Just to fill up the dialog")
layout.addWidget(lineEdit)

self.widget = QWidget()
self.widget.setLayout(layout)

self.setCentralWidget(self.widget)
self.setWindowTitle("Win2")

class MainWindow(QMainWindow):
def __init__(self):
QMainWindow.__init__(self)
layout = QHBoxLayout()
button = QPushButton()
layout.addWidget(button)

self.widget = QWidget()
self.widget.setLayout(layout)

self.setCentralWidget(self.widget)
self.setWindowTitle("Win1")

self.connect(button, SIGNAL('clicked()'), self.newWindow)
def newWindow(self):
self.myOtherWindow = OtherWindow()
self.myOtherWindow.show()

if __name__ == "__main__":

app = QApplication(sys.argv)
mainWindow = MainWindow()
mainWindow.setGeometry(100, 100, 200, 200)
mainWindow.show()
sys.exit(app.exec_())



So the key point is the latter part:


...
self.connect(button, SIGNAL('clicked()'), self.newWindow)
def newWindow(self):
self.myOtherWindow = OtherWindow()
self.myOtherWindow.show()


In the init of MainWindow, you connect the clicked signal of button to newWindow method.

newWindow method just creates a second window and shows it. That's it.

jaybstory
25th January 2010, 00:58
ah ok, great thank you!

hyoumoku
3rd February 2011, 05:37
I have made a new post here (http://www.qtcentre.org/threads/38314-How-to-open-a-2nd-form-after-a-button-is-clicked-in-the-1st-PyQT) which pointed out this thread. I'm sorry if I suddenly revive this thread. Nobody was replying at the thread I made so I thought if replying here would help me with what I'm asking ^^;;

I would like to ask you, aladagemre or jaybstory or anyone who can help, if by making ui through QtDesigner (with eric4 as I'm using it) the 2nd ui form, how do you by chance open it just like I made in the original thread I posted?

Below are parts of the 2nd post I replied within the original post

-----------------------------------

I imported the class of the second form. This is somewhat the codes (but I changed the names of the classes mostly)

form1.py

from PyQt4.QtGui import QMainWindow
from PyQt4.QtCore import pyqtSignature

from Ui_form1 import Ui_MainWindow

from ui.form2 import FormTwo

class FormOne(QMainWindow, Ui_MainWindow):
"""
Class documentation goes here.
"""
def __init__(self, parent = None):
"""
Constructor
"""
QMainWindow.__init__(self, parent)
self.setupUi(self)

@pyqtSignature("")
def on_button_released(self):
"""
Slot documentation goes here.
"""
FT = FormTwo()
FT.show()

form2.py

from PyQt4.QtGui import QDialog
from PyQt4.QtCore import pyqtSignature

from Ui_form2 import Ui_Dialog

class FormTwo(QDialog, Ui_Dialog):
"""
Class documentation goes here.
"""
def __init__(self, parent = None):
"""
Constructor
"""
QDialog.__init__(self, parent)
self.setupUi(self)

__init__.py

from PyQt4 import QtCore, QtGui
from ui.form1 import FormOne

if __name__ == "__main__":
import sys
app = QtGui.QApplication(sys.argv)
ui = FormOne()
ui.show()
sys.exit(app.exec_())

There is a new problem though that the 2nd form doesn't stay at all and closes immediately after I click the button on the first form. Would it be alright to ask how to correct and solve this?

-----------------------------------

Would it be alright if I ask what I'm doing wrong or something to solve this problem by chance? Thank you *bows deeply*

aladagemre
3rd February 2011, 09:15
You might try to put a "self." in front of your Form object name:


self.FT = FormTwo()
self.FT.show()

If you don't put it, garbage collection will remove that object as soon as setupUi method finishes. So the window will disappear.

But if you put a self there, object is kept in the memory so it won't disappear.