我有点问题。当我按下QPushButton(孩子)或点击QDialog外面时,我正在尝试关闭QDialog。 我不希望在按钮的功能中关闭连接。当QDialog检测到一个孩子被点击时我想要关闭。
我的英语。
非常感谢。
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_())
此代码,在SimpleDialog中没有检测focusOut :(任何解决方案?
答案 0 :(得分:1)
我想向您展示我发现问题的解决方案 首先,如何关闭我的小部件SimpleDialogal点击它? 我在小部件的末尾做了一个小修改,这允许我扩展这个并管理它的整个区域。
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)
因此,通过这种方式,我可以编写mouseReleaseEvent
来检测顶部区域以及何时关闭点击。
def mouseReleaseEvent(self, QEvent):
if QApplication.widgetAt(QEvent.pos()) == self.fullWidget:
self.close()
现在,在按SimpleDialog
时关闭SimpleDialogItem
的问题的解决方案是关于将mouseReleaseEvent
方法写入QApplication,找到父级并关闭它
def mouseReleaseEvent(self, *args, **kwargs):
for widget in QApplication.topLevelWidgets():
if type(widget) == SimpleDialog:
widget.close()