PDA

View Full Version : Is it possible to put footer in the same line of legend?



freshairfly
19th January 2014, 16:37
Hi All,

I need add a label under the plot, and the footer seems what I need.
But I don't want the label occupy an extra line. That is I hope put the lable in the same line of legend. and the legend should be right alignment. Just like this:
9951

Any advice/hint is appreciated.

Uwe
21st January 2014, 07:30
Unfortunately I don't see an easy solution out of the box.



You could manually add a QLabel with your footer text and align it manually according to resize events of the legend. When using QwtPlotRenderer you have to add some code for your footer too.
Another option might be to overload QwtPlotLayout::activate() and manipulate the rects for footer and legend.
You could also derive from QwtLegend and try to add your footer there


Uwe

freshairfly
22nd January 2014, 04:52
Uwe,

I'd done it by your advice#3. And it works as expected exactly.
Thank you very much.


TrendPlotLegend::TrendPlotLegend(QWidget* parent)
: QwtLegend(parent)
{
contentsWidget()->layout()->setAlignment(Qt::AlignRight | Qt::AlignTop);

QHBoxLayout* hbLayout = new QHBoxLayout(this);
hbLayout->setContentsMargins(10, 0, 5, 0);
hbLayout->setSpacing(0);

m_footer = new QLabel(this);
hbLayout->addWidget(m_footer, 0, Qt::AlignTop);

// the old layout is a QVBoxLayout/ replace it with QHBoxLayout
QLayout* oldLayout = layout();
for (int i = 0; i < oldLayout->count(); ++i) {
hbLayout->addItem(oldLayout->takeAt(i));
}
delete oldLayout;
setLayout(hbLayout);
}

QSize TrendPlotLegend::sizeHint() const
{
QSize size = QwtLegend::sizeHint();

int width = size.width() + m_footer->sizeHint().width();
size.setWidth(qMax(parentWidget()->width(), width));
return size;
}

Uwe
22nd January 2014, 06:08
To make it complete you would also have to overload QwtLegend::renderLegend adding some code for your footer. Otherwise your footer will be missing, when using QwtPlotRenderer ( f.e. for generating pdf documents ).

Uwe