鼠标悬停时QTableView显示表项的内容

时间:2017-09-26 16:23:31

标签: c++ qt qt5 qtableview

用手机写字,所以格式可能不好。

我有一张QTableView的表格,表格中有两列。第二列包含一个非常长的字符串,如果没有调整大小,则无法完全显示。当我将鼠标悬停在一个项目上时,我想在矩形中显示字符串,并且矩形靠近鼠标(许多软件如Eclipse和VS都有这样的功能)。

我已经在互联网上搜索了一段时间,但仍然不知道如何编写此视图功能。

enter image description here enter image description here

2 个答案:

答案 0 :(得分:2)

首先实现当鼠标进入表中项目区域时需要知道的弹出窗口,为此我们将使用eventFilter()方法并查找QEvent::MouseMove事件的时间使用,我们将通过函数indexAt()和鼠标的位置获取索引,并比较它是否与前一个索引不同。如果它发生,它将根据需要显示或隐藏弹出窗口。

要创建PopUp,我们使用对话框并插入QLabel,并使用setWordWrap属性正确拟合文本

#ifndef TABLEVIEW_H
#define TABLEVIEW_H

#include <QDialog>
#include <QEvent>
#include <QLabel>
#include <QMouseEvent>
#include <QTableView>
#include <QVBoxLayout>
#include <QHeaderView>

class TableView: public QTableView{
    Q_OBJECT
    QDialog *popup;
    QLabel *popupLabel;

public:
    TableView(QWidget *parent = Q_NULLPTR):QTableView(parent){
        viewport()->installEventFilter(this);
        setMouseTracking(true);
        popup = new QDialog(this, Qt::Popup | Qt::ToolTip);

        QVBoxLayout *layout = new QVBoxLayout;
        popupLabel = new QLabel(popup);
        popupLabel->setWordWrap(true);
        layout->addWidget(popupLabel);
        popupLabel->setTextFormat(Qt::RichText);
        //popupLabel->setOpenExternalLinks(true);
        popup->setLayout(layout);
        popup->installEventFilter(this);
    }

    bool eventFilter(QObject *watched, QEvent *event){
        if(viewport() == watched){
            if(event->type() == QEvent::MouseMove){
                QMouseEvent *mouseEvent = static_cast<QMouseEvent*>(event);
                QModelIndex index = indexAt(mouseEvent->pos());
                if(index.isValid()){
                    showPopup(index);
                }
                else{
                    popup->hide();
                }
            }
            else if(event->type() == QEvent::Leave){
                popup->hide();
            }
        }
        else if(popup == watched){
            if(event->type() == QEvent::Leave){
                popup->hide();
            }
        }
        return QTableView::eventFilter(watched, event);
    }

private:
    void showPopup (const QModelIndex &index) const {
        if(index.column() == 1){
            QRect r = visualRect(index);
            popup->move(viewport()->mapToGlobal(r.bottomLeft()));
            popup->setFixedSize(100, popup->heightForWidth(100));
            popupLabel->setText(index.data(Qt::DisplayRole).toString());
            popup->adjustSize();
            popup->show();
        }
        else {
            popup->hide();
        }
    }
};

#endif // TABLEVIEW_H

<强>截图:

enter image description here

在以下链接中,您会找到example

答案 1 :(得分:0)

该死。每个人都必须尝试以艰苦的方式做到这一点,然后开始将其分类,重新发明不需要它的东西。答案已经在Displaying tooltips in PyQT for a QTreeView item

答案是,在yer模型的data()函数中,仅当调用'Qt :: ToolTipRole;'时返回有用的东西。首先检查“ index.column()”以确保它是正确的列。

这里有些类似的愚蠢:

Show tooltips for long entries of your custom model

我将给他们带来疑问的好处,并假设Qt曾经以这种方式工作;但是现在不行了。他们让人们制作了这个自定义事件过滤器;而我为我的模型中给定列返回了ToolTipRole的工具提示,并且该工具提示仅针对返回值的列中的单元格显示。这根本是不必要的工作。现在不需要了;浪费时间该Wiki条目很有可能,只是过时了。