QCalendarWidget弹出窗口关闭时如何执行操作?

时间:2016-09-28 11:51:28

标签: python qt qt4 pyqt4

我正在使用QDateEdit小部件QDateEdit.setCalendarPopup(True)。我正在尝试在日历弹出窗口关闭时将插槽连接到事件。请参阅下面的示例,了解到目前为止我在MyCalendarWidget中找到的尝试。到目前为止,我的尝试都没有奏效。每当日历小部件弹出窗口关闭时,我该怎么做才能执行操作,只有在日期更改时

from PyQt4 import QtGui, QtCore
import sys

class MainWindow(QtGui.QMainWindow):
    def __init__(self, *args):
        super(MainWindow,self).__init__(*args)
        self._date = QtGui.QDateEdit()
        self._date.setCalendarPopup(True)
        self._date.setCalendarWidget(MyCalendarWidget())
        self.setCentralWidget(self._date)

class App(QtGui.QApplication):
    def __init__(self, *args):
        super(App,self).__init__(*args)
        self.main = MainWindow()
        self.connect(self, QtCore.SIGNAL("lastWindowClosed()"), self.byebye )
        self.main.show()

    def byebye( self ):
        self.exit(0)

class MyCalendarWidget(QtGui.QCalendarWidget):
    def __init__(self, parent=None):
        print("mycal initialized")
        super(MyCalendarWidget, self).__init__(parent)

        self.installEventFilter(self)
        self._many = 2
        self._many2 = 2

    def focusInEvent(self, event):
        print('-'*self._many + 'focus in')
        if self._many == 2:
            self._many = 4
        else:
            self._many = 2
        super(MyCalendarWidget, self).focusInEvent(event)

    def focusOutEvent(self, event):
        print('-'*self._many2+'focus out')
        if self._many2 == 2:
            self._many2 = 4
        else:
            self._many2 = 2
        super(MyCalendarWidget, self).focusOutEvent(event)

    def closeEvent(self, event):
        print('close')
        super(MyCalendarWidget, self).closeEvent(event)

    def mouseReleaseEvent(self, event):
        print('mouse')
        super(MyCalendarWidget, self).mouseReleaseEvent(event)

def main(args):
    global app
    app = App(args)
    app.exec_()

if __name__ == "__main__":
    main(sys.argv)

1 个答案:

答案 0 :(得分:0)

想出来 - 结果我需要使用@Override public void onActivityCreated(Bundle savedInstanceState) { super.onActivityCreated(savedInstanceState); final InputMethodManager imm = (InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE); imm.hideSoftInputFromWindow(locationEt.getWindowToken(), 0); } 中的clicked信号。这也消除了对子类QCalendarWidget的需要。

QCalendarWidget