Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PyQt : Prevent a window to be opened several times

I made the simple code below as example. It justs open a new window clicking on a button. I don't find a way to prevent this widget to be re-opened if it is already on the screen. I would like to open a QDialog warning if the window already exists and mainly to have the closeEvent method sending a signal to Mainwidget saying that the new window has been closed. This would allow to open the newWidget again.

import sys
from PyQt4 import QtCore, QtGui

class NewWidget(QtGui.QWidget): 
    def __init__(self, parent=None):
        super(NewWidget,self).__init__(parent)

        self.lineEdit = QtGui.QLineEdit('new window',self)
        self.resize(200,50)
        self.show()

    def closeEvent(self,ev):

        self.Exit = QtGui.QMessageBox.question(self,
                  "Confirm Exit...",
                  "Are you sure you want to exit ?",
                  QtGui.QMessageBox.Yes| QtGui.QMessageBox.No)
        ev.ignore()

        if self.Exit  == QtGui.QMessageBox.Yes:            
            ev.accept()     

class MainWidget(QtGui.QWidget):
    def __init__(self, parent=None):
        super(MainWidget,self).__init__(parent)

        self.button = QtGui.QPushButton("button", self)
        self.button.clicked.connect(self.open_new)

    def open_new(self):

        self.new = NewWidget()

if __name__ == "__main__":

    app = QtGui.QApplication(sys.argv)
    main = MainWidget()
    main.resize(200,50)
    main.move(app.desktop().screen().rect().center() - main.rect().center())
    main.show()
    sys.exit(app.exec_())
like image 284
user3046026 Avatar asked Mar 21 '23 16:03

user3046026


2 Answers

I think a better solution is to avoid creating a new window every time you click the button.

One way to do this would be to change the subwindow to a QDialog:

class NewWidget(QtGui.QDialog):
...

and move the resize/show lines into the open_new method:

class MainWidget(QtGui.QWidget):
    def __init__(self, parent=None):
        ...
        self._subwindow = None

    def open_new(self):
        if self._subwindow is None:
            self._subwindow = NewWidget(self)
            self._subwindow.resize(200, 50)
            # move it next to the main window
            pos = self.frameGeometry().topLeft()
            self._subwindow.move(pos.x() - 250, pos.y())
        self._subwindow.show()
        self._subwindow.activateWindow()

So now there is only ever one subwindow, which just gets re-activated whenever the button is clicked.

like image 135
ekhumoro Avatar answered Mar 31 '23 19:03

ekhumoro


Great. The final solution of my problem looks like this :

class MainWidget(QtGui.QWidget):
def __init__(self, parent=None):
    ...
    self._subwindow = QtGui.Qdialog()

def open_new(self):
    if self.subwindow.isVisible() is False:
        self._subwindow = NewWidget(self)
        self._subwindow.resize(200, 50)
        # move it next to the main window
        pos = self.frameGeometry().topLeft()
        self._subwindow.move(pos.x() - 250, pos.y())
    self._subwindow.show()
    self._subwindow.activateWindow()
like image 41
user3046026 Avatar answered Mar 31 '23 17:03

user3046026