0

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)
  • self.sc = SymbolizacjaConfig(self) edit: self.sc = SymbolizacjaConfig() – Đô Trần Apr 18 '18 at 4:06
2

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.

  • I have changed my code but something is still wrog. Here is an error which appears when clicking a button: super(SymbolizacjaConfig, self).__init__(parent) TypeError: QWidget(QWidget parent=None, Qt.WindowFlags flags=0): argument 1 has unexpected type 'instance' – W. Tom Jul 13 '16 at 7:12
1

I was thinking about difficoult soulution and I just overwrote it all. It was so simple. I must just import dialog for second window, add a method to open new dialog and add a line in init to react when button pushed.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.