PDA

View Full Version : Close window ( Doubt )



vitorlobo
9th February 2012, 02:23
Hi Guys!

I'm hours trying to solve this code .... how do I close the first by window the X title bar button...popup and close together?

Obs: (I'm using PyQt4)

http://s14.postimage.org/wf1m8alxb/close.png



#!/usr/bin/env python
#-*- coding: utf-8 -*-

import sys
from PyQt4.QtGui import *
from PyQt4.QtCore import *


class Mainw(object):
def __init__(self, MainWindow):
super(Mainw, self).__init__()

self.cw = QWidget(MainWindow)

self.btn1 = QPushButton("Popup", self.cw)
MainWindow.setCentralWidget(self.cw)

self.btn1.setGeometry(QRect(0, 0, 100, 30))


QObject.connect(self.btn1, SIGNAL("clicked()"), self.openpopup)

def openpopup(self):
self.pop = QWidget()
self.pop.setGeometry(QRect(100, 100, 400, 200))
self.pop.show()



if __name__ == "__main__":
app = QApplication(sys.argv)
MainWindow = QMainWindow()
main = Mainw(MainWindow)
MainWindow.show()
sys.exit(app.exec_())

ChrisW67
9th February 2012, 06:04
Reimplement the main window closeEvent() and close it explicitly.... at least you could if your code was taking a traditional approach to subclassing QMainWindow.

vitorlobo
9th February 2012, 12:57
Solved.

The function closeEvent() is inappropriate for this case.
I just created a kinship between the windows to make this possible. The closeEvent () now comes as a complement.



#!/usr/bin/env python
#-*- coding: utf-8 -*-

import sys
from PyQt4.QtGui import *
from PyQt4.QtCore import *


class popup(QMainWindow):
def __init__(self,parent=None): # Parentesco
super(popup,self).__init__(parent)


self.setWindowTitle("Janela Popup")


self.setMinimumSize(400 , 400)



class Mainw(QMainWindow):
def __init__(self, parent=None):
super(Mainw, self).__init__(parent)


self.setMinimumSize(400 , 400)


self.setWindowTitle("Janela Principal")

self.btn1 = QPushButton("Popup", self)

self.btn1.setGeometry(QRect(100, 50, 200, 200))

QObject.connect(self.btn1, SIGNAL("clicked()"), self.openpopup)

def openpopup(self):
pop = popup(self)
pop.show()



if __name__ == "__main__":
app = QApplication(sys.argv)
main = Mainw()
main.show()
sys.exit(app.exec_())