Results 1 to 12 of 12

Thread: set visibility of legend with its corresponding Curve (multiple Y-axes)

  1. #1
    Join Date
    Aug 2013
    Posts
    41
    Thanks
    4
    Qt products
    Qt5
    Platforms
    Windows

    Default set visibility of legend with its corresponding Curve (multiple Y-axes)

    I have the following lines:

    Qt Code:
    1. QVector<QwtPlotCurve*> pMyPlotCurve;
    2. pMyPlotCurve[channelwidget]->setVisible(true); //or false depending on the 'checkbox' toogling of each channel.
    To copy to clipboard, switch view to plain text mode 

    In total, I have 3 channelwidgets (each having a ComboBox with two items in it) and a color associated with each channelwidget (red, blue, black)

    Now I would also like to show or hide the legend of the correspodning curve when I toggle the checkbox 'on' or 'off' along with the visibility of the curve (as mentioned above).
    In other words, when i set the curve as visible, its corresponding legend should also be visible and vise versa.

    Also, I have two Y-axes (left and right). So, I would like to correspond the legends according to the Y-axis the selected curve uses.

    For example,
    In the combobox if I choose 'itemleft', then its legend should appear on the left of leftYaxis and when I choose 'itemright' from the ComboBox, then its legend show appear on the right of rightYaxis.

    Please help.
    Thanks! :-)

  2. #2
    Join Date
    Feb 2006
    Location
    Munich, Germany
    Posts
    3,234
    Thanked 857 Times in 806 Posts
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: set visibility of legend with its corresponding Curve (multiple Y-axes)

    In other words, when i set the curve as visible, its corresponding legend should also be visible and vise versa.
    Qt Code:
    1. pMyPlotCurve[channelwidget]->setItemAttribute(QwtPlotItem::Legend, ...);
    To copy to clipboard, switch view to plain text mode 
    So, I would like to correspond the legends according to the Y-axis the selected curve uses.
    For the terminology: the legend is a widget, where you have legend items. Several ( usually only one ) legend items correspond to a plot item.

    The layout system of QwtPlot is not able to handle more than one legend, so if you need to have more than one legend you have to implement the layout code yourself. On screen this can be done using the standard Qt layouts easily. But QwtPlotRenderer is not aware of elements that are not part of the QwtPlot layout and you will have to write some extra layout code there calling QwtAbstractLegend::renderLegend() manually - if you want to have PDF export or printing.

    For connecting your legends to the plot see: http://qwt.sourceforge.net/class_qwt...14325c50a1c22b

    Uwe

  3. #3
    Join Date
    Aug 2013
    Posts
    41
    Thanks
    4
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: set visibility of legend with its corresponding Curve (multiple Y-axes)

    Thanks for ur response @Uwe.

    I have tried before the following line as u suggested:

    Qt Code:
    1. pMyPlotCurve[channelwidget]->setItemAttribute(QwtPlotItem::Legend, ...);
    To copy to clipboard, switch view to plain text mode 

    But I am not able to control the visibility of legend items as intended.

    So far I have written the following lines in a seperate class:
    Qt Code:
    1. T_INT k;
    2.  
    3. mPlotCanvas = new QwtPlotCanvas;
    4. mPlotCanvas->setPaintAttribute(QwtPlotCanvas::BackingStore, false);
    5.  
    6. /*-------legend------*/
    7.  
    8. QwtLegend *legend = new QwtLegend;
    9. legend->setFrameStyle(QFrame::Box|QFrame::Sunken);
    10. insertLegend(legend, QwtPlot::TopLegend);
    11.  
    12. /*-------grid------*/
    13.  
    14. QwtPlotGrid *grid = new QwtPlotGrid;
    15. grid->enableXMin(true);
    16. grid->setMajorPen(QPen(Qt::gray, 0, Qt::DotLine));
    17. grid->setMinorPen(QPen(Qt::gray, 0, Qt::DotLine));
    18. grid->attach(this);
    19.  
    20. /*-------curve labels------*/
    21.  
    22. QVector<QString> vTextLabels;
    23.  
    24. for (k=0; k< 2; k++)
    25. {
    26. vTextLabels.append(QString("Signal %1").arg(k));
    27.  
    28. }
    29. QVector <QwtPlotCurve*> pMyPlotCurve;
    30.  
    31. for (k=0; k< 2; k++)
    32. {
    33. if (k < vTextLabels.count())
    34. {
    35. pPlotCurveTime.append(new QwtPlotCurve(vTextLabels[k]));
    36. }
    37.  
    38. pMyPlotCurve[k]->attach(this);
    39. }
    40.  
    41. /*-------curve styles------*/
    42.  
    43. QVector<QPen> vColor;
    44. vColor.append(QPen(Qt::red));
    45. vColor.append(QPen(Qt::blue));
    46.  
    47.  
    48. for (k=0; k< 2; k++)
    49. {
    50. pMyPlotCurve[k]->setPen(vColor[k%vColor.count()]);
    51. }
    To copy to clipboard, switch view to plain text mode 

    Now, as mentioned in my previous post, I call the plotting curve vector 'pMyPlotCurve' in another class in the slot function of checkbox as:

    Qt Code:
    1. QVector<QwtPlotCurve*> pMyPlotCurve;
    2. pMyPlotCurve[channelwidget]->setVisible(true); //or false depending on the 'checkbox' toogling of each channel.
    To copy to clipboard, switch view to plain text mode 

    Currently I have the Im_1.jpg
    I would like to achieve Im_2.jpg. The curves which I select to be visible should have their legend items displayed either on left or right.

    The layout system of QwtPlot is not able to handle more than one legend, so if you need to have more than one legend you have to implement the layout code yourself. On screen this can be done using the standard Qt layouts easily.
    Can u please show me an example, where multiple legend widgets can be displayed using Qt layouts? When I add another legend widget by:

    Qt Code:
    1. QwtLegend *legend1 = new QwtLegend;
    2. legend1->setFrameStyle(QFrame::Box|QFrame::Sunken);
    3. insertLegend(legend1, QwtPlot::TopLegend);
    4.  
    5. QwtLegend *legend2 = new QwtLegend;
    6. legend2->setFrameStyle(QFrame::Box|QFrame::Sunken);
    7. insertLegend(legend1, QwtPlot::RightLegend);
    To copy to clipboard, switch view to plain text mode 
    only the last declared widget legend2 is shown in the Qwt plot.

    Please advice.
    Attached Images Attached Images
    Last edited by uz_qt; 8th September 2013 at 13:14.

  4. #4
    Join Date
    Feb 2006
    Location
    Munich, Germany
    Posts
    3,234
    Thanked 857 Times in 806 Posts
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: set visibility of legend with its corresponding Curve (multiple Y-axes)

    But I am not able to control the visibility of legend items as intended.
    All you need to do is to change the QwtPlotItem::Legend attribute together with the visibility of the item. There is no chance to fail.

    Can u please show me an example, where multiple legend widgets can be displayed using Qt layouts?
    No, I neverdid it myself and never heard from someone else doing it.

    When I add another legend widget by:
    You don't have to insert a legend at all ( not even the first one ). Instead you implement your own layout ( f.e QHBoxLayout with legend1 + plot + legend2 ), connecting the legends manually to the signals I have given you in my previous post.

    cu,
    Uwe

  5. #5
    Join Date
    Aug 2013
    Posts
    41
    Thanks
    4
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: set visibility of legend with its corresponding Curve (multiple Y-axes)

    Hi Uwe,

    I have tried it again but the legend items just dont show up on the layout! Only the curves are toggled visible/invisible.
    I think I am missing something here! :-(

    So far from what I have understood, I first have to connect the curves to the legend-items (in my case, two curves and two legend items) using legendDataChanged().

    Then as next step, I use:
    [CODE] pMyPlotCurve[channelwidget]->setItemAttribute(QwtPlotItem::Legend, true); //or false [\CODE]
    to toggle them on or off.

    But how do I realize the part of connecting the curves to legends? I am unable to understand the syntax of legendDataChanged() from sourceforge. What should be the two input arguments in my case? An example of how it is actually used would greatly help!

  6. #6
    Join Date
    Feb 2006
    Location
    Munich, Germany
    Posts
    3,234
    Thanked 857 Times in 806 Posts
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: set visibility of legend with its corresponding Curve (multiple Y-axes)

    Quote Originally Posted by uz_qt View Post
    So far from what I have understood, I first have to connect the curves to the legend-items...
    Qt Code:
    1. class Box: public QWidget
    2. {
    3. Q_OBJECT
    4.  
    5. public:
    6. Box( QWidget *parent ):
    7. QWidget( parent )
    8. {
    9. mLegendLeft= new QwtLegend();
    10. mLegendLeft->setMaxColums(1 );
    11. mLegendLeft->setSizePolicy( ... );
    12.  
    13. mLegendRight= new QwtLegend();
    14. mLegendRight->setMaxColums(1 );
    15. mLegendRight->setSizePolicy( ... );
    16.  
    17. mPlot = new QwtPlot();
    18.  
    19. QHBoxLayout layout = new QHBoxLayout( this );
    20. layout->addWidget( mLegendLeft );
    21. layout->addWidget( mPlot );
    22. layout->addWidget( mLegendRight );
    23.  
    24. connect( mPlot, SIGNAL( legendDataChanged( const QVariant &, const QList<QwtLegendData> & ) ),
    25. this, SLOT( updateLegend( const QVariant &, const QList<QwtLegendData> & ) )
    26. );
    27. }
    28.  
    29. private slots:
    30. void updateLegend( const QVariant &itemInfo,
    31. const QList<QwtLegendData> &data )
    32. {
    33. const QwtPlotItem* plotItem = mPlot->infoToItem( itemInfo );
    34. if ( plotItem->yAxis() == QwtPlot::yRight )
    35. mLegendRight->updateLegend( itemInfo, data );
    36. else
    37. mLegendLeft->updateLegend( itemInfo, data );
    38. }
    39. };
    To copy to clipboard, switch view to plain text mode 

    Uwe

  7. #7
    Join Date
    Aug 2013
    Posts
    41
    Thanks
    4
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: set visibility of legend with its corresponding Curve (multiple Y-axes)

    @Uwe: Thanks a lot for your example. It was really helpful. :-)

    Regarding toggling the legend visible/invisible, I have implemented it as you suggested.
    However, when I toggle it ON, the legend item appears. Fine. But then onwards, it does not toggle and always remains visible independent of me toggling it ON or OFF.
    I have set a break point in the SLOT function n tested it. The flag is rightly being set false, but it does not effect the visibility of the legend (it always stays visible).

    Following is my slot function:

    private slots:
    void VisibleSignal(bool bFlag)
    {
    mpPlotCurveTime[mChannelNumber]->setVisible(bFlag);
    pMyPlotCurve[channelwidget]->setItemAttribute(QwtPlotItem::Legend, bFlag);
    }

    Note: I want the legend item to work in tandom with the signal visibility.

  8. #8
    Join Date
    Feb 2006
    Location
    Munich, Germany
    Posts
    3,234
    Thanked 857 Times in 806 Posts
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: set visibility of legend with its corresponding Curve (multiple Y-axes)

    Like always you need a replot to make things happen ( or QwtPlot::updateLegend() - not sure myself ).

    Uwe

  9. #9
    Join Date
    Aug 2013
    Posts
    41
    Thanks
    4
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: set visibility of legend with its corresponding Curve (multiple Y-axes)

    Usually, how can one hide or toggle an existing legendItem?
    The function calls the updateLegend() slot function only when:

    Qt Code:
    1. 1.pMyPlotCurve[channelwidget]->setItemAttribute(QwtPlotItem::Legend, true);
    To copy to clipboard, switch view to plain text mode 

    When false, it does not call the updateLegend(). I want to hide the updated legend item when I toggle the checkbox off.

    Does it only work in 1-way?

  10. #10
    Join Date
    Feb 2006
    Location
    Munich, Germany
    Posts
    3,234
    Thanked 857 Times in 806 Posts
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: set visibility of legend with its corresponding Curve (multiple Y-axes)

    Qt Code:
    1. void Plot::toggleCurveVisibility()
    2. {
    3. const bool on = !m_curve1->isVisible();
    4.  
    5. m_curve->setVisible( on );
    6. m_curve1->setItemAttribute( QwtPlotItem::Legend, on );
    7.  
    8. updateLegend();
    9. }
    To copy to clipboard, switch view to plain text mode 

    HTH,
    Uwe

  11. #11
    Join Date
    Aug 2013
    Posts
    41
    Thanks
    4
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: set visibility of legend with its corresponding Curve (multiple Y-axes)

    @Uwe,

    Thanks for ur response. It still does not work in connection with curve visibiltiy.

    You suggesion makes it work in reverse order, that is, if toggled off, then legend appears ( and remains visible there after).
    Last edited by uz_qt; 10th September 2013 at 16:48.

  12. #12
    Join Date
    Feb 2006
    Location
    Munich, Germany
    Posts
    3,234
    Thanked 857 Times in 806 Posts
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: set visibility of legend with its corresponding Curve (multiple Y-axes)

    Quote Originally Posted by uz_qt View Post
    You suggesion makes it work in reverse order, that is, if toggled off, then legend appears ( and remains visible there after).
    No it works in the right order - but this doesn't matter as long as you got the idea.

    Uwe

Similar Threads

  1. Replies: 2
    Last Post: 23rd June 2012, 18:12
  2. PyQwt - Toggling Legend Visibility
    By ftnirp in forum Qwt
    Replies: 1
    Last Post: 25th May 2012, 21:13
  3. connect 1 legend item and 2 curve
    By ruzik in forum Qwt
    Replies: 1
    Last Post: 13th September 2011, 11:21
  4. Replies: 5
    Last Post: 7th February 2011, 08:38
  5. Drawing curve legend
    By lni in forum Qt Programming
    Replies: 1
    Last Post: 16th February 2010, 03:42

Tags for this Thread

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.