Results 1 to 4 of 4

Thread: change QwtPlotCurve title by clicking on the legend

  1. #1
    Join Date
    Apr 2016
    Posts
    15
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default change QwtPlotCurve title by clicking on the legend

    Hello. I would like to be able to change a QwtPlotCurve's title and color through the legend. I'm open to suggestions as to how to implement this. I'm thinking I will add code to the mousePressEvent() function in qwt_legend_label.cpp that will check if it was the RightButton that was selected. If so, popup a menu which would have various colors as menu items, as well as a menu item that says "change title". If the "change title" menu item is selected, I would somehow show a text editor that would allow the user to enter a new title. Then, somehow, I would have to get the new title back to the curve. This seems a bit clumsy to me. Does anyone have a better idea? Thank you.

  2. #2
    Join Date
    Apr 2016
    Posts
    15
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: change QwtPlotCurve title by clicking on the legend

    I solved my own problem. Each time I add a curve to the plot, I create a "LegendPicker" that is associated with that curve's QwtLegendLabel. I then capture mouse events on this label. When I right click, on the legend label, I popup a context menu.

  3. #3
    Join Date
    Jul 2015
    Posts
    87
    Thanks
    1
    Thanked 4 Times in 4 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: change QwtPlotCurve title by clicking on the legend

    It would be nice if you post your solution that other can provide also from it.

  4. #4
    Join Date
    Apr 2016
    Posts
    15
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: change QwtPlotCurve title by clicking on the legend

    Here is my solution:

    Here is LegendPicker.h:

    class LegendPicker : public QObject
    {
    Q_OBJECT
    public:
    explicit LegendPicker(QwtPlot*, QwtPlotCurve*, QwtLegendLabel*);
    ~LegendPicker();

    private:
    QwtPlotCurve *m_pCurve;
    QwtLegendLabel *m_pLegendLabel;
    QwtPlot* plot();
    bool eventFilter(QObject*, QEvent*);
    };

    and here is LegendPicker.cpp:

    LegendPicker::LegendPicker(QwtPlot *plot, QwtPlotCurve *curve, QwtLegendLabel *label) : QObject(plot)
    {
    m_pLegendLabel = label;
    m_pCurve = curve;
    if (m_pLegendLabel)
    {
    m_pLegendLabel->installEventFilter(this);
    }
    }

    LegendPicker::~LegendPicker()
    {
    if (m_pLegendLabel)
    delete m_pLegendLabel;
    if (m_pCurve)
    delete m_pCurve;
    }

    QwtPlot* LegendPicker:lot()
    {
    return qobject_cast<QwtPlot *>(parent());
    }

    bool LegendPicker::eventFilter(QObject *p_object, QEvent *p_event)
    {
    if (p_object == (QObject*) m_pLegendLabel)
    {
    switch (p_event->type())
    {
    case QEvent::ContextMenu:
    {
    const QMouseEvent *p_mouse_event = static_cast<QMouseEvent *>(p_event);
    plot()->ShowContextMenu(m_pCurve, p_mouse_event->pos());
    break;
    }
    default:
    break;
    }
    }
    return QObject::eventFilter(p_object, p_event);
    }

    Then, in my QwtPlot, whenever I add a curve I do this:

    QwtPlotCurve *p_curve = new QwtPlotCurve(curve_title);
    p_curve->attach(this);
    QwtLegend *p_plot_legend = qobject_cast<QwtLegend*>(legend());
    QVariant variant = itemToInfo(p_curve);
    QWidget *p_legend_widget = p_plot_legend->legendWidget(variant);
    QwtLegendLabel *p_legend_label = qobject_cast<QwtLegendLabel *>(p_legend_widget);
    (void) new LegendPicker(this, p_curve, p_legend_label);

  5. The following user says thank you to sunburns for this useful post:

    embeddedmz (20th June 2019)

Similar Threads

  1. Replies: 6
    Last Post: 13th November 2015, 10:43
  2. Replies: 2
    Last Post: 12th May 2015, 10:19
  3. Replies: 2
    Last Post: 26th October 2012, 16:26
  4. Replies: 8
    Last Post: 18th October 2012, 07:23
  5. Replies: 1
    Last Post: 2nd February 2011, 12:06

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.