目前我正在用QT编写一个日历程序。我的主窗口包含一个QCalendarWidget,现在我想听单元格的双击事件。我的问题是,我不知道如何才能获得一个单元格(这是QCalendarWidget的一个子单元),所以我可以向它添加一个事件侦听器。通过以下方式:
calendarWidget.findChildren(QtCore.QObject)我可以得到Widget的所有子类,但是我不知道如何识别一个单元。你知道我该怎么做吗?
发布于 2016-02-25 21:12:21
日历小部件包含一个QTableView,因此您可以获得对它的引用并查询其内容。
下面的演示在表上安装一个事件过滤器以获得双击,因为该表的doubleClicked信号被日历禁用(可能是为了防止对单元格的编辑)。
from PyQt4 import QtCore, QtGui
class Window(QtGui.QWidget):
def __init__(self):
super(Window, self).__init__()
self.calendar = QtGui.QCalendarWidget(self)
self.table = self.calendar.findChild(QtGui.QTableView)
self.table.viewport().installEventFilter(self)
layout = QtGui.QVBoxLayout(self)
layout.addWidget(self.calendar)
def eventFilter(self, source, event):
if (event.type() == QtCore.QEvent.MouseButtonDblClick and
source is self.table.viewport()):
index = self.table.indexAt(event.pos())
print('row: %s, column: %s, text: %s' % (
index.row(), index.column(), index.data()))
return super(Window, self).eventFilter(source, event)
if __name__ == '__main__':
import sys
app = QtGui.QApplication(sys.argv)
window = Window()
window.setGeometry(750, 250, 300, 300)
window.show()
sys.exit(app.exec_())https://stackoverflow.com/questions/35635826
复制相似问题