QwtText background padding
Hi again,
I'm using QwtText in order to show a message on a QwtPlot.
I would like my QwtText to have some padding
I'm trying to do it in a dirty way, i'm adding some space so that the rectangle grow bigger.
See here : https://www.dropbox.com/s/7tojqexfon0b1ag/QwtTextEx.png
Problem I have with this approach is that the text is not centered no matter how I add the space
I also tried to subclass QwtText but I can't use the private field used in the draw() method, I would just increase the QRect size
or maybe I can write code directly in QwtText, but I'm not sure i'm allowed to do that and not good when Qwt has update..
if you have an idea thanks :)
Current code :
Quote:
void WorkoutPlot::setDisplayMessage(QString text, bool workoutPausedMsg) {
/// DISPLAY MSG
QFont fontBig;
fontBig.setPointSize(16);
int lengthText = text.length();
QString emptySpace = " ";
QString emptyLine;
for (int i=0; i<lengthText + 8; i++) {
emptyLine+= emptySpace;
}
QString msg = emptyLine + "\n";
msg += " " + text + " " + "\n";
msg += emptyLine;
displayMsgTxt = QwtText( msg );
displayMsgTxt.setFont(fontBig);
displayMsgTxt.setRenderFlags( Qt::AlignHCenter | Qt::AlignHCenter );
displayMsgTxt.setColor(Qt::white);
if (workoutPausedMsg)
displayMsgTxt.setBackgroundBrush(QBrush(QColor(1, 1, 1, 128), Qt::SolidPattern));
displayMessageLabel = new QwtPlotTextLabel();
displayMessageLabel->setText( displayMsgTxt );
displayMessageLabel->attach( this );
Re: QwtText background padding
Quote:
Originally Posted by
Maximus2
I would like my QwtText to have some padding
You could try to do some formatting using the rich text syntax or simply overload QwtPlotTextLabel::textRect():
Code:
QRectF YourTextLabel
::textRect( {
QRectF rect
= QwtPlotTextLabel
::textRect( rect, textSize
);
rerturn rect.adjusted( ... );
}
Uwe
Re: QwtText background padding
Thanks Uwe,
Finally I decided to go with a QLabel on top of the canvas and customize it with stylesheet
See you!
Re: QwtText background padding
But be aware, that this label is not seen by QwtPlotRenderer and willl be missing, if you want to create something like a PDF document.
Uwe
Re: QwtText background padding
Quote:
Originally Posted by
Uwe
You could try to do some formatting using the rich text syntax or simply overload QwtPlotTextLabel::textRect():
Code:
QRectF YourTextLabel
::textRect( {
QRectF rect
= QwtPlotTextLabel
::textRect( rect, textSize
);
rerturn rect.adjusted( ... );
}
Uwe
QwtPlotTextLabel::textRect is not static .. and you don't have the text to be able to create a temporary one ?
Or did I misunderstand ?
Re: QwtText background padding
YourTextLabel is derived from QwtTextLabel and the method above is an overloading method ( calling the overloaded one from the base class ).
Uwe
Re: QwtText background padding
Quote:
Originally Posted by
Uwe
YourTextLabel is derived from QwtTextLabel and the method above is an overloading method ( calling the overloaded one from the base class ).
Uwe
Oh, the original question (and what I was trying to do myself) was add padding to a QwtText not a QwtTextLabel.
In my case because I reimplementing QwtPlotPicker::trackerText()
Re: QwtText background padding
Then you have to overload QwtPicker::trackerRect() - the idea is the same.
The target rectangle in a parameter of the draw operation, not an attribute of the text object.
Uwe
1 Attachment(s)
Re: QwtText background padding
Hey Uwe, replying to this old thread since this is the exact thing I'm struggling with now.
I tried overriding trackerRect in my QwtPlotPicker subclass with
Code:
QRect ConcentrationMap
::ConcentrationPicker::trackerRect(const QFont &font
) const {
}
but the result is
Attachment 12982
In my case, I'd like a margin around the text, not just to the right/bottom. Do you know if this is possible somehow?
but
Added after 1 21 minutes:
I tried working around this with a custom text engine derived from QwtRichTextEngine which overrides textMargins, e.g. setTextEngine(QwtText::OtherFormat, new TextEngine()) and then m_text.setText(..., QwtText::OtherFormat) on my text, but despite this the textMargins() of my custom engine is never called :/
Re: QwtText background padding
Overloading QwtPlotPicker::trackerRect - like you did - should be good enough.
I checked your code in the spectrogram example and it works like expected - also with rich text content.
So there has to be something in your code, what makes a difference.
Uwe