popup of QTableView doesn't show well

2019-08-06 00:36发布

问题:

I have a QTableView which show the content in popup when the mouse hover on it. In most cases it works well, but sometimes when I move the mouse, the popup become transparency. I have go through the code but still don't find out the reason. The correct case and incorrect case are shown below.

The code related to this popup is shown below:

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);d
}


void showPopup (const QModelIndex &index) const {
    if(index.column() == 1){
        QRect r = visualRect(index);
        popup->move(viewport()->mapToGlobal(r.bottomLeft()));

        QString showString = index.data(Qt::DisplayRole).toString();
        for (int i = 0; i<8; i++){
            showString = showString.replace(17+18*i,1,QChar(' '));
        }

        popupLabel->setText(showString);

        popup->adjustSize();
        popup->show();
    }
    else {
        popup->hide();
    }
}

A clue which may be useful is that when I change

        if(event->type() == QEvent::Leave){
            popup->hide();
        }

to

           Popup->hide();

The popup is always transparent. The full project is in git@github.com:bigbigda/DbgGUI.git

标签: qt qtableview