How to use multiple windows in Python Plugin - python

I wrote a plugin in PyQGIS, which contains several Ui's (using QT Designer). When I run the code in the QGIS Python console, it works wonderfully. Now I would like to make it available internally for the company as a classic QGIS plugin (start in menu bar). It always worked well with previous plugins, but there was always only one Ui.
At its core there are three important files. 1. __ init __.py, 2. geowerkzeug.py, which is responsible for starting from the menu, and 3. functionality.py, which contains all my functions.
##__init__.py
from Plugin.geowerkzeug import GeoWerkzeug
def classFactory(iface):
plugin = GeoWerkzeug(iface)
return plugin
Now the geowerkzeug.py:
from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
from plugin.functionality import *
from qgis.utils import iface
import os
class GeoWerkzeug:
def __init__ (self, iface):
self.iface = iface
def initGui (self):
self.startButton = QAction("Plugin starten", self.iface.mainWindow())
self.iface.addPluginToMenu('Plugin', self.startButton)
self.startButton.triggered.connect(self.maskeAufrufen)
def unload (self):
self.iface.removePluginMenu('Plugin', self.startButton)
def maskeAufrufen (self):
self.gui = MainWindow(self)
dock_widget = QDockWidget("Plugin", self.iface.mainWindow())
dock_widget.setWidget(self.gui)
self.iface.addDockWidget(QtCore.Qt.RightDockWidgetArea, dock_widget)
dock_widget.setAllowedAreas(QtCore.Qt.RightDockWidgetArea)
self.gui.show()
Up to here it works. MainWindow is the first class on functionality.py. The window will appear. But if I click on a button to switch to the next Ui (class), the Ui does not change. I have a total of 17 Ui's in my plugin. Here I only show two classes as an example.
Now the functionality.py:
from PyQt5.uic import loadUi
from PyQt5 import QtWidgets
from PyQt5.QtWidgets import *
from PyQt5 import *
from qgis.core import *
from qgis.utils import iface
from qgis.gui import *
import processing
from PyQt5 import uic
import os
pluginPath = os.path.dirname(__file__)
uiPath = os.path.join(pluginPath, 'mainwindow.ui')
uiPath_second_window = os.path.join(pluginPath, 'window2.ui')
WIDGET, BASE = uic.loadUiType(uiPath)
widget = QDockWidget()
class MainWindow(BASE, WIDGET):
def __init__(self, parent=None):
super(MainWindow, self).__init__()
self.gui = loadUi(uiPath, self)
self.window_second.clicked.connect(self.next_window)
def next_window(self):
window_the_second=Second_window()
widget.setWidget(window_the_second)
class Second_window(BASE, WIDGET):
def __init__(self):
super(Second_window, self).__init__()
self.gui = loadUi(uiPath_second_window , self)
My biggest problem with understanding is how to correctly link my code from functionality.py (which runs in the console) with the other two files. The next problem is that I don't even get an error message that I could build on. The plugin is in the menu bar and it can be started, but after that I can't get any further. I hope my explanations are understandable.

The main issue which causes the second window not to appear, is that you never show the QDockWidget. With widget.setWidget(window_the_second) you set the content, but it doesn't make the window appear.
In your method that shows the first window, add the dockwidget with self.iface.addDockWidget(QtCore.Qt.RightDockWidgetArea, widget)
then set your first window as content of that dockwidget (widget.setWidget(self.gui)).
In general, you got some things a bit mixed up, so I'll try to clarify two points.
The contents of a window are set by subclassing the results of uic.loadUiType. In order to have a second window display your second ui, you can't sublass the same BASE and WIDGET. Call uic.loadUiType for each of your UI-Files instead.
You are missing a call to setupUi() to initialize the UI. Then you can also get rid of any loadUi.
Implementing my advices then results in the following:
WIDGET, BASE = uic.loadUiType('1.ui')
WIDGET2, BASE2 = uic.loadUiType('2.ui')
widget = QDockWidget()
class GeoWerkzeug:
def __init__ (self, iface):
self.iface = iface
def initGui (self):
self.startButton = QAction("Plugin starten", self.iface.mainWindow())
self.iface.addPluginToMenu('Plugin', self.startButton)
self.startButton.triggered.connect(self.maskeAufrufen)
def unload (self):
self.iface.removePluginMenu('Plugin', self.startButton)
def maskeAufrufen (self):
# add DockWidget to GUI
self.iface.addDockWidget(QtCore.Qt.RightDockWidgetArea, widget)
widget.setAllowedAreas(QtCore.Qt.RightDockWidgetArea)
# set first window as content of the DockWidget
self.gui = MainWindow(self)
widget.setWidget(self.gui)
class MainWindow(BASE, WIDGET):
def __init__(self, parent=None):
super(MainWindow, self).__init__()
self.setupUi(self)
self.window_second.clicked.connect(self.next_window)
def next_window(self):
# set second window as content of the DockWidget
window_the_second=Second_window()
widget.setWidget(window_the_second)
class Second_window(BASE2, WIDGET2):
def __init__(self):
super(Second_window, self).__init__()
self.setupUi(self)

