Reputation: 210
I have a little problem. I'm trying close a QDialog when I press a QPushButton(child) or at click outside of the QDialog. I dont want close with connect in the function of the button. I want close when the QDialog detect a child clicked.
Sry my english.
thanks a lot.
Dialog.py
# coding=utf-8
from PyQt4.QtCore import Qt
from PyQt4.QtGui import *
class SimpleDialog(QDialog):
def __init__(self, title=None):
QDialog.__init__(self)
self.setWindowFlags(Qt.FramelessWindowHint | Qt.Tool)
self.setMinimumWidth(280)
self.__inWidget = []
self.__textColor = "#212121"
font = QFont()
font.setFamily("Roboto Medium")
font.setPixelSize(20)
self.__title = QLabel()
self.__title.setFont(font)
self.__title.setWordWrap(True)
self.__title.hide()
if title:
self.setTitle(title)
self.__vLayout = QVBoxLayout()
textLayout = QVBoxLayout()
textLayout.setAlignment(Qt.AlignTop)
textLayout.addWidget(self.__title)
contentLayout = QVBoxLayout()
contentLayout.addLayout(textLayout)
contentLayout.addLayout(self.__vLayout)
widget = QWidget()
widget.setLayout(contentLayout)
main = QVBoxLayout()
main.addWidget(widget)
backBoard = QHBoxLayout()
backBoard.addLayout(main)
self.setLayout(backBoard)
def focusOutEvent(self, *args, **kwargs):
self.close()
def addItem(self, simpleDialogItem):
""" SimpleDialog.addItem(SimpleDialogItem)
Agrega un item al SimpleDialog
"""
if type(simpleDialogItem) == SimpleDialogItem:
self.__vLayout.addWidget(simpleDialogItem)
self.__inWidget.append(simpleDialogItem)
else:
raise TypeError("Se espera un SimpleDialogItem y se recibio: " + type(simpleDialogItem))
def setTitle(self, title):
""" SimpleDialog.setTitle(str)
Establece el titulo del SimpleDialog
"""
self.__title.setText(title)
self.__title.show()
class SimpleDialogItem(QPushButton):
def __init__(self, icon=None, text=None):
""" SimpleDialogItem(icon=path, text=str)
Item utilizado en SimpleDialog
"""
QPushButton.__init__(self)
self.__text = QLabel()
self.__text.setWordWrap(True)
self.__layout = QHBoxLayout()
self.__layout.addWidget(self.__text)
self.setFixedHeight(48)
self.setLayout(self.__layout)
if text:
self.setText(text)
def setText(self, text):
""" SimpleDialogItem.setText(str)
Establece el texto del widget
"""
self.__text.setText(text)
test.py
import sys
from PyQt4.QtGui import QApplication
from PyQt4.QtGui import QHBoxLayout
from PyQt4.QtGui import QPushButton, QDialog
from Dialog import SimpleDialog, SimpleDialogItem
class Main(QDialog):
def __init__(self):
QDialog.__init__(self)
b = QPushButton("button")
b.released.connect(self.hola)
l = QHBoxLayout()
l.addWidget(b)
self.addView(l)
def hola(self):
t = "Three line wrapped text goes here making it wrap to next line and continues longer to be here "
tt = "You'll lose all photos and media."
ttt = "You'll lose all photos and media."
sd = SimpleDialog(title="Are you sure?")
self.item = SimpleDialogItem(text=t, icon="slide_1.jpg")
self.item.released.connect(self.chao)
item2 = SimpleDialogItem(text=tt)
item3 = SimpleDialogItem(text=ttt)
sd.addItem(self.item)
sd.addItem(item2)
sd.addItem(item3)
sd.exec_()
def chao(self):
print "Hola mundo!"
if __name__ == '__main__':
app = QApplication(sys.argv)
window = Main()
window.activateWindow()
window.show()
sys.exit(app.exec_())
This code, no detect focusOut in SimpleDialog :( Any solution?
Upvotes: 1
Views: 2209
Reputation: 210
I want to show you the solution that I found to my problem.
First, how do I close my widget SimpleDialogal click away from it?
I made a small modification at the end of the widget, which allows me to expand this one and manage its entire area.
main = QVBoxLayout()
main.addWidget(...contenido...)
self.fullWidget = QWidget()
self.fullWidget.setLayout(main)
self.fullWidget.setStyleSheet("background-color: RGB(0,0,0,30)")
l = QHBoxLayout()
l.addWidget(self.fullWidget)
self.showFullScreen()
self.setLayout(l)
So, this way I can write the mouseReleaseEvent
to detect the top area and when the click is closed.
def mouseReleaseEvent(self, QEvent):
if QApplication.widgetAt(QEvent.pos()) == self.fullWidget:
self.close()
Now as a solution to the problem of closing SimpleDialog
when pressing the SimpleDialogItem
is about writing the mouseReleaseEvent
method to with QApplication find the parent and close it
def mouseReleaseEvent(self, *args, **kwargs):
for widget in QApplication.topLevelWidgets():
if type(widget) == SimpleDialog:
widget.close()
Upvotes: 1