Results 1 to 7 of 7

Thread: Qwt : align center the date/time label text on an bottom axis

  1. #1
    Join Date
    Jul 2018
    Posts
    6
    Thanks
    1
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default Qwt : align center the date/time label text on an bottom axis

    With Qwt, I want that the time and the date are both center under the stick under a bottom axis, but the time is still on the left side like in this picture :

    Capture.jpg

    I have tried to use

    Qt Code:
    1. QwtDateScaleDraw::setLabelAlignment(Qt::AlignHCenter|Qt::AlignBottom);
    To copy to clipboard, switch view to plain text mode 

    But the time is still on the left.

    I have tried to subclass the QwtScaleDraw like this:

    Qt Code:
    1. class MyDateScaleDraw:public QwtDateScaleDraw
    2. {
    3. public:
    4. QwtText label(double value) const override
    5. {
    6. QwtText txt=QwtDateScaleDraw::label(value);
    7. txt.setRenderFlags(Qt::AlignHCenter);
    8. return txt;
    9. }
    10. };
    To copy to clipboard, switch view to plain text mode 

    But the time is still on the left side.

    Somebody know how to center the time ?

  2. #2
    Join Date
    Jul 2018
    Posts
    3
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android

    Default Re: Qwt : align center the date/time label text on an bottom axis

    Actually I didn't get which way you are following:
    If it is Layout design
    http://doc.qt.io/qt-5/qtwidgets-layo...s-example.html

    take QGridlayout and add your label at pirtuculer position
    or
    my_label ->setGeometry(x,y,width,hight);

  3. #3
    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: Qwt : align center the date/time label text on an bottom axis

    Qt Code:
    1. class YourScaleDraw: public QwtScaleDraw
    2. {
    3. virtual QwtText label( double value ) const override
    4. {
    5. auto text = QwtScaleDraw::label( value );
    6. text.setRenderFlags( ... );
    7. return text;
    8. }
    9. };
    10.  
    11. plot->setAxisScaleDraw( ..., new YourScaleDraw() );
    To copy to clipboard, switch view to plain text mode 
    The render flags go as flags into https://doc.qt.io/qt-5/qpainter.html#drawText-4.
    If you like to do more fancy texts you can consider using rich text labels.

    In your case you have to derive from QwtDateScaleDraw - the idea is the same.

    Uwe
    Last edited by Uwe; 19th July 2018 at 10:31.

  4. #4
    Join Date
    Jul 2018
    Posts
    6
    Thanks
    1
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Qwt : align center the date/time label text on an bottom axis

    Thanks Uwe for your answer,

    Your suggestion is what I have tried (see the code in my post), but the
    Qt Code:
    1. setRenderFlags
    To copy to clipboard, switch view to plain text mode 
    in MyDateScaleDraw class doesn't change the alignment of the axis label...
    Last edited by vcloarec; 19th July 2018 at 15:35.

  5. #5
    Join Date
    Jul 2018
    Posts
    6
    Thanks
    1
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Qwt : align center the date/time label text on an bottom axis

    I have found in the source code of Qwt this:

    Qt Code:
    1. const QwtText &QwtAbstractScaleDraw::tickLabel(
    2. const QFont &font, double value ) const
    3. {
    4. QMap<double, QwtText>::const_iterator it = d_data->labelCache.find( value );
    5. if ( it == d_data->labelCache.end() )
    6. {
    7. QwtText lbl = label( value );
    8. lbl.setRenderFlags( 0 );
    9. lbl.setLayoutAttribute( QwtText::MinimumLayout );
    10.  
    11. ( void )lbl.textSize( font ); // initialize the internal cache
    12.  
    13. it = d_data->labelCache.insert( value, lbl );
    14. }
    15.  
    16. return ( *it );
    17. }
    To copy to clipboard, switch view to plain text mode 

    It is the method who, I guess, generate the QwtText Label.

    It seems that the line 8 set the render flags to '0'. May be it why changing the renderFlags of the label don't change the alignement of the label under the tick.

    What do you think ?


    Added after 10 minutes:


    That's it

    Instead of override the method
    Qt Code:
    1. label(double value)
    To copy to clipboard, switch view to plain text mode 
    .

    I have to override the drawlable method:

    Qt Code:
    1. void drawLabel( QPainter *painter, double value ) const override
    2. {
    3. QwtText lbl = tickLabel( painter->font(), value );
    4. if ( lbl.isEmpty() )
    5. return;
    6.  
    7. lbl.setRenderFlags(Qt::AlignCenter); //<---- add this line in the subclass
    8.  
    9. QPointF pos = labelPosition( value );
    10.  
    11. QSizeF labelSize = lbl.textSize( painter->font() );
    12.  
    13. const QTransform transform = labelTransformation( pos, labelSize );
    14.  
    15. painter->save();
    16. painter->setWorldTransform( transform, true );
    17.  
    18. lbl.draw ( painter, QRect( QPoint( 0, 0 ), labelSize.toSize() ) );
    19.  
    20. painter->restore();
    21.  
    22. }
    To copy to clipboard, switch view to plain text mode 

    And after the label is really center !


    the setRenderFlags do not the job !!!
    Last edited by vcloarec; 19th July 2018 at 20:20. Reason: updated contents

  6. #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: Qwt : align center the date/time label text on an bottom axis

    Quote Originally Posted by vcloarec View Post
    What do you think ?
    Usually setting an alignment does not make much sense as the label is rendered into its bounding rectangle and when there is no extra space there is nothing to align.
    That might be the reason, why the flags have been explicitly set to 0 - but of course this is wrong for text with newlines inside.
    But having a pointless alignment might have some negative impact on the performance when considering rich text labels.

    One possible fix would be to set the flags to 0 inside of the default implementation of QwtAbstractScaleDraw::label.
    Then your initial implementation overloading QwtAbstractScaleDraw::label would work, but then all other applications overloading this method will have a side effect.

    So I need to check a couple of things first, before deciding, what s the best way for fixing it.

    Uwe

  7. #7
    Join Date
    Jul 2018
    Posts
    6
    Thanks
    1
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Qwt : align center the date/time label text on an bottom axis

    Thanks Uwe for your answer,

    For the moment, I will use my fix.

    An other thanks for the Qwt library which render me a lot services !

Similar Threads

  1. Date-time axis
    By Cat in forum Qwt
    Replies: 1
    Last Post: 20th October 2016, 05:46
  2. Align WindowTitle text to the Center in Windows 10
    By santosh.kumar in forum Qt Programming
    Replies: 3
    Last Post: 6th November 2015, 16:53
  3. Align text and icon of QToolButton on center
    By bibhukalyana in forum Qt Programming
    Replies: 7
    Last Post: 24th November 2014, 13:46
  4. KDChart Date-time Axis howto?
    By galrub in forum Qt Programming
    Replies: 0
    Last Post: 15th October 2012, 12:36
  5. Time/Date Axis
    By sigger in forum Qwt
    Replies: 12
    Last Post: 1st May 2011, 10:55

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.