[GIS] how to open second window in the plugin for QGIS

pyqtpython-2.7qgis-plugins

I create a Mainwindow for my plugin, but if I want to open a second window from this Mainwindow, with click on push button. How can I do?
I tried the following function in my Main Class:

    dialog = QDialog()
    dialog.ui = Ui_SecondWindow()
    dialog.ui.setupUi(dialog)
    dialog.exec_()

But does not work.
I use QGIS 2.4 and PyQt 4.

Best Answer

If you have compiled your Ui file to a .py with pyuic then you setup like this:

yourplugin_dialog.py:

from PyQt4 import QtCore, QtGui 
from ui_yourdialog import Ui_YourDialog

#create the dialog for zoom to point in yourplugin_dialog.py

class YourDialog(QtGui.QDialog): 

    def init(self): QtGui.QDialog.init(self)
       # Set up the user interface from compiled ui file. 
       self.ui = Ui_YourDialog() self.ui.setupUi(self)

Or to compile at runtime from the.ui file:

import os

from PyQt4 import QtGui, uic

FORM_CLASS, _ = uic.loadUiType(os.path.join(
os.path.dirname(__file__), 'yourplugin_ui.ui'))

class YourDialog(QtGui.QDialog, FORM_CLASS):

   def __init__(self, parent=None):
      """Constructor."""
      super(YourDialog, self).__init__(parent)
      self.setupUi(self)

And create the instance wherever you want to use the dialog:

from yourplugin_dialog import YourDialog

    def __init__(self, iface):
       self.dialog_instance = YourDialog()

    def show_dialog(self):
       self.dialog_instance.exec_()