#include #include #include #include #include class Watcher : public QObject { Q_OBJECT public: Watcher(QWidget *w, const QCursor &c1, const QCursor &c2, QTreeWidgetItem *item) : m_w(w) , m_cursor1(c1) , m_cursor2(c2) , m_item(item) { m_timer.setInterval(500); m_timer.start(); connect(&m_timer, SIGNAL(timeout()), this, SLOT(onTimeout())); } private Q_SLOTS: void onTimeout() { static int i = 0; QCursor cur(((i++) % 2) ? m_cursor1 : m_cursor2); m_w->setCursor(cur); const QPoint pos = QCursor::pos(); QWidget *w = QApplication::widgetAt(pos); const QPoint loc = w ? w->mapFromGlobal(pos) : QPoint(); m_item->setText(0, QString("Widget at global pos %1/%2 is %3 at local pos %4/%5").arg(pos.x()).arg(pos.y()).arg(w ? w->objectName() : QString()).arg(loc.x()).arg(loc.y())); } private: QTimer m_timer; QWidget *m_w; QCursor m_cursor1; QCursor m_cursor2; QTreeWidgetItem *m_item; }; int main(int argc, char *argv[]) { QApplication a(argc, argv); QPixmap pm(1,1); pm.fill(Qt::red); QCursor redCursor(pm, 0, 0); pm.fill(Qt::blue); QCursor blueCursor(pm, 0, 0); QTreeWidget *treeWidget = new QTreeWidget(); treeWidget->move(100,100); treeWidget->show(); treeWidget->header()->hide(); treeWidget->setCursor(redCursor); treeWidget->resize(400,200); QTreeWidgetItem * top = new QTreeWidgetItem(QStringList() << "TopLevel"); treeWidget->addTopLevelItem(top); Watcher watcher(treeWidget->viewport(), QCursor(Qt::CrossCursor), blueCursor, top); return a.exec(); } #include "main.moc"