Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 60 additions & 0 deletions lib/include/Spix/Events/QtEventIdSniffer.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
/***
* Copyright (C) Falko Axmann. All rights reserved.
* Licensed under the MIT license.
* See LICENSE.txt file in the project root for full license information.
****/

#pragma once

#include <QObject>

#include <QEvent>
#include <QMouseEvent>
#include <QQmlContext>
#include <QQmlEngine>

#include <iostream>

namespace spix {

/**
* Object used for development to inspect object ids. It reacts on mouse events.
* The id path of the clicked item is written to stdout.
*
* Example Usage: app.installEventFilter(new spix::QtEventIdSniffer());
*/
class QtEventIdSniffer : public QObject {
public:
bool eventFilter(QObject* object, QEvent* event)
{
if (event->type() == QEvent::MouseButtonPress) {
auto tempObj = object;
std::string path = "";

while (tempObj != nullptr) {
// take object name if given
auto token = tempObj->objectName().toStdString();
if (token.empty()) {
QQmlContext* const context = qmlContext(tempObj);
if (context) {
// use object id
token = context->nameForObject(tempObj).toStdString();
}
}

// add id to front
if (!token.empty()) {
path = token + "/" + path;
}
tempObj = tempObj->parent();
}

std::cout << "path to clicked object: " << path << std::endl;
}

// always ignore event
return false;
}
};

} // namespace spix