QFileDialog中的工具提示(或其他操作)



悬停在QFileDialog::getOpenFileName实例中的文件上时,我希望弹出一个工具提示(或者理想情况下是QWidget(
有没有一种方法可以做到这一点而不需要对类进行子类化?

我不确定是否有任何批准/明确的方法可以做到这一点。以下是实现(我认为(您想要的东西的一种相当巧妙的方法,但它对与QFileDialog实例相关的小部件层次结构进行了某些假设。具体来说,它依赖于这样一个假设,即QFileDialog实例所包含的小部件层次结构将包含一个或多个QAbstractItemView实例。。。

#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();
}

最新更新