Related

Pyqt5 Crashes without error when i specifically double left click (double right works)

I'm creating this application in Python that talks with Outlook and retrieve some data about the amount of mails in each folder and some other stuff and I'm using PyQt5 to do so.
But some werid thing happened when I assigned a signal to a function:
self.table_widget.itemDoubleClicked.connect(self.some_function)
If I double right-click this item, everything runs just fine. But if I do it with the left-click, it just freezes, and then crashes without any error screen whatsoever (usually if you execute the program, when it crashes you can see some stack on the console behind, right? In this case literally nothing shows up).
I'm not sure if I'm using he signals thing right, so there might be something...?
Anyways, since no error occurred, I tried to put some prints to see where it would crash, but it turns out that the t2 screen loads just fine, and apparently it crashes when the code goes back to the main loop...? I don't know, it's impossible for me to debug that stuff.
I'll try to put here only the essential code so it gets easier to see the mistake, but I can show some details if this is not enough.
EDIT
As requested, I did a minimum reproducible example below, it does crash in the same way:
from PyQt5.QtCore import QDateTime, Qt, QTimer, pyqtSignal
from PyQt5.QtWidgets import QApplication, QMainWindow, QDialog, QTableWidgetItem
from PyQt5 import uic
import sys, os
class t0(QMainWindow):
switch_window = pyqtSignal()
def __init__(self):
super(t0, self).__init__()
uic.loadUi(Controller.application_path + r'\UI\t0.ui', self)
#This .ui file has pretty much a button to click
self.btn_ok.clicked.connect(self.onButtonClick1)
def onButtonClick1(self):
self.switch_window.emit()
class t1(QDialog):
switch_window = pyqtSignal()
def __init__(self, parent=None):
super(t1, self).__init__(parent)
uic.loadUi(Controller.application_path + r'\UI\t1.ui', self)
#This .ui file has a QTableWidget
self.tw_indi.setColumnCount(1)
self.tw_indi.setRowCount(1)
self.tw_indi.setItem(0, 0, QTableWidgetItem("*"))
self.tw_indi.itemDoubleClicked.connect(self.direcionar_detalhes)
def direcionar_detalhes(self, item):
self.switch_window.emit()
class t2(QDialog):
switch_window = pyqtSignal(str)
def __init__(self, parent=None):
super(t2, self).__init__(parent)
uic.loadUi(Controller.application_path + r'\UI\t2.ui', self)
#This .ui file is identical as the t1.ui, just some minor changes
class Controller():
controller = None
#questões técnicas do módulo os com pyinstaller
if getattr(sys, 'frozen', False):
application_path = sys.exec_prefix
else:
application_path = os.path.dirname(os.path.abspath(__file__))
tela = None
app = QApplication(sys.argv)
def __init__(self):
Controller.controller = self
self.mostra_carregamento()
sys.exit(Controller.app.exec_())
def mostra_carregamento(self):
self.view = t0()
self.view.switch_window.connect(self.mostra_indicador)
self.view.show()
def mostra_indicador(self):
self.view.close()
self.view = t1()
self.view.switch_window.connect(self.sinal_indicador)
self.view.show()
def sinal_indicador(self):
self.view.close()
self.view = t2()
self.view.show()
if __name__ == '__main__':
control = Controller()
EDIT 2
Here's the link for the ui's:
https://drive.google.com/file/d/1-iFNaWUGtJ4687nTcTFtb7WRKUL37als/view?usp=drivesdk
The problem is not the signal, but when you want to reuse the same variable for a new window, you are eliminating the memory of that object that is still visible and therefore running, generating unreserved memory access. The solution is to use different variable names:
class Controller:
# ...
def mostra_carregamento(self):
self.view0 = t0()
self.view0.switch_window.connect(self.mostra_indicador)
self.view0.show()
def mostra_indicador(self):
self.view0.close()
self.view1 = t1()
self.view1.switch_window.connect(self.sinal_indicador)
self.view1.show()
def sinal_indicador(self):
self.view1.close()
self.view2 = t2()
self.view2.show()

