Reputation: 204
I want to have a Tooltip (or ideally, a QWidget) pop-up when hovering over a file in a QFileDialog::getOpenFileName
instance.
Is there a way to do this without subclassing the class?
Upvotes: 0
Views: 221
Reputation: 12899
I'm not sure there's any approved/definitive way of doing this. The following is a rather hacky way of achieving what (I think) you want but which makes certain assumptions regarding the widget hierarchy associated with a QFileDialog
instance. Specifically, it relies on the assumption that the widget hierarchy subtended by a QFileDialog
instance will contain one or more QAbstractItemView
instances...
#include <iostream>
#include <QAbstractItemView>
#include <QApplication>
#include <QCursor>
#include <QFileDialog>
#include <QToolTip>
int
main (int argc, char **argv)
{
QApplication app(argc, argv);
QFileDialog fd;
/*
* Further to the comments by @Parisa.H.R, we need to make sure we use a
* non-native file dialog here otherwise there's no way to get the desired
* behaviour.
*/
fd.setOption(QFileDialog::DontUseNativeDialog);
/*
* Search the widget hierarchy under the QFileDialog looking for instances of
* QAbstractItemView or derived classes.
*/
for (auto *v: fd.findChildren<QAbstractItemView *>()) {
std::clog << "view = " << v << "(type=" << v->metaObject()->className()
<< ", name=\"" << v->objectName().toStdString() << "\")\n";
/*
* Connect the view's entered signal to a lambda which, for the time being,
* simply displays a tooltip showing the name of the filesystem item.
*/
QObject::connect(v, &QAbstractItemView::entered,
[](const QModelIndex &index)
{
QToolTip::showText(QCursor::pos(), index.data(Qt::DisplayRole).toString());
});
/*
* In order to receive the QAbstractItemView::entered signal mouse tracking
* must be enabled for the view.
*/
v->setMouseTracking(true);
}
fd.exec();
}
Upvotes: 3