4

我连接到一个插槽的hovered信号,当鼠标悬停在栏集上时QBarSet,它将改变颜色,并在鼠标离开时重置颜色。 代码片段如下所示:QBarSet

void BarChart::hoverTest(bool status, int index)
{
    if(status == true) {
        set->setColor(Qt::red); //changes to bar set color to red mouse when hovers on bar set
    }
    else {
        set->setColor(QColor(52, 152, 219)); //reset the color when mouse leaves
    }
}

这些是悬停前和悬停时的照片: 在此处输入图像描述

在此处输入图像描述

如您所见,如果我将鼠标悬停在栏集上,所有这些栏集栏(元素)的颜色都变为红色。但是我想将鼠标悬停在栏集的特定栏(元素)上,并且该栏(元素)改变了它的颜色,其余的保持不变。

有没有办法做到这一点?

4

2 回答 2

3

我有点搜索并试图让它工作,它可以通过转换QGraphicsItemQGraphicsRectItem.

这与之前的答案类似:

QObject::connect(set0, &QBarSet::hovered, [&w](bool status, int /*index*/){
    QPoint p = w.mapFromGlobal(QCursor::pos());

    if(status){
        QGraphicsRectItem *rect = qgraphicsitem_cast<QGraphicsRectItem *>(w.itemAt(p));
        rect->brush().setColor(Qt::red);
        rect->update();     
    }
    else{
        rect->brush().setColor(Qt::blue);     //or change it to default colour
        rect->update();
    }
});

此外,可以使用 index ofQBarSet::hovered但需要大量工作并且无法直接执行。在我的例子中,我创建了在图表中查找所有条形图对象并按x位置对它们进行排序的方法,以便索引QObject::connect对应于排序列表。

因此,首先,我们需要找到图表中的所有条形图,QGraphicsRectItem并将它们放入并排序。

void sortGraphicItems( std::vector<std::pair<float,QGraphicsRectItem*> > &item_list){

    for(int i = 0; i<this->items().size();i++){
        if(w->items().at(i)->flags().testFlag(QGraphicsItem::ItemIsSelectable)){    //This selects all selectable items
            QGraphicsRectItem *it = qgraphicsitem_cast<QGraphicsRectItem *>(this->items().at(i));
            if (!it)    //if the graphic object is not type of QGraphicsRectItem
                continue;
            item_list.push_back( std::make_pair(it->rect().x(), it) );
        }
    }
    std::sort(item_list.begin(),item_list.end());
}

然后做同样的事情,但使用QBarset.

QObject::connect(set0, &QBarSet::hovered, [&w](bool status, int ind){
if(status){
    std::vector<std::pair<float,QGraphicsRectItem*> > item_list;
    sortGraphicItems(item_list);

    QGraphicsRectItem *rect = item_list.at(ind).second;

    //change colour of rect
}
else{
    //change rect colour back
}
于 2019-07-17T17:31:01.360 回答
2

目前无法单独更改列的颜色,因此我将展示一种解决方法。这包括在悬停的项目顶部放置一个新项目,如下所示:

#include <QApplication>
#include <QtCharts>
QT_CHARTS_USE_NAMESPACE

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    QChartView w;

    QBarSet *set0 = new QBarSet("bar1");

    *set0 << 1 << 4 << 3 << 7 << 2 << 5 << 1 << 3 << 3 << 2 << 1 << 6 << 7 << 5;

    QBarSeries *series = new QBarSeries;
    series->append(set0);

    QChart *chart= new QChart;
    w.setChart(chart);
    chart->addSeries(series);
    w.show();

    QGraphicsRectItem hoverItem;
    hoverItem.setBrush(QBrush(Qt::red));
    hoverItem.setPen(Qt::NoPen);

    QObject::connect(set0, &QBarSet::hovered, [&w, &hoverItem](bool status, int /*index*/){
        QPoint p = w.mapFromGlobal(QCursor::pos());
        if(status){
            QGraphicsItem *it = w.itemAt(p);
            hoverItem.setParentItem(it);
            hoverItem.setRect(it->boundingRect());
            hoverItem.show();
        }
        else{
            hoverItem.setParentItem(nullptr);
            hoverItem.hide();
        }
    });
    return a.exec();
}

在此处输入图像描述

于 2018-05-29T17:48:05.590 回答