Can't open multiple windows in PyQt5

so I've made several GUIs using the PyQt designer and I aim for each of them to link with eachother and for them to be able to open eachother. I have gotten the start up page to open up the home page, however when I click the respective buttons on the home page to open the other pages, it crashes my program. Any help would in fixing this would be greatly appreciated.
My code is below:
import sys
from PyQt5 import QtGui, QtCore, QtWidgets
from StartupPage import Ui_StartupWindow
from HomePage import Ui_HomeWindow
from FoodPage import Ui_FoodWindow
from ExercisePage import Ui_ExerciseWindow
from ProfilePage import Ui_ProfileWindow
class Startup(QtWidgets.QMainWindow, Ui_StartupWindow):
def __init__(self, parent=None):
super(Startup, self).__init__(parent)
self.setupUi(self)
self.NewEntryButton.clicked.connect(self.NewButtonHandle)
self.ContinueButton.clicked.connect(self.ContinueButtonHandle)
self.HomeP=Home()
def NewButtonHandle(self):
self.HomeP.show()
def ContinueButtonHandle(self):
self.HomeP.show()
class Home(QtWidgets.QMainWindow, Ui_HomeWindow):
def __init__(self, parent=None):
QtWidgets.QMainWindow.__init__(self, parent)
self.setupUi(self)
self.FoodP=Food()
self.ExerciseP=Exercise()
self.ProfileP=Profile()
self.exerciseButton.clicked.connect(self.ExerciseButtonHandle)
self.foodButton.clicked.connect(self.FoodButtonHandle)
self.profileButton.clicked.connect(self.ProfileButtonHandle)
def ExerciseButtonHandle():
self.ExerciseP.show()
def FoodButtonHandle():
self.FoodP.show()
def ProfileButtonHandle():
self.ProfileP.show()
class Food(QtWidgets.QMainWindow, Ui_FoodWindow):
def __init__(self, parent=None):
QtWidgets.QMainWindow.__init__(self, parent)
self.setupUi(self)
class Exercise(QtWidgets.QMainWindow, Ui_ExerciseWindow):
def __init__(self, parent=None):
QtWidgets.QMainWindow.__init__(self, parent)
self.setupUi(self)
class Profile(QtWidgets.QMainWindow, Ui_ProfileWindow):
def __init__(self, parent=None):
QtWidgets.QMainWindow.__init__(self, parent)
self.setupUi(self)
if __name__ == '__main__':
app = QtWidgets.QApplication(sys.argv)
window = Startup()
window.show()
sys.exit(app.exec_())
try to move import sys to:
if __name__ == '__main__':
import sys """<--------------------------here"""
app = QtWidgets.QApplication(sys.argv)
window = Startup()
window.show()
sys.exit(app.exec_())
try to check that other you import modules such as this.
It could be useful to know what error message is displayed when the program crashes.
Having said that, I was fiddling with something similar recently and the below seems to work fine. Note it is mostly the same as what you were doing.
Also, I had wanted to prevent interaction with the main (home) window when any of it's child windows (window1, window2, window3) were open, to force the user to focus on one task at a time rather than opening multiple windows - hence the setEnabled() calls. The
self.Window1.setEnabled(True)
line is required in home.toWindow1() because the Window1 is a member of home, and as such gets disabled along with all other members of home as a result of the first setEnabled call.
Also, while I did put my
import sys
in the main method as suggested by Volodymyr, that's just for the sake of neatness. As references to sys will resolve just as well (scoping rules, LEGB) if the import is the first line of code or the first line in the main method, I don't see how having it in either location could cause any issues (unless it was imported at the start of the file and then later the variable sys was assigned a different value).
I also don't see that altering the import, as suggested by Bahnzo, from
from HomePage import Ui_HomeWindow
to
import HomePage
and then later referring to Ui_HomeWindow as HomePage.Ui_HomeWindow would make any difference, for the same reason - the only case it would is if Ui_homePage had some value explicitly assigned to it after the import occurred and before it was used in the class definition - and you can see that doesn't happen in the OP's code.
#!/bin/python3
from PyQt5.QtWidgets import QApplication, QMainWindow
from startwindow import Ui_startWindow
from homewindow import Ui_homeWindow
from window1 import Ui_Window1
from window2 import Ui_Window2
from window3 import Ui_Window3
class start(QMainWindow, Ui_startWindow):
def __init__(self):
super(start, self).__init__(None)
self.setupUi(self)
self.homeButton.clicked.connect(self.toHome)
self.home = home()
def toHome(self):
self.home.show()
self.hide()
class home(QMainWindow, Ui_homeWindow):
def __init__(self):
super(home, self).__init__(None)
self.setupUi(self)
self.window1Button.clicked.connect(self.toWindow1)
self.window2Button.clicked.connect(self.toWindow2)
self.window3Button.clicked.connect(self.toWindow3)
self.Window1 = window1(self)
self.Window2 = window2(self)
self.Window3 = window3(self)
def toWindow1(self):
self.Window1.show()
self.setEnabled(False)
self.Window1.setEnabled(True)
def toWindow2(self):
self.Window2.show()
self.setEnabled(False)
self.Window2.setEnabled(True)
def toWindow3(self):
self.Window3.show()
self.setEnabled(False)
self.Window3.setEnabled(True)
def reEnable(self):
self.setEnabled(True)
class window1(QMainWindow, Ui_Window1):
def __init__(self, home):
super(window1, self).__init__(home)
self.home = home
self.setupUi(self)
self.homeButton.clicked.connect(self.toHome)
def toHome(self):
self.home.setEnabled(True)
self.hide()
class window2(QMainWindow, Ui_Window2):
def __init__(self, home):
super(window2, self).__init__(home)
self.home = home
self.setupUi(self)
self.homeButton.clicked.connect(self.toHome)
def toHome(self):
self.home.setEnabled(True)
self.hide()
class window3(QMainWindow, Ui_Window3):
def __init__(self, home):
super(window3, self).__init__(home)
self.home = home
self.setupUi(self)
self.homeButton.clicked.connect(self.toHome)
def toHome(self):
self.home.setEnabled(True)
self.hide()
Try this:
import Homepage #import the whole thing
then:
class Home(QtWidgets.QMainWindow, Homepage.Ui_HomeWindow):
#import the UI there

