Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

How to draw draggable custom legend enclosing all the legends items in qt charts



  • Hi,
    I wanted to know as to how i can have the legends of a chart draggable and movable to custom position. I do know that there is an example for making legends position custom. The example name is - "Legend Example". As soon as i detach the legend here, the transparent rectangle is enclosing a larger space than that is needed. how do i enclose the rectangle here only over the legends displayed? Thanks for any help in advance.
    8c004039-cb67-4806-bcae-53cdc70306ac-image.png
    Attached is the code snippet in the qt legend example pertinent to making the legend position custom

            legend->detachFromChart();
            m_chart->legend()->setBackgroundVisible(true);
            m_chart->legend()->setBrush(QBrush(QColor(50, 50, 50, 10)));
            m_chart->legend()->setPen(QPen(QColor(192, 192, 192, 192)));
            //![2]
            showLegendSpinbox();
            m_chart->legend()->setGeometry(QRectF(legend->geometry().x(),
                                                  legend->geometry().y(),
                                                  legend->geometry().width(),
                                                  legend->geometry().height()));
            QGraphicsScene *scene=m_chartView->scene();
            RectItem *item=new RectItem(legend,m_chartView);
            item->setRect(legend->geometry());
            item->setFlags(QGraphicsItem::ItemIsSelectable | QGraphicsItem::ItemIsMovable | 
           QGraphicsItem::ItemIsFocusable | QGraphicsItem::ItemIsFocusScope);
    
            scene->addItem(item);
            m_chart->legend()->update();```

Log in to reply