Results 1 to 6 of 6

Thread: centered text label

Hybrid View

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

    Default Re: centered text label

    A marker might represent a point or a coordinate only ( HLine, VLine ). In the second case the text is aligned to the coordinate + the canvas rectangle, what is exactly the requirement you have.

    F.e the bode example shows such markers.

    Uwe

  2. #2
    Join Date
    Apr 2009
    Location
    San Francisco, CA
    Posts
    4
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: centered text label

    Ahh! Sorry. You're right, of course. From the source code:
    Qt Code:
    1. 00204 if (d_data->style == HLine)
    2. 00205 {
    3. 00206 if (d_data->align & (int) Qt::AlignLeft)
    4. 00207 dx = r.left() + xLabelDist - tr.x();
    5. 00208 else if (d_data->align & (int) Qt::AlignRight)
    6. 00209 dx = r.right() - xLabelDist + tr.x();
    7. 00210 else
    8. 00211 dx = r.left() + r.width() / 2;
    9. 00212 }
    To copy to clipboard, switch view to plain text mode 

    Which is basically what I had (except I assumed r.left() = 0).

    The exercise hasn't been a waste, however. In this example, I align the text to a diagonal of a plot, where for each label, x * y = watts:
    Qt Code:
    1. void draw(QPainter *painter,
    2. const QwtScaleMap &xMap, const QwtScaleMap &yMap,
    3. const QRect &rect) const
    4. {
    5. if ((rect.width() > 0) &&
    6. (rect.height() > 0)
    7. ) {
    8. // draw the label along a plot diagonal:
    9. // 1. x*y = watts * dx/dv * dy/df
    10. // 2. x/width = y/height
    11. // =>
    12. // 1. x^2 = width/height * watts
    13. // 2. y^2 = height/width * watts
    14.  
    15. double xscale = fabs(xMap.transform(3) - xMap.transform(0)) / 3;
    16. double yscale = fabs(yMap.transform(600) - yMap.transform(0)) / 600;
    17. double w = watts * xscale * yscale;
    18. int x = xMap.transform(sqrt(w * rect.width() / rect.height()) / xscale);
    19. int y = yMap.transform(sqrt(w * rect.height() / rect.width()) / yscale);
    20.  
    21. // the following code based on source for QwtPlotMarker::draw()
    22. QRect tr(QPoint(0, 0), text.textSize(painter->font()));
    23. tr.moveCenter(QPoint(x, y));
    24. text.draw(painter, tr);
    25. }
    26. }
    To copy to clipboard, switch view to plain text mode 

    I think this would be more challenging within the standard API.

    Thanks!
    Last edited by djconnel; 28th April 2009 at 17:31.

Similar Threads

  1. Replies: 15
    Last Post: 31st January 2020, 18:25
  2. Unhandled exception in qatomic
    By NewGuy in forum Qt Programming
    Replies: 14
    Last Post: 23rd July 2013, 09:49
  3. how to append text in label
    By wagmare in forum Qt Programming
    Replies: 3
    Last Post: 12th March 2009, 11:06
  4. label with image and text
    By mattia in forum Newbie
    Replies: 1
    Last Post: 7th March 2008, 11:28
  5. Replies: 1
    Last Post: 24th October 2006, 16:40

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
  •  
Qt is a trademark of The Qt Company.