[英]Tooltips (or other action) in QFileDialog
我想在將QFileDialog::getOpenFileName
懸停在QFileDialog::getOpenFileName
實例中的文件上時彈出工具提示(或理想情況下,QWidget)。
有沒有辦法在不繼承類的情況下做到這一點?
我不確定是否有任何批准/確定的方法可以做到這一點。 以下是實現(我認為)您想要的東西的一種相當hacky 的方式,但是它對與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();
}
Qt 有用於顯示工具提示的QToolTip類。 但是你可以在QFileDialog
打開你的系統文件管理器之前設置它並使用它。
而且 QWidget 具有setToolTip功能,您可以輕松地將字符串設置為小部件、按鈕和其他繼承自 QWidget 的類的工具提示。
例如 :
pushButton->setToolTip(QCoreApplication::translate("MainWindow", "<html><head/><body><p>Open File Dialog </p></body></html>", nullptr));
或者
pushButton->setToolTip("Open File Dialog");
我編寫這兩個示例只是為了表明您甚至可以在工具提示中使用HTML 。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.