Results 1 to 7 of 7

Thread: Opening another created gui pyqt

  1. #1
    Join Date
    Jan 2010
    Posts
    20
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Opening another created gui pyqt

    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.
    Attached Images Attached Images

  2. #2
    Join Date
    Jan 2010
    Location
    Istanbul, Turkey
    Posts
    10
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Opening another created gui pyqt

    An example portion of code from my own (click event for New User button):
    Qt Code:
    1. def slotUserNew(self):
    2. self.userDialog = QtGui.QDialog(self)
    3. self.userDialog.ui = UserDialog(self) # caller=self, just if you wanted to call main dialog from the sub-dialog.
    4. self.userDialog.ui.show()
    To copy to clipboard, switch view to plain text mode 

  3. #3
    Join Date
    Jan 2010
    Posts
    20
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Opening another created gui pyqt

    Quote Originally Posted by aladagemre View Post
    An example portion of code from my own (click event for New User button):
    Qt Code:
    1. def slotUserNew(self):
    2. self.userDialog = QtGui.QDialog(self)
    3. self.userDialog.ui = UserDialog(self) # caller=self, just if you wanted to call main dialog from the sub-dialog.
    4. self.userDialog.ui.show()
    To copy to clipboard, switch view to plain text mode 

    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.

  4. #4
    Join Date
    Jan 2010
    Location
    Istanbul, Turkey
    Posts
    10
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Opening another created gui pyqt

    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 (13.1 Using the Generated Code)

    Here's a minimal example where all the stuff is done by code:
    Qt Code:
    1. import sys
    2. from PyQt4.QtCore import *
    3. from PyQt4.QtGui import *
    4.  
    5. class OtherWindow(QMainWindow):
    6. def __init__(self):
    7. QMainWindow.__init__(self)
    8. layout = QHBoxLayout()
    9. lineEdit = QLineEdit()
    10. lineEdit.setText("Just to fill up the dialog")
    11. layout.addWidget(lineEdit)
    12.  
    13. self.widget = QWidget()
    14. self.widget.setLayout(layout)
    15.  
    16. self.setCentralWidget(self.widget)
    17. self.setWindowTitle("Win2")
    18.  
    19. class MainWindow(QMainWindow):
    20. def __init__(self):
    21. QMainWindow.__init__(self)
    22. layout = QHBoxLayout()
    23. button = QPushButton()
    24. layout.addWidget(button)
    25.  
    26. self.widget = QWidget()
    27. self.widget.setLayout(layout)
    28.  
    29. self.setCentralWidget(self.widget)
    30. self.setWindowTitle("Win1")
    31.  
    32. self.connect(button, SIGNAL('clicked()'), self.newWindow)
    33. def newWindow(self):
    34. self.myOtherWindow = OtherWindow()
    35. self.myOtherWindow.show()
    36.  
    37. if __name__ == "__main__":
    38.  
    39. app = QApplication(sys.argv)
    40. mainWindow = MainWindow()
    41. mainWindow.setGeometry(100, 100, 200, 200)
    42. mainWindow.show()
    43. sys.exit(app.exec_())
    To copy to clipboard, switch view to plain text mode 

    So the key point is the latter part:
    Qt Code:
    1. ...
    2. self.connect(button, SIGNAL('clicked()'), self.newWindow)
    3. def newWindow(self):
    4. self.myOtherWindow = OtherWindow()
    5. self.myOtherWindow.show()
    To copy to clipboard, switch view to plain text mode 

    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.
    Last edited by aladagemre; 18th January 2010 at 10:38.

  5. #5
    Join Date
    Jan 2010
    Posts
    20
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Opening another created gui pyqt

    ah ok, great thank you!

  6. #6
    Join Date
    Feb 2011
    Posts
    4
    Qt products
    PyQt3 PyQt4
    Platforms
    Unix/X11 Windows

    Default Re: Opening another created gui pyqt

    I have made a new post here 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
    Qt Code:
    1. from PyQt4.QtGui import QMainWindow
    2. from PyQt4.QtCore import pyqtSignature
    3.  
    4. from Ui_form1 import Ui_MainWindow
    5.  
    6. from ui.form2 import FormTwo
    7.  
    8. class FormOne(QMainWindow, Ui_MainWindow):
    9. """
    10. Class documentation goes here.
    11. """
    12. def __init__(self, parent = None):
    13. """
    14. Constructor
    15. """
    16. QMainWindow.__init__(self, parent)
    17. self.setupUi(self)
    18.  
    19. @pyqtSignature("")
    20. def on_button_released(self):
    21. """
    22. Slot documentation goes here.
    23. """
    24. FT = FormTwo()
    25. FT.show()
    To copy to clipboard, switch view to plain text mode 

    form2.py
    Qt Code:
    1. from PyQt4.QtGui import QDialog
    2. from PyQt4.QtCore import pyqtSignature
    3.  
    4. from Ui_form2 import Ui_Dialog
    5.  
    6. class FormTwo(QDialog, Ui_Dialog):
    7. """
    8. Class documentation goes here.
    9. """
    10. def __init__(self, parent = None):
    11. """
    12. Constructor
    13. """
    14. QDialog.__init__(self, parent)
    15. self.setupUi(self)
    To copy to clipboard, switch view to plain text mode 

    __init__.py
    Qt Code:
    1. from PyQt4 import QtCore, QtGui
    2. from ui.form1 import FormOne
    3.  
    4. if __name__ == "__main__":
    5. import sys
    6. app = QtGui.QApplication(sys.argv)
    7. ui = FormOne()
    8. ui.show()
    9. sys.exit(app.exec_())
    To copy to clipboard, switch view to plain text mode 

    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*

  7. #7
    Join Date
    Jan 2010
    Location
    Istanbul, Turkey
    Posts
    10
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Opening another created gui pyqt

    You might try to put a "self." in front of your Form object name:

    Qt Code:
    1. self.FT = FormTwo()
    2. self.FT.show()
    To copy to clipboard, switch view to plain text mode 

    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.

Similar Threads

  1. Replies: 5
    Last Post: 10th November 2009, 15:46
  2. Opening a browser in MAC
    By gren15 in forum Newbie
    Replies: 2
    Last Post: 30th June 2009, 16:54
  3. opening PPT files in Qt
    By jay in forum Qt Programming
    Replies: 5
    Last Post: 14th May 2009, 12:20
  4. opening webpage using qt....
    By anupamgee in forum Qt Programming
    Replies: 8
    Last Post: 20th April 2009, 11:13
  5. Opening a shapefile
    By peace_comp in forum Qt Programming
    Replies: 1
    Last Post: 1st May 2008, 20:59

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.