目前我正在用QT编写日历程序。我的主窗口有一个QCalendarWidget
,现在我想听单元格的双击事件。我的问题是,我不知道我怎么能得到一个单元格(它是QCalendarWidget
的孩子),所以我可以添加一个事件监听器。随着:获取QCalendarWidget的单元格
calendarWidget.findChildren(QtCore.QObject)
我可以得到小工具的所有孩子,但我不知道如何识别细胞。你有什么想法,我可以做到这一点?
目前我正在用QT编写日历程序。我的主窗口有一个QCalendarWidget
,现在我想听单元格的双击事件。我的问题是,我不知道我怎么能得到一个单元格(它是QCalendarWidget
的孩子),所以我可以添加一个事件监听器。随着:获取QCalendarWidget的单元格
calendarWidget.findChildren(QtCore.QObject)
我可以得到小工具的所有孩子,但我不知道如何识别细胞。你有什么想法,我可以做到这一点?
日历小部件包含QTableView,因此您可以获取对其的引用并查询其内容。
下面的演示在桌面上安装event-filter以获得双击,因为表的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_())
您使用的是哪个版本的Qt? – Joseph
哦,对不起,忘了这个 - 我正在使用Qt 4.8 – Cilenco