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?
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();
}
Qt has QToolTip Class for show tooltips . but you can set it and use it before QFileDialog
opens your system file manager.
and Also QWidget has setToolTip function that you easily set a string as a tooltip for your widgets, pushbutton and other classes that inherit from QWidget.
For Example :
pushButton->setToolTip(QCoreApplication::translate("MainWindow", "<html><head/><body><p>Open File Dialog </p></body></html>", nullptr));
OR
pushButton->setToolTip("Open File Dialog");
I write those two examples just to show that you can even use HTML inside your tooltip .
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.