Results 1 to 10 of 10

Thread: QwtText background padding

  1. #1
    Join Date
    Oct 2013
    Location
    Quebec
    Posts
    31
    Thanks
    4
    Qt products
    Qt5
    Platforms
    Windows

    Default 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 :
    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 );

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

    Default Re: QwtText background padding

    Quote Originally Posted by Maximus2 View Post
    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():

    Qt Code:
    1. QRectF YourTextLabel::textRect(
    2. const QRectF &rect, const QSizeF &textSize ) const
    3. {
    4. QRectF rect = QwtPlotTextLabel::textRect( rect, textSize );
    5. rerturn rect.adjusted( ... );
    6. }
    To copy to clipboard, switch view to plain text mode 
    Uwe

  3. #3
    Join Date
    Oct 2013
    Location
    Quebec
    Posts
    31
    Thanks
    4
    Qt products
    Qt5
    Platforms
    Windows

    Default 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!

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

    Default 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

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

    Maximus2 (9th January 2014)

  6. #5
    Join Date
    Sep 2009
    Posts
    57
    Thanks
    7
    Thanked 5 Times in 4 Posts

    Default Re: QwtText background padding

    Quote Originally Posted by Uwe View Post
    You could try to do some formatting using the rich text syntax or simply overload QwtPlotTextLabel::textRect():

    Qt Code:
    1. QRectF YourTextLabel::textRect(
    2. const QRectF &rect, const QSizeF &textSize ) const
    3. {
    4. QRectF rect = QwtPlotTextLabel::textRect( rect, textSize );
    5. rerturn rect.adjusted( ... );
    6. }
    To copy to clipboard, switch view to plain text mode 
    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 ?

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

    Default 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

  8. #7
    Join Date
    Sep 2009
    Posts
    57
    Thanks
    7
    Thanked 5 Times in 4 Posts

    Default Re: QwtText background padding

    Quote Originally Posted by Uwe View Post
    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()

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

    Default 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

  10. The following user says thank you to Uwe for this useful post:

    liversedge (8th January 2015)

  11. #9
    Join Date
    Feb 2009
    Posts
    8
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default 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

    Qt Code:
    1. QRect ConcentrationMap::ConcentrationPicker::trackerRect(const QFont &font) const
    2. {
    3. return QwtPlotPicker::trackerRect(font).adjusted(-5, -5, 5, 5);
    4. }
    To copy to clipboard, switch view to plain text mode 

    but the result is

    trackertext.png

    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 :/
    Last edited by estan; 5th December 2018 at 13:08.

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

    Default 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

Similar Threads

  1. Replies: 0
    Last Post: 21st November 2013, 07:39
  2. Replies: 2
    Last Post: 16th March 2012, 07:55
  3. QLineEdit padding
    By bunjee in forum Qt Programming
    Replies: 0
    Last Post: 17th September 2008, 23:43
  4. Unwanted padding on QWidget
    By etru1927 in forum Qt Programming
    Replies: 4
    Last Post: 7th August 2008, 08:42
  5. QString:: Padding Zeros?
    By Harvey West in forum Qt Programming
    Replies: 2
    Last Post: 27th February 2007, 19:25

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.