Skip to main content
grammar
Source Link
Matt
  • 17.9k
  • 4
  • 23
  • 59

I agree with the comments that some further reading is advisable. That said, here is a small example. Most of the code is taken, or adapted from, the Plugin Builder 3 plugin. I just added some silly functionality to demonstrate the connection between the dialog and the QGIS interface.

It can be run in the Script Editor of the Python console. But, I recommend reading Ian Turtons's link (https://www.qgistutorials.com/en/docs/building_a_python_plugin.html) and buildbuilding a fully fledged plugin, it will be easier to use and distribute in the long-run.

from qgis.PyQt import uic
from qgis.PyQt.QtWidgets import QDialog

ui_file = r".\my_ui_file.ui"

# This loads your .ui file so that PyQt can populate your plugin with the elements from Qt Designer
FORM_CLASS, _ = uic.loadUiType(ui_file)

# this is a customised subclass of QDialog. 
# its appearance is governed initially by your .ui file. 
# but everything within the existing form can be customised,
# or new widgets can be added, within this class. 

class MyCustomDialog(QDialog, FORM_CLASS):
    def __init__(self, parent=None):
        """Constructor."""
        super(MyCustomDialog, self).__init__(parent)
        # Set up the user interface from Designer through FORM_CLASS.
        # After self.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)

        # connect the signals of your form widgets with the desired functionality
        self.pushButton.clicked.connect(self.handle_button)
        self.dial.valueChanged.connect(self.handle_dial)
        
    def handle_button(self):
        self.label.setText(iface.activeLayer().name())
        
    def handle_dial(self):
        iface.mapCanvas().setRotation(self.dial.value())

# instantiate the class    
dlg = MyCustomDialog()

# show the dialog window
dlg.show()

enter image description here

I agree with the comments that some further reading is advisable. That said, here is a small example. Most of the code is taken, or adapted from, the Plugin Builder 3 plugin. I just added some silly functionality to demonstrate the connection between the dialog and the QGIS interface.

It can be run in the Script Editor of the Python console. But, I recommend reading Ian Turtons's link (https://www.qgistutorials.com/en/docs/building_a_python_plugin.html) and build a fully fledged plugin, it will be easier to use and distribute in the long-run.

from qgis.PyQt import uic
from qgis.PyQt.QtWidgets import QDialog

ui_file = r".\my_ui_file.ui"

# This loads your .ui file so that PyQt can populate your plugin with the elements from Qt Designer
FORM_CLASS, _ = uic.loadUiType(ui_file)

# this is a customised subclass of QDialog. 
# its appearance is governed initially by your .ui file. 
# but everything within the existing form can be customised,
# or new widgets can be added, within this class. 

class MyCustomDialog(QDialog, FORM_CLASS):
    def __init__(self, parent=None):
        """Constructor."""
        super(MyCustomDialog, self).__init__(parent)
        # Set up the user interface from Designer through FORM_CLASS.
        # After self.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)

        # connect the signals of your form widgets with the desired functionality
        self.pushButton.clicked.connect(self.handle_button)
        self.dial.valueChanged.connect(self.handle_dial)
        
    def handle_button(self):
        self.label.setText(iface.activeLayer().name())
        
    def handle_dial(self):
        iface.mapCanvas().setRotation(self.dial.value())

# instantiate the class    
dlg = MyCustomDialog()

# show the dialog window
dlg.show()

enter image description here

I agree with the comments that some further reading is advisable. That said, here is a small example. Most of the code is taken, or adapted from, the Plugin Builder 3 plugin. I just added some silly functionality to demonstrate the connection between the dialog and the QGIS interface.

It can be run in the Script Editor of the Python console. But, I recommend reading Ian Turtons's link (https://www.qgistutorials.com/en/docs/building_a_python_plugin.html) and building a fully fledged plugin, it will be easier to use and distribute in the long-run.

from qgis.PyQt import uic
from qgis.PyQt.QtWidgets import QDialog

ui_file = r".\my_ui_file.ui"

# This loads your .ui file so that PyQt can populate your plugin with the elements from Qt Designer
FORM_CLASS, _ = uic.loadUiType(ui_file)

# this is a customised subclass of QDialog. 
# its appearance is governed initially by your .ui file. 
# but everything within the existing form can be customised,
# or new widgets can be added, within this class. 

class MyCustomDialog(QDialog, FORM_CLASS):
    def __init__(self, parent=None):
        """Constructor."""
        super(MyCustomDialog, self).__init__(parent)
        # Set up the user interface from Designer through FORM_CLASS.
        # After self.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)

        # connect the signals of your form widgets with the desired functionality
        self.pushButton.clicked.connect(self.handle_button)
        self.dial.valueChanged.connect(self.handle_dial)
        
    def handle_button(self):
        self.label.setText(iface.activeLayer().name())
        
    def handle_dial(self):
        iface.mapCanvas().setRotation(self.dial.value())

# instantiate the class    
dlg = MyCustomDialog()

