QT C++ - QCandlestickSet::hovered 无法正常工作

问题描述

我在自定义 QChartView 类中实现 QCandlestickSet::hovered 信号时遇到问题。这是我如何将信号和插槽以及插槽功能连接起来:


connect(this->m_series,SIGNAL(hovered(bool,QCandlestickSet *)),this,SLOT(sltTooltip(bool,QCandlestickSet *)));

void QCandlestickChartView::sltTooltip(bool status,QCandlestickSet *set)
{
    if (this->m_tooltip == nullptr) {
        this->m_tooltip = new QLabel(this);
        this->m_tooltip->setStyleSheet(
            "background: rgba(51,51,185);"
            "color: rgb(255,255,255);"
            "border:0px groove gray;border-radius:10px;padding:2px 4px;"
            "border:2px groove gray;border-radius:10px;padding:2px 4px;");
        this->m_tooltip->setVisible(false);
    }
    if (status) {
        QPoint cursor_g = QCursor::pos();
        QPoint cursor_w = QWidget::mapFromGlobal(QCursor::pos());
        QString date_time = QDateTime::fromSecsSinceEpoch(set->timestamp())
                                .toString("yyyy-MM-dd | hh:mm\n");
        QString open = "Open: " + QString::number(set->open()) + "\n";
        QString high = "High: " + QString::number(set->high()) + "\n";
        QString low = "Low: " + QString::number(set->low()) + "\n";
        QString close = "Close: " + QString::number(set->close());
        this->m_tooltip->setText(date_time + open + high + low + close);
        // this->m_tooltip->move(cursor_g.x(),cursor_g.y());
        this->m_tooltip->move(cursor_w.x(),cursor_w.y());
        this->m_tooltip->show();

        qDebug() << "global: " << cursor_g.x() << cursor_g.y();
        qDebug() << "global: " << cursor_w.x() << cursor_w.y();
    }
    else {
        this->m_tooltip->hide();
    }
}

将鼠标悬停在一个集合上时,工具提示应出现在光标正下方,其中包含该集合的数据。当我最大化 GUI 并将鼠标悬停在任何集合上时,我可以确认这有效,如图 here

当我使用 this->m_tooltip->move(cursor_w.x(),cursor_w.y()) 将工具提示设置为小部件坐标时,一些奇怪的行为开始发生。当一个集合悬停时,工具提示不再总是出现,只是随机出现。我可以在烛台图表上拖动光标,也许会弹出几个工具提示。通过 qDebug() 输出,我可以看到当我在集合内部移动光标时,信号被连续触发。如果工具提示碰巧出现,我可以在设置内正确移动它而不会重复信号。 当我在集合内部移动光标时,什么会导致悬停信号持续触发?

更新/附加信息/tldr...

我可以将标签的位置设置为特定值(例如 this->m_tooltip->move(400,400);),并且标签在 QChartView 小部件中正确显示/隐藏在 (400,400) 处。我还可以将标签的位置设置为全局坐标,即 this->m_tooltip->move(QCursor::pos()); 并且它在我的光标处正确显示/隐藏(当然只有在最大化时)。将位置设置为小部件坐标时开始出现问题,即 this->m_tooltip->move(this->mapFromGlobal(QCursor::pos())); 再次,工具提示随机触发(但是正确放置在光标处)。当工具提示未触发时,我可以在一组内移动鼠标,并看到每次移动都会触发悬停信号。

以下是我在标签设置为 specific coordinates 和设置为 widget coordinates 时看到的一些 gif

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)