[GIS] When buton clicked, open new window – pyqgis

connectionpyqgispyqtpython

I'm making a plugin which has two windows. What I want to know is how to refer pushButton in one window to method which opens new window.

I have dialog *.ui files for both windows and their *.py. Should I have a method in my main_plugin.py file which opens new window? Should I have two classes in my main_plugin.py file or should I make a new file for new window 'body'? Is there anything else I should take into consideration?

Maybe someone have a code with similar button-window connection?

EDIT:
I tried to do it like dmh126 said, but something is still wrong. Here is my code I what had I changed:

plugin_dialog.py:

import os
from PyQt4 import QtGui, uic

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


class SymbolizacjaDialog(QtGui.QDialog, FORM_CLASS):
    def __init__(self, parent=None):
        """Constructor."""
        super(SymbolizacjaDialog, self).__init__(parent)
        # Set up the user interface from Designer.
        # After setupUI you can access any designer object by doing
        # self.<objectname>, and you can use autoconnect slots - see
        # http://qt-project.org/doc/qt-4.8/designer-using-a-ui-file.html
        # #widgets-and-dialogs-with-auto-connect
        self.setupUi(self)

plugin_config_dialog.py:

import os
from PyQt4 import QtGui, uic

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


class SymbolizacjaConfigDialog(QtGui.QDialog, FORM_CLASS):
    def __init__(self, parent=None):
        """Constructor."""
        super(SymbolizacjaConfigDialog, self).__init__(parent)
        # Set up the user interface from Designer.
        # After setupUI you can access any designer object by doing
        # self.<objectname>, and you can use autoconnect slots - see
        # http://qt-project.org/doc/qt-4.8/designer-using-a-ui-file.html
        # #widgets-and-dialogs-with-auto-connect
        self.setupUi(self)

plugin.py:

in main class (Symbolizacja):

from Symbolizacja_dialog import SymbolizacjaDialog
from Symbolizacja_config_dialog import SymbolizacjaConfigDialog

in def init(self, iface):

self.dlg.pushButton.clicked.connect(self.handleButton)

added new method:

def handleButton(self):
        self.sc = SymbolizacjaConfig(self)
        self.sc.show()

added new class (at the end):

class SymbolizacjaConfig(QtGui.QWidget):
    def __init__(self, parent):
        super(SymbolizacjaConfig, self).__init__(parent)

Best Answer

You can create a new dialog as a class, even in your main .py file.

class NewDialog(QtGui.QWidget):
  def __init__(self, parent):
    super(NewDialog, self).__init__(parent)

Than in your main class you can put function like that:

...
def open_new_dialog(self):
    self.nd = NewDialog(self)
    self.nd.show()
...

This function creates a new dialog and display it.

Now just create a connection in your main class init:

class Plugin:
    """QGIS Plugin Implementation."""
    def __init__(self, iface):
    ...
    self.dlg.button.connect(self.open_new_dialog)

Of course you can do it on a different way. Ex. you can create self.nd = NewDialog(self) in main class in init and only self.nd.show() it in this function. And remember about passing objects like parent (it is your main dialog window) and iface if you want to use them with this new window.

Related Question