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

女生的网名这么多〃 提交于 2019-12-10 18:50:01

问题


I connected to the hovered signal of QBarSet to a slot, which will change the QBarSet color when mouse hovers on the bar set and reset the color when mouse leaves.
The code snippet looks like this:

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
    }
}

And those are the pics before hovering and when hovers:

As you can see, if I hover on the bar set, all this bar set bars(elements) color changed to red. But I want to hover on a specific bar(element) of the bar set, and that bar(element) changes its color, and the rest of them stay the same.

Is there a way to achieve this?


回答1:


It is not currently possible to change the color of the column individually, so I will show a workaround. This consists of placing a new item on top of the hovered item as shown below:

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




回答2:


I was little bit searching and trying to make it work and it can be changed by casting QGraphicsItem to QGraphicsRectItem.

It is similar as in previous answer:

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

Additionally, it is possible use index of QBarSet::hovered but that take lot of work and it is not possible do it directly. In my case I created method to find all bar graphic objects in chart and sort them by x position so the indices in QObject::connect corresponds to sorted list.

So at first, we need to find all bars in a chart and cast the into QGraphicsRectItem and sort them.

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

and then just do the same but use index of 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
}


来源:https://stackoverflow.com/questions/50584771/how-to-change-one-qbarset-bar-element-color-in-qt

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!