PyQt4 signals and slots - QToolButton

In PyQt4 I have a main window which when the settings button is clicked opens the settings dialog
from PyQt4 import QtCore, QtGui
import ui_Design, ui_Settings_Design
class MainDialog(QtGui.QMainWindow, ui_Design.Ui_arbCrunchUI):
def __init__(self, parent=None):
super(MainDialog, self).__init__(parent)
self.setupUi(self)
self.settingsBtn.clicked.connect(lambda: self.showSettings())
def showSettings(self):
dialog = QtGui.QDialog()
dialog.ui = SettingsDialog()
dialog.ui.setupUi(dialog)
dialog.exec_()
The above works and my SettingsDialog is displayed but I cant get the setPageIndex to work
class SettingsDialog(QtGui.QDialog, ui_Settings_Design.Ui_SettingsDialog):
def __init__(self, parent=None):
super(SettingsDialog, self).__init__(parent)
self.setupUi(self)
self.bookSettingsBtn.clicked.connect(self.setPageIndex)
#QtCore.pyqtSlot()
def setPageIndex(self):
print 'selected'
self.settingsStackedWidget.setCurrentIndex(0)
The bookSettingsBtn is a QToolButton
self.bookSettingsBtn = QtGui.QToolButton(self.navigationFrame)
And the settingsStackedWidget is a QStackedWidget
self.settingsStackedWidget = QtGui.QStackedWidget(SettingsDialog)
At this point I am pretty confused on signals and slots and nothing I have read clears this up - if anyone can point out what I am doing wrong above and also potentially direct me to a good (beginners) guide / tutorial on signals and slots it would be greatly appreciated
I would also like to know how to make setPageIndex work as follows:
def setPageIndex(self, selection):
self.settingsStackedWidget.setCurrentIndex(selection)
I'm not sure why you're doing the following, but that's the issue:
def showSettings(self):
dialog = QtGui.QDialog()
dialog.ui = SettingsDialog()
dialog.ui.setupUi(dialog)
dialog.exec_()
SettingsDialog itself is a proper QDialog. You don't need to instantiate another QDialog.
Right now, you're creating an empty QDialog and then populate it with the same ui as SettingsDialog (i.e. setupUi(dialog)), then you show this dialog. But... The signal connection is for SettingsDialog, and the dialog you're showing doesn't have that.
Basically, you don't need that extra QDialog at all. The following should be enough:
def showSettings(self):
dialog = SettingsDialog()
dialog.exec_()
Ok. So here is an example how you pass an argument to a slot
from functools import partial
# here you have a button bookSettingsBtn:
self.bookSettingsBtn = QtGui.QPushButton("settings")
self.bookSettingsBtn.clicked.connect(partial(self.setPageIndex, self.bookSettingsBtn.text()))
#pyqtSlot(str) # this means the function expects 1 string parameter (str, str) 2 string parameters etc.
def setPageIndex(self, selection):
print "you pressed button " + selection
In your case the argument would be an int. Of course you have to get the value from somewhere
and then put it in the partial part as the argument (here I just used the text of the button),
but you can use int, bool etc. Just watch the slot signature.
Here is a tutorial that helped me:
http://zetcode.com/gui/pyqt4/
I hope this helps.
Hey here I have a fully running example (just copy paste it in a python file and run it):
Maybe this helps you. It's a small example but here you see how it works.
from PyQt4.QtCore import *
from PyQt4.QtGui import *
from functools import partial
class MyForm(QMainWindow):
def __init__(self, parent=None):
super(MyForm, self).__init__(parent)
button1 = QPushButton('Button 1')
button2 = QPushButton('Button 2')
button1.clicked.connect(partial(self.on_button, button1.text()))
button2.clicked.connect(partial(self.on_button, button1.text()))
layout = QHBoxLayout()
layout.addWidget(button1)
layout.addWidget(button2)
main_frame = QWidget()
main_frame.setLayout(layout)
self.setCentralWidget(main_frame)
#pyqtSlot(str)
def on_button(self, n):
print "Text of button is: " + str(n)
if __name__ == "__main__":
import sys
app = QApplication(sys.argv)
form = MyForm()
form.show()
app.exec_()
So I dont really understand why but changing the way the settingsDialog is called from the MainWindow has fixed my problem. I guess the parent window needed to be specified??:
class MainDialog(QtGui.QMainWindow, ui_Design.Ui_arbCrunchUI):
....
def showSettings(self):
self.settingsDialog = QtGui.QDialog(self)
self.settingsDialog.ui = SettingsDialog(self)
self.settingsDialog.ui.show()
class SettingsDialog(QtGui.QDialog, ui_Settings_Design.Ui_SettingsDialog):
def __init__(self, parent=None):
super(SettingsDialog, self).__init__(parent)
self.setupUi(self)
self.bookSettingsBtn.clicked.connect(partial(self.setPageIndex, 1))
#QtCore.pyqtSlot(int)
def setPageIndex(self, selection):
self.settingsStackedWidget.setCurrentIndex(selection)

