Answers for "qgis canvas as pyqt widget"

0

qgis canvas as pyqt widget

from PyQt5 import QtCore, QtGui, QtWidgets
from qgis.gui import QgsMapCanvas
from qgis.core import QgsVectorLayer

class Ui_MainWindow(object):
    def setupUi(self, MainWindow):
        self.main_window = MainWindow
        self.main_window.setObjectName("MainWindow")
        self.main_window.resize(800, 600)
        self.centralwidget = QtWidgets.QWidget(self.main_window)
        self.centralwidget.setObjectName("centralwidget")
        self.horizontalLayout = QtWidgets.QHBoxLayout(self.centralwidget)
        self.horizontalLayout.setObjectName("horizontalLayout")
        self.pushButton = QtWidgets.QPushButton(self.centralwidget)
        self.pushButton.setObjectName("pushButton")
        self.horizontalLayout.addWidget(self.pushButton)
        self.widget = QgsMapCanvas(self.centralwidget)
        self.widget.setObjectName("widget")
        self.horizontalLayout.addWidget(self.widget)
        self.main_window.setCentralWidget(self.centralwidget)
        self.menubar = QtWidgets.QMenuBar(self.main_window)
        self.menubar.setGeometry(QtCore.QRect(0, 0, 800, 26))
        self.menubar.setObjectName("menubar")
        self.main_window.setMenuBar(self.menubar)
        self.statusbar = QtWidgets.QStatusBar(self.main_window)
        self.statusbar.setObjectName("statusbar")
        self.main_window.setStatusBar(self.statusbar)

        self.retranslateUi(self.main_window)
        QtCore.QMetaObject.connectSlotsByName(self.main_window)

        self.pushButton.clicked.connect(self.open_new_mapcanvas_window)
        # add a layer to map canvas
        self.layer = QgsVectorLayer("ADD/FILE/PATH", 'layer', 'ogr')
        self.widget.setExtent(self.layer.extent())
        self.widget.setLayers([self.layer])

    def open_new_mapcanvas_window(self):
        self.new_window = QtWidgets.QMainWindow()
        self.new_window.showMaximized()
        self.new_window.setCentralWidget(self.widget)
        self.new_window.show()


    def retranslateUi(self, MainWindow):
        _translate = QtCore.QCoreApplication.translate
        MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
        self.pushButton.setText(_translate("MainWindow", "Fullscreen"))


class Main(QtWidgets.QMainWindow, Ui_MainWindow):
    def __init__(self, parent=None):
        super(Main, self).__init__(parent)
        self.setupUi(self)

if __name__ == "__main__":
    import sys
    app = QtWidgets.QApplication(sys.argv)
    MainWindow = QtWidgets.QMainWindow()
    ui = Ui_MainWindow()
    ui.setupUi(MainWindow)
    MainWindow.show()
    sys.exit(app.exec_())
Posted by: Guest on March-14-2021

Browse Popular Code Answers by Language