QLabel 作为弹出窗口:单击外部时不会关闭

问题描述 投票:0回答:3

当在 QTableView 中单击单元格时,我使用 QLabel 作为弹出窗口来显示 HTML 信息。单击表时,将使用行名称和所需的弹出位置调用以下函数:

void DatabaseTableModel::showPopup (int rowIndex, const QPoint &location) const
{
    QLabel *popup = new QLabel(data_[rowIndex].displayHtml(), 0, Qt::Popup);
    popup->setTextFormat(Qt::RichText);
    popup->setOpenExternalLinks(true);
    popup->move(location);
    popup->show();
}

弹出窗口正确显示在正确的位置,并且 HTML 看起来不错。在装有 Qt 5.6 的 Mac 上,在弹出窗口外部单击时,弹出窗口可以正常关闭。

但是,在 Windows(使用 Qt 5.7)上,无论是在弹出窗口内部还是外部,单击时弹出窗口都不会关闭。有关修复的任何想法吗?

qt
3个回答
2
投票

我没有看到这个问题的任何其他答案,但我自己找到了答案: 似乎通用小部件弹出窗口已经过时(实际上),但在 QDialog 上使用它可以正常工作。这是修改后的代码:

void DatabaseTableModel::showPopup (int rowIndex, const QPoint &location) const {
    QDialog *popup = new QDialog(0, Qt::Popup | Qt::FramelessWindowHint);
    QVBoxLayout *layout = new QVBoxLayout;
    QLabel *popupLabel = new QLabel(data_.value(rowIndex).displayHtml(), 0);
    layout->addWidget(popupLabel);
    popupLabel->setTextFormat(Qt::RichText);
    popupLabel->setOpenExternalLinks(true);
    popup->setLayout(layout);
    popup->move(location);
    popup->exec();
}

0
投票

您应该使用QTooltip::showText。它支持HTML显示并且会自动关闭。工具提示旨在向用户显示不稳定的信息,

QLabel
不是。


0
投票

您可以将标签(或任何其他小部件)添加到简单的弹出小部件容器中。例如:

auto label = new QLabel(htmlText);
label->setTextFormat(Qt::RichText);
label->setOpenExternalLinks(true);
label->move(location);
 
auto popup = new QWidget;
popup->setWindowFlag(Qt::Popup);
auto layout = new QHBoxLayout(popup);
layout->addWidget(label);
layout->setMargin(0);
popup->show();
© www.soinside.com 2019 - 2024. All rights reserved.