Qt mdi application with custiom UI from QtDesigner

Assume I have two UI files from Qt Designer:mainform.ui stores mdiArea and figureslist.ui stores listView.
Now I'd like to create a mdi application, that can open numbers of figureList windows.
import sys
from PyQt4 import QtGui
#from PyQt4.QtGui import *
from PyQt4 import QtCore, QtGui, uic
class HelloWorldApplication(QtGui.QApplication):
def __init__(self, args):
QtGui.QApplication.__init__(self, args)
self.maindialog = MainUI(None)
class MainUI(QtGui.QMainWindow):
def __init__(self, parent):
QtGui.QMainWindow.__init__(self, parent)
self.ui = uic.loadUi("mainform.ui")
self.ui.show()
# create child and show it
child = self.createFiguresListView()
# problem here (*)
child.show()
def createFiguresListView(self):
child = FiguresListView()
self.ui.mdi.addSubWindow(child)
return child
class FiguresListView(QtGui.QWidget):
def __init__(self):
super(FiguresListView, self).__init__()
self.ui = uic.loadUi("figureslist.ui")
app = HelloWorldApplication(sys.argv)
sys.exit(app.exec_())
But unfortunately my child window shows up collapsed without layout described in figureslist.ui, but acts like mdi child, but if I replace code marked with (*) to child.ui.show() it shows actual layout, but doesn't act like mdi child.
What's wrong?
You forgot to set the parent for the ui (also, if you didn't specified minimum size in Designer, you need to do it here):
class FiguresListView(QtGui.QWidget):
def __init__(self):
super(FiguresListView, self).__init__()
self.ui = uic.loadUi("figureslist.ui", self)
#self.setMinimumSize(400, 200)

Running python files

I am working on pyqt4 and python26 application.I created forms using qt designer (.ui files).
I converted them to .py and .pyc files.But when i try to run .py file ,python command line comes and goes within a second,the form (corresponding .ui file) cannot be seen...what can be the problem??
this is my code:(.py file)
from DlgAbout_ui import Ui_DlgAbout
from PyQt4.QtCore import *
from PyQt4.QtGui import *
import resources
class DlgAbout(QDialog, Ui_DlgAbout):
def __init__(self, parent=None):
QDialog.__init__(self, parent)
self.setupUi(self)
self.logo.setPixmap( QPixmap( ":/icons/faunalia_logo.png" ) )
text = self.txt.toHtml()
text = text.replace( "$PLUGIN_NAME$", "RT Sql Layer" )
self.txt.setHtml(text)
First, don't use:
from PyQt4.QtCore import *
from PyQt4.QtGui import *
Instead:
from PyQt4 import QtCore, QtGui
And reference the modules explicitly.
class DlgAbout(QtGui.QDialog, Ui_DlgAbout):
etc.
In your code, all you've done is defined a dialog box. You haven't defined any main application to run, or any way to show the dialog box.
For an example, here's a basic main application to run:
from PyQt4 import QtGui
import sys
class MyMainWindow(QtGui.QMainWindow):
def __init__(self, parent=None):
super(MyMainWindow, self).__init__(parent)
self.form_widget = FormWidget(self)
self.setCentralWidget(self.form_widget)
class FormWidget(QtGui.QWidget):
def __init__(self, parent):
super(FormWidget, self).__init__(parent)
self.layout = QtGui.QVBoxLayout(self)
self.button = QtGui.QPushButton("Button!")
self.layout.addWidget(self.button)
if __name__ == "__main__":
app = QtGui.QApplication([])
foo = MyMainWindow()
foo.show()
sys.exit(app.exec_())
This defines a main window, and a form (Which MyMainWindow sets up, as you can see).
I then check if this is the main file being run (if __name__ == "__main__":), and I start the application (The app = QtGui.QApplication([]), create the main window, and show the main window.
In your case, you could define a main application like I did, and make it alert your QDialog.
Your python code just imports some modules and then defines a new class. It doesn't do anything with the class it has defined, though. In other words, once Python is done creating the new class, it is finished, and it exits.
I don't know PyQT at all, but most likely you need to start the GUI's main loop, and also instantiate an instance of your new class and pass it to PyQT.

Categories