Results 1 to 4 of 4

Thread: opening a second window

  1. #1
    Join Date
    Feb 2010
    Posts
    12
    Qt products
    Qt4 PyQt3 PyQt4
    Platforms
    Unix/X11

    Default opening a second window

    Hi all i'm new to python and qt

    I have played a little with c++ and basic ,But most of the programing I have done in the past was for the web using java script, so this is all new to me.

    I am working on a program, and would like to open a window when I push on a button. so fare every thing I have tried has failed. There really doesn't seem to be a good tutorial on this, so I was hoping some would post a simple example of how this is done.

    To help with this I have posted three files two are the window files the last one is starting point for the program.

    I think other newbies will find this interesting

    Thanks for any help



    Saved as "openwindowmain.py"
    Qt Code:
    1. # -*- coding: utf-8 -*-
    2.  
    3. # Form implementation generated from reading ui file 'openwindowmain.ui'
    4. #
    5. # Created: Sun Feb 21 18:59:44 2010
    6. # by: PyQt4 UI code generator 4.3.3
    7. #
    8. # WARNING! All changes made in this file will be lost!
    9.  
    10. from PyQt4 import QtCore, QtGui
    11.  
    12. class Ui_Dialog(object):
    13. def setupUi(self, Dialog):
    14. Dialog.setObjectName("Dialog")
    15. Dialog.resize(QtCore.QSize(QtCore.QRect(0,0,268,184).size()).expandedTo(Dialog.minimumSizeHint()))
    16.  
    17. self.pushButton = QtGui.QPushButton(Dialog)
    18. self.pushButton.setGeometry(QtCore.QRect(60,60,161,61))
    19. self.pushButton.setObjectName("pushButton")
    20.  
    21. self.retranslateUi(Dialog)
    22. QtCore.QMetaObject.connectSlotsByName(Dialog)
    23.  
    24. def retranslateUi(self, Dialog):
    25. Dialog.setWindowTitle(QtGui.QApplication.translate("Dialog", "Dialog", None, QtGui.QApplication.UnicodeUTF8))
    26. self.pushButton.setText(QtGui.QApplication.translate("Dialog", "open window", None, QtGui.QApplication.UnicodeUTF8))
    To copy to clipboard, switch view to plain text mode 


    Saved as "openwindowchild.py"
    Qt Code:
    1. # -*- coding: utf-8 -*-
    2.  
    3. # Form implementation generated from reading ui file 'openwindowchild.ui'
    4. #
    5. # Created: Sat Feb 27 13:49:23 2010
    6. # by: PyQt4 UI code generator 4.3.3
    7. #
    8. # WARNING! All changes made in this file will be lost!
    9.  
    10. from PyQt4 import QtCore, QtGui
    11.  
    12. class Ui_Dialog(object):
    13. def setupUi(self, Dialog):
    14. Dialog.setObjectName("Dialog")
    15. Dialog.resize(QtCore.QSize(QtCore.QRect(0,0,203,227).size()).expandedTo(Dialog.minimumSizeHint()))
    16.  
    17. self.label = QtGui.QLabel(Dialog)
    18. self.label.setGeometry(QtCore.QRect(50,70,131,51))
    19. self.label.setObjectName("label")
    20.  
    21. self.pushButton = QtGui.QPushButton(Dialog)
    22. self.pushButton.setGeometry(QtCore.QRect(40,160,141,51))
    23. self.pushButton.setObjectName("pushButton")
    24.  
    25. self.retranslateUi(Dialog)
    26. QtCore.QMetaObject.connectSlotsByName(Dialog)
    27.  
    28. def retranslateUi(self, Dialog):
    29. Dialog.setWindowTitle(QtGui.QApplication.translate("Dialog", "Dialog", None, QtGui.QApplication.UnicodeUTF8))
    30. self.label.setText(QtGui.QApplication.translate("Dialog", "<html><head><meta name=\"qrichtext\" content=\"1\" /><style type=\"text/css\">\n"
    31. "p, li { white-space: pre-wrap; }\n"
    32. "</style></head><body style=\" font-family:\'Sans Serif\'; font-size:9pt; font-weight:400; font-style:normal;\">\n"
    33. "<p style=\" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;\"><span style=\" font-size:25pt; color:#ff7f50;\">Worked</span></p></body></html>", None, QtGui.QApplication.UnicodeUTF8))
    34. self.pushButton.setText(QtGui.QApplication.translate("Dialog", "Close", None, QtGui.QApplication.UnicodeUTF8))
    To copy to clipboard, switch view to plain text mode 


    saved as "openwindow.py"
    Qt Code:
    1. import sys
    2. from PyQt4 import QtCore, QtGui
    3. from openwindowmain import Ui_Dialog
    4.  
    5.  
    6.  
    7.  
    8.  
    9.  
    10.  
    11.  
    12.  
    13. class StartQT4(QtGui.QMainWindow):
    14. def __init__(self, parent=None):
    15. QtGui.QWidget.__init__(self, parent)
    16. self.ui = Ui_Dialog()
    17. self.ui.setupUi(self)
    18. QtCore.QObject.connect(self.ui.pushButton,QtCore.SIGNAL("clicked()"), self.text_handler)
    19.  
    20.  
    21.  
    22.  
    23. def text_handler(self):
    24. print "worked"
    25.  
    26.  
    27.  
    28.  
    29.  
    30. if __name__ == "__main__":
    31.  
    32. app = QtGui.QApplication(sys.argv)
    33. myapp = StartQT4()
    34. myapp.show()
    35. sys.exit(app.exec_())
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Feb 2010
    Posts
    96
    Thanks
    4
    Thanked 5 Times in 5 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: opening a second window

    I think your code is pretty messy, to start with, making it harder to work with. Take a look at some of my code, because I am sure it will help you.

    main.py:
    Qt Code:
    1. # Version: Traipse 'Pious-Paladin'
    2. # $Id: main.py,v Traipse 'Pious-Paladin' prof.ebral Exp $
    3. #
    4. # Description: This is the main entry point of the oprg application
    5.  
    6. from PyQt4 import QtCore, QtGui
    7. from os import sep
    8.  
    9. from map_engine.map_dock import MapDock
    10. from chat_engine.chat_dock import ChatDock
    11. from net_engine.plist_dock import PListDock
    12. from net_engine.client_gear import ClientGear
    13. from tree_engine.tree_dock import TreeDock
    14. from tool_gears.path_gear import PathTool, path
    15.  
    16. PathTool().load_paths(path)
    17.  
    18. class MainWindow(QtGui.QMainWindow):
    19.  
    20. def __init__(self):
    21. super(MainWindow, self).__init__()
    22. self.setWindowIcon(QtGui.QIcon(path['core_images'] + 'traipse-logo-2.png'))
    23. self.client = ClientGear(self)
    24. self.createActions()
    25. self.createDocks()
    26. self.StatusBar('Ready')
    27. self.createToolBar()
    28. self.createStyle()
    29. self.setCentralWidget(self.center1)
    30.  
    31. def createDocks(self):
    32. #Game Tree Docking Bay
    33. self.treeDock = QtGui.QDockWidget("Dock-Game Tree", self)
    34. self.center4 = TreeDock(self, self.client)
    35. self.treeDock.setWidget(self.center4)
    36. self.treeDock.setFeatures(QtGui.QDockWidget.DockWidgetMovable |
    37. QtGui.QDockWidget.DockWidgetFloatable)
    38. self.center4.add('Child-2')
    39. self.addDockWidget(QtCore.Qt.LeftDockWidgetArea, self.treeDock)
    40.  
    41. #Player List Docking Bay
    42. self.PlayerList = QtGui.QDockWidget("Dock-Player List", self)
    43. self.center3 = PListDock(self, self.client)
    44. self.PlayerList.setWidget(self.center3)
    45. self.PlayerList.setFeatures(QtGui.QDockWidget.DockWidgetMovable |
    46. QtGui.QDockWidget.DockWidgetFloatable)
    47. self.center3.add(2, 'Player-2', 'Idle')
    48. self.center3.add(3, 'Player-3', 'Typing')
    49. self.addDockWidget(QtCore.Qt.LeftDockWidgetArea, self.PlayerList)
    50.  
    51. #Map Docking Bay
    52. self.mapDock = QtGui.QDockWidget("Dock-Map", self)
    53. self.center1 = MapDock(self, self.client)
    54. self.mapDock.setWidget(self.center1)
    55. self.mapDock.setFeatures(QtGui.QDockWidget.DockWidgetMovable |
    56. QtGui.QDockWidget.DockWidgetFloatable)
    57. self.addDockWidget(QtCore.Qt.RightDockWidgetArea, self.mapDock)
    58. self.mapDock.hide()
    To copy to clipboard, switch view to plain text mode 

    This is the Main file. This is my anchor to all my other UI scripts. This is not a complete version, but here you can see I am creating a proxy Player List and proxy Game Tree. I want you to look at the Map Docking Bay. It inserts the MapDock class, passing the parent and the future network client.

    map_dock.py
    Qt Code:
    1. from PyQt4 import QtCore, QtGui
    2. from tool_gears.path_gear import path
    3. from map_engine.tool_gear import mapToolGear
    4. from map_engine.map_gear import MapDocket
    5.  
    6. class MapDock(QtGui.QMainWindow):
    7.  
    8. def __init__(self, parent, client):
    9. super(MapDock, self).__init__()
    10. self.parent = parent; self.client = client
    11. self.createMap()
    12. self.createToolBar()
    13. self.setCentralWidget(self.mapDocket)
    14.  
    15. def createToolBar(self):
    16. self.ToolBay = mapToolGear(self.parent)
    17. self.addToolBar(QtCore.Qt.BottomToolBarArea, self.ToolBay)
    18.  
    19. ## Map Docket
    20. def createMap(self):
    21. self.mapDocket = QtGui.QWidget()
    22. layout = QtGui.QGridLayout()
    23. self.mapView = MapDocket()
    24. layout.addWidget(self.mapView)
    25. self.mapDocket.setLayout(layout)
    26.  
    27. def createMapEditor(self):
    28. self.parent.mapEditor = QtGui.QDialog()
    29. layout = QtGui.QGridLayout()
    30. self.parent.mapEditView = MapDocket()
    31. layout.addWidget(self.parent.mapEditView)
    32. self.parent.mapEditor.setLayout(layout)
    33. self.parent.mapEditor.show()
    34.  
    35. def empty(self):
    36. pass
    To copy to clipboard, switch view to plain text mode 

    Here is the entirety of my map_dock file, which contains the MapDock class. Can you see how I am re using the MapDocket() class to create a map for the core software and a map for the map editor? Maybe I am wrong, but I think that is what you are trying to do. Notice how the second one is rooted in the parent?

    I would suggest you look at your Ui_Dialog Classes. They don't need to declared (object), the should be declared (QtGui.QDialog). I am also not sure why you would create a setupUI function and not use the pre-built __init__ function. I would suggest you use __init__ and then you can create room for you parent object, plus you can build the UI just by calling the class each time.

  3. #3
    Join Date
    Feb 2010
    Posts
    12
    Qt products
    Qt4 PyQt3 PyQt4
    Platforms
    Unix/X11

    Default Re: opening a second window

    thanks you have given me a lot to take in.

    I only had a few minutes tonight so will look it over again tomorrow and post any questions or thoughts then.

    thanks for the reply

  4. #4
    Join Date
    Feb 2010
    Posts
    96
    Thanks
    4
    Thanked 5 Times in 5 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: opening a second window

    You are welcome.

Similar Threads

  1. Qt 4.6.0: Opening a dialog from a main window
    By dmginc in forum Qt Programming
    Replies: 3
    Last Post: 14th January 2010, 12:16
  2. Opening a new window
    By k12yp70n in forum Newbie
    Replies: 1
    Last Post: 26th March 2009, 15:31
  3. Opening a shapefile
    By peace_comp in forum Qt Programming
    Replies: 1
    Last Post: 1st May 2008, 20:59
  4. Opening an URL that contains accents
    By Nyphel in forum Newbie
    Replies: 8
    Last Post: 26th June 2007, 08:13
  5. Help with opening many pictures
    By philipp1 in forum Qt Programming
    Replies: 3
    Last Post: 14th October 2006, 00:13

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.