Results 1 to 6 of 6

Thread: centered text label

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

    Default centered text label

    I have a QwtPlot where I want to place a label such that it is at a given coordinate on the y-axis but is always centered on the x-axis, even when the axis scale is updated. I'm really lame at this stuff, but I suspect that this would involve handling a "redraw()" method so that the x-position is recalculated. But I'm not sure.

    If I want to draw the label at a gtiven coordinate in both x and y, I do the following:
    Qt Code:
    1. text.setFont(QFont("Helvetica",24, QFont::Bold));
    2. text.setColor(text_color);
    3. QwtPlotMarker *label_mark;
    4. label_mark = new QwtPlotMarker();
    5. label_mark->setValue(x, y);
    6. label_mark->setLabel(text);
    7. label_mark->attach(thisPlot);
    To copy to clipboard, switch view to plain text mode 

    Simple. Is there something equally simple to have x instead be the center of the plot?
    Last edited by djconnel; 25th April 2009 at 23:51.

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

    Default Re: centered text label

    Okay, through trial and error I found a solution. I went into the source code for QwtPlotMarker and found the draw() method. I removed the section dealing with centered QwtText labels, changed the x coordinate from a mapped value to the center of the plot window, and bingo!

    Qt Code:
    1. void draw(QPainter *painter,
    2. const QwtScaleMap &, const QwtScaleMap &yMap,
    3. const QRect &rect) const
    4. {
    5. if (parent->shade_power) {
    6. int x = rect.width() / 2;
    7. int y = yMap.transform(yvalue);
    8.  
    9. // the following code based on source for QwtPlotMarker::draw()
    10. QRect tr(QPoint(0, 0), text.textSize(painter->font()));
    11. tr.moveCenter(QPoint(x, y));
    12. text.draw(painter, tr);
    13. fprintf(stderr, "draw called with x = %d; y = %d\n", x, y);
    14. }
    15. }
    16. };
    To copy to clipboard, switch view to plain text mode 

    It works!

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

    Default Re: centered text label

    Another option is to use the regular API of QwtPlotMarker:

    Qt Code:
    1. marker->setLineStyle(QwtPlotMarker::HLine);
    2. marker->setPen(Qt::NoPen);
    3. marker->setLabelAlignment(Qt::AlignCenter);
    To copy to clipboard, switch view to plain text mode 

    The Qwt docs are far from being good, but ...

    Uwe

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

    Default Re: centered text label

    The documentation says:

    The alignment determines where the label is drawn relative to the marker's position.

    Parameters:
    align Alignment. A combination of AlignTop, AlignBottom, AlignLeft, AlignRight, AlignCenter, AlgnHCenter, AlignVCenter.

    See also:
    labelAlignment()

    Definition at line 312 of file qwt_plot_marker.cpp.


    What I wanted was centered relative to the plot boundaries, not relative to the marker position.

  5. #5
    Join Date
    Feb 2006
    Location
    Munich, Germany
    Posts
    3,312
    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

  6. #6
    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
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.