2017-01-19 28 views
0

私は少し問題があります。 QPushButton(子)を押すか、QDialogの外側をクリックすると、QDialogを閉じようとしています。 私はボタンの機能で接続したいとは思わない。 QDialogが子供をクリックしたことを検出すると、私は閉じたい。QDialogをクリックして閉じるとサブウィジェットを閉じると閉じる方法を教えてください。

Sry my english。

ありがとうございます。

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

を閉じる押したときSimpleDialogを閉じるの問題に対する解決策として、私は、ユーザーQt.PopupどのようwindowFlatしてみてください窓に影を描く。 –

+1

コードの最も重要な部分だけを配置することができます。 – eyllanesc

+0

何がMainLayoutですか? – eyllanesc

答えて

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() 

は今SimpleDialogItemははQApplicationとにmouseReleaseEventメソッドを書くことについてである親を見つけて、それに

def mouseReleaseEvent(self, *args, **kwargs): 
    for widget in QApplication.topLevelWidgets(): 
     if type(widget) == SimpleDialog: 
      widget.close() 
関連する問題