PDA

View Full Version : QCursor with info box



madrich
7th September 2018, 10:04
Hi,

I'd like to attach a small text based info box to the mouse cursor, when I use a special tool in my application.
My first idea was to use QLabel without a parent and move the QLabels position to the cursors location.
To have a more general code, it would be easier to create a custom QCursor handling the text box and replace the QApplications QCursor. Unfortunately, there is a function setOverrideCursor for QGuiApplication only, that will be used already at different locations.
Is there any way how to set the standard cursors Instance?

Thanks

madrich
7th September 2018, 14:12
I think I found an elegant solution. The visibility depends on the parents widget - could be the main widget too.


QCursorLabel::QCursorLabel(QWidget *parent) : QLabel(parent)
{
setForegroundRole(QPalette::ToolTipText);
setBackgroundRole(QPalette::ToolTipBase);
setPalette(QToolTip::palette());
setWindowFlags(Qt::ToolTip);
setText("Set a QLabel Text");
setVisible(false);
parent->installEventFilter(this); // so we get the parents widget events
}


bool QCursorLabel::eventFilter(QObject *o, QEvent *e)
{
if (e->type() == QEvent::HoverMove)
{
QHoverEvent *me = dynamic_cast<QHoverEvent *>(e);
move(parentWidget()->mapToGlobal(me->pos() + QPoint(+10, +10)));
setVisible(!text().isEmpty());
}

if (e->type() == QEvent::Leave) setVisible(false);

return false;
}