如何在 qt 中更改一个 QBarSet bar/element 颜色?

How to change one QBarSet bar/element color in qt?

我将 QBarSethovered 信号连接到一个插槽,当鼠标悬停在条形集上时,它将改变 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
    }
}

这些是悬停前和悬停时的照片:

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

有办法实现吗?

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

#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();
}

我进行了一些搜索并试图让它工作,可以通过将 QGraphicsItem 转换为 QGraphicsRectItem 来更改它。

与之前的回答类似:

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

此外,可以使用 QBarSet::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
}