# show the dialog window
dlg.show()

enter image description here

added 219 characters in body
Source Link
Matt
  • 17.9k
  • 4
  • 23
  • 59

I agree with the comments that some further reading is advisable. That said, here is a small example. Most of the code is taken, or adapted from, the Plugin Builder 3 plugin. I just added some silly functionality to demonstrate the connection between the dialog and the QGIS interface.

It can be run in the Script Editor of the Python console. But, I recommend reading Ian Turtons's link (https://www.qgistutorials.com/en/docs/building_a_python_plugin.html) and build a fully fledged plugin, it will be easier to use and distribute in the long-run.

from qgis.PyQt import uic
from qgis.PyQt.QtWidgets import QDialog

ui_file = r".\my_ui_file.ui"

# This loads your .ui file so that PyQt can populate your plugin with the elements from Qt Designer
FORM_CLASS, _ = uic.loadUiType(ui_file)

# this is a customised subclass of QDialog. 
# its appearance is governed initially by your .ui file. 
# but everything within the existing form can be customised,
# or new widgets can be added, within this class. 

class MyCustomDialog(QDialog, FORM_CLASS):
    def __init__(self, parent=None):
        """Constructor."""
        super(MyCustomDialog, self).__init__(parent)
        # Set up the user interface from Designer through FORM_CLASS.
        # After self.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)

        # connect the signals of your form widgets with the desired functionality
        self.pushButton.clicked.connect(self.handle_button)
        self.dial.valueChanged.connect(self.handle_dial)
        
    def handle_button(self):
        self.label.setText(iface.activeLayer().name())
        
    def handle_dial(self):
        iface.mapCanvas().setRotation(self.dial.value())

# instanstiateinstantiate the class    
dlg = MyCustomDialog()

# show the dialog window
dlg.show()

enter image description here

I agree with the comments that some further reading is advisable. That said, here is a small example. Most of the code is taken, or adapted from, the Plugin Builder 3 plugin. I just added some silly functionality to demonstrate the connection between the dialog and the QGIS interface.

It can be run in the Script Editor of the Python console. But, I recommend reading Ian Turtons's link (https://www.qgistutorials.com/en/docs/building_a_python_plugin.html) and build a fully fledged plugin, it will be easier to use and distribute in the long-run.

from qgis.PyQt import uic
from qgis.PyQt.QtWidgets import QDialog

ui_file = r".\my_ui_file.ui"

# This loads your .ui file so that PyQt can populate your plugin with the elements from Qt Designer
FORM_CLASS, _ = uic.loadUiType(ui_file)

# this is a customised subclass of QDialog. 
# its appearance is governed initially by your .ui file. 
# but everything within the existing form can be customised,
# or new widgets can be added, within this class. 

class MyCustomDialog(QDialog, FORM_CLASS):
    def __init__(self, parent=None):
        """Constructor."""
        super(MyCustomDialog, self).__init__(parent)
        # Set up the user interface from Designer through FORM_CLASS.
        # After self.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)

        # connect the signals of your form widgets with the desired functionality
        self.pushButton.clicked.connect(self.handle_button)
        self.dial.valueChanged.connect(self.handle_dial)
        
    def handle_button(self):
        self.label.setText(iface.activeLayer().name())
        
    def handle_dial(self):
        iface.mapCanvas().setRotation(self.dial.value())

# instanstiate the class    
dlg = MyCustomDialog()

# show the dialog window
dlg.show()

enter image description here

I agree with the comments that some further reading is advisable. That said, here is a small example. Most of the code is taken, or adapted from, the Plugin Builder 3 plugin. I just added some silly functionality to demonstrate the connection between the dialog and the QGIS interface.

It can be run in the Script Editor of the Python console. But, I recommend reading Ian Turtons's link (https://www.qgistutorials.com/en/docs/building_a_python_plugin.html) and build a fully fledged plugin, it will be easier to use and distribute in the long-run.

from qgis.PyQt import uic
from qgis.PyQt.QtWidgets import QDialog

ui_file = r".\my_ui_file.ui"

# This loads your .ui file so that PyQt can populate your plugin with the elements from Qt Designer
FORM_CLASS, _ = uic.loadUiType(ui_file)

# this is a customised subclass of QDialog. 
# its appearance is governed initially by your .ui file. 
# but everything within the existing form can be customised,
# or new widgets can be added, within this class. 

class MyCustomDialog(QDialog, FORM_CLASS):
    def __init__(self, parent=None):
        """Constructor."""
        super(MyCustomDialog, self).__init__(parent)
        # Set up the user interface from Designer through FORM_CLASS.
        # After self.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)

        # connect the signals of your form widgets with the desired functionality
        self.pushButton.clicked.connect(self.handle_button)
        self.dial.valueChanged.connect(self.handle_dial)
        
    def handle_button(self):
        self.label.setText(iface.activeLayer().name())
        
    def handle_dial(self):
        iface.mapCanvas().setRotation(self.dial.value())

# instantiate the class    
dlg = MyCustomDialog()

# show the dialog window
dlg.show()

enter image description here

added 219 characters in body
Source Link
Matt
  • 17.9k
  • 4
  • 23
  • 59

I agree with the comments that some further reading is advisable. That said, here is a small example. Most of the code is taken, or adapted from, the Plugin Builder 3 plugin. I just added some silly functionality to demonstrate the connection between the dialog and the QGIS interface.

It can be run in the Script Editor of the Python console. But, I recommend reading Ian Turtons's link (https://www.qgistutorials.com/en/docs/building_a_python_plugin.html) and build a fully fledged plugin, it will be easier to use and distribute in the long-run.

from qgis.PyQt import uic
from qgis.PyQt.QtWidgets import QDialog

ui_file = r".\my_ui_file.ui"

# This loads your .ui file so that PyQt can populate your plugin with the elements from Qt Designer
FORM_CLASS, _ = uic.loadUiType(ui_file)

# this is a customised subclass of QDialog. 
# its appearance is governed initially by your .ui file. 
# but everything within the existing form can be customised,
# or new widgets can be added, within this class. 

class MyCustomDialog(QDialog, FORM_CLASS):
    def __init__(self, parent=None):
        """Constructor."""
        super(MyCustomDialog, self).__init__(parent)
        # Set up the user interface from Designer through FORM_CLASS.
        # After self.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)

        # connect the signals of your form widgets with the desired functionality
        self.pushButton.clicked.connect(self.handle_button)
        self.dial.valueChanged.connect(self.handle_dial)
        
    def handle_button(self):
        self.label.setText(iface.activeLayer().name())
        
    def handle_dial(self):
        iface.mapCanvas().setRotation(self.dial.value()) 

# instanstiate the class    
dlg = MyCustomDialog()

# show the dialog window
dlg.show()

enter image description here

I agree with the comments that some further reading is advisable. That said, here is a small example. Most of the code is taken, or adapted from, the Plugin Builder 3 plugin. I just added some silly functionality to demonstrate the connection between the dialog and the QGIS interface.

It can be run in the Script Editor of the Python console. But, I recommend reading Ian Turtons's link (https://www.qgistutorials.com/en/docs/building_a_python_plugin.html) and build a fully fledged plugin, it will be easier to use and distribute in the long-run.

from qgis.PyQt import uic
from qgis.PyQt.QtWidgets import QDialog

ui_file = r".\my_ui_file.ui"

# This loads your .ui file so that PyQt can populate your plugin with the elements from Qt Designer
FORM_CLASS, _ = uic.loadUiType(ui_file)

class MyCustomDialog(QDialog, FORM_CLASS):
    def __init__(self, parent=None):
        """Constructor."""
        super(MyCustomDialog, self).__init__(parent)
        # Set up the user interface from Designer through FORM_CLASS.
        # After self.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)

        # connect the signals of your form widgets with the desired functionality
        self.pushButton.clicked.connect(self.handle_button)
        self.dial.valueChanged.connect(self.handle_dial)
        
    def handle_button(self):
        self.label.setText(iface.activeLayer().name())
        
    def handle_dial(self):
        iface.mapCanvas().setRotation(self.dial.value())
    
dlg = MyCustomDialog()
dlg.show()

enter image description here

I agree with the comments that some further reading is advisable. That said, here is a small example. Most of the code is taken, or adapted from, the Plugin Builder 3 plugin. I just added some silly functionality to demonstrate the connection between the dialog and the QGIS interface.

It can be run in the Script Editor of the Python console. But, I recommend reading Ian Turtons's link (https://www.qgistutorials.com/en/docs/building_a_python_plugin.html) and build a fully fledged plugin, it will be easier to use and distribute in the long-run.

from qgis.PyQt import uic
from qgis.PyQt.QtWidgets import QDialog

ui_file = r".\my_ui_file.ui"

# This loads your .ui file so that PyQt can populate your plugin with the elements from Qt Designer
FORM_CLASS, _ = uic.loadUiType(ui_file)

# this is a customised subclass of QDialog. 
# its appearance is governed initially by your .ui file. 
# but everything within the existing form can be customised,
# or new widgets can be added, within this class. 

class MyCustomDialog(QDialog, FORM_CLASS):
    def __init__(self, parent=None):
        """Constructor."""
        super(MyCustomDialog, self).__init__(parent)
        # Set up the user interface from Designer through FORM_CLASS.
        # After self.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)

        # connect the signals of your form widgets with the desired functionality
        self.pushButton.clicked.connect(self.handle_button)
        self.dial.valueChanged.connect(self.handle_dial)
        
    def handle_button(self):
        self.label.setText(iface.activeLayer().name())
        
    def handle_dial(self):
        iface.mapCanvas().setRotation(self.dial.value()) 

# instanstiate the class    
dlg = MyCustomDialog()

# show the dialog window
dlg.show()

enter image description here

Source Link
Matt
  • 17.9k
  • 4
  • 23
  • 59
Loading