set visibility of legend with its corresponding Curve (multiple Y-axes)
I have the following lines:
Code:
QVector<QwtPlotCurve*> pMyPlotCurve;
pMyPlotCurve[channelwidget]->setVisible(true); //or false depending on the 'checkbox' toogling of each channel.
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! :-)
Re: set visibility of legend with its corresponding Curve (multiple Y-axes)
Quote:
In other words, when i set the curve as visible, its corresponding legend should also be visible and vise versa.
Code:
pMyPlotCurve
[channelwidget
]->setItemAttribute
(QwtPlotItem::Legend, ...
);
Quote:
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
2 Attachment(s)
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:
Code:
pMyPlotCurve
[channelwidget
]->setItemAttribute
(QwtPlotItem::Legend, ...
);
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:
Code:
T_INT k;
mPlotCanvas
->setPaintAttribute
(QwtPlotCanvas::BackingStore,
false);
/*-------legend------*/
legend
->setFrameStyle
(QFrame::Box|QFrame
::Sunken);
insertLegend
(legend,
QwtPlot::TopLegend);
/*-------grid------*/
grid->enableXMin(true);
grid
->setMajorPen
(QPen(Qt
::gray,
0, Qt
::DotLine));
grid
->setMinorPen
(QPen(Qt
::gray,
0, Qt
::DotLine));
grid->attach(this);
/*-------curve labels------*/
QVector<QString> vTextLabels;
for (k=0; k< 2; k++)
{
vTextLabels.
append(QString("Signal %1").
arg(k
));
}
QVector <QwtPlotCurve
*> pMyPlotCurve;
for (k=0; k< 2; k++)
{
if (k < vTextLabels.count())
{
}
pMyPlotCurve[k]->attach(this);
}
/*-------curve styles------*/
QVector<QPen> vColor;
vColor.
append(QPen(Qt
::red));
vColor.
append(QPen(Qt
::blue));
for (k=0; k< 2; k++)
{
pMyPlotCurve[k]->setPen(vColor[k%vColor.count()]);
}
Now, as mentioned in my previous post, I call the plotting curve vector 'pMyPlotCurve' in another class in the slot function of checkbox as:
Code:
QVector<QwtPlotCurve*> pMyPlotCurve;
pMyPlotCurve[channelwidget]->setVisible(true); //or false depending on the 'checkbox' toogling of each channel.
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.
Quote:
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:
Code:
legend1
->setFrameStyle
(QFrame::Box|QFrame
::Sunken);
insertLegend
(legend1,
QwtPlot::TopLegend);
legend2
->setFrameStyle
(QFrame::Box|QFrame
::Sunken);
insertLegend
(legend1,
QwtPlot::RightLegend);
only the last declared widget legend2 is shown in the Qwt plot.
Please advice.
Re: set visibility of legend with its corresponding Curve (multiple Y-axes)
Quote:
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.
Quote:
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.
Quote:
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
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!
Re: set visibility of legend with its corresponding Curve (multiple Y-axes)
Quote:
Originally Posted by
uz_qt
So far from what I have understood, I first have to connect the curves to the legend-items...
Code:
{
Q_OBJECT
public:
{
mLegendLeft->setMaxColums(1 );
mLegendLeft->setSizePolicy( ... );
mLegendRight->setMaxColums(1 );
mLegendRight->setSizePolicy( ... );
layout->addWidget( mLegendLeft );
layout->addWidget( mPlot );
layout->addWidget( mLegendRight );
connect( mPlot,
SIGNAL( legendDataChanged
( const QVariant &,
const QList<QwtLegendData>
& ) ),
this,
SLOT( updateLegend
( const QVariant &,
const QList<QwtLegendData>
& ) ) );
}
private slots:
void updateLegend
( const QVariant &itemInfo,
const QList<QwtLegendData> &data )
{
const QwtPlotItem* plotItem
= mPlot
->infoToItem
( itemInfo
);
if ( plotItem
->yAxis
() == QwtPlot::yRight ) mLegendRight->updateLegend( itemInfo, data );
else
mLegendLeft->updateLegend( itemInfo, data );
}
};
Uwe
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.
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
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:
Code:
1.
pMyPlotCurve[channelwidget
]->setItemAttribute
(QwtPlotItem::Legend,
true);
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?
Re: set visibility of legend with its corresponding Curve (multiple Y-axes)
Code:
void Plot::toggleCurveVisibility()
{
const bool on = !m_curve1->isVisible();
m_curve->setVisible( on );
updateLegend();
}
HTH,
Uwe
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).
Re: set visibility of legend with its corresponding Curve (multiple Y-axes)
Quote:
Originally Posted by
uz_qt
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