Results 1 to 4 of 4

Thread: Legend identifiers disappear after inserting widget

  1. #1
    Join Date
    Apr 2011
    Posts
    9
    Thanks
    1

    Default Legend identifiers disappear after inserting widget

    Hello all,

    I'm using Qwt 5.2.1 to plot some data in a subclass of QwtPlot. I have the following code:

    Qt Code:
    1. QwtPlotCurve* curve = new QwtPlotCurve("Variable");
    2. curve->attach(this);
    3.  
    4. legend()->insert(curve, new QLabel());
    To copy to clipboard, switch view to plain text mode 

    There's a bunch of other code being executed, but the problem seems to be the legend()->insert() call. If I comment that line out, I get a nice display of the curve's line, symbol, and text (as per the identifierMode() that is set). If that line is left in, the line and symbol display disappear. I check the identifierMode() on each plot update, and it is equal to 7 (QwtLegendItem::ShowLine | QwtLegendItem::ShowSymbol | QwtLegendItem::ShowText) each time. When the insert() call is left in, I can update the QLabel just fine.

    How do I get both the QLabel and the curve's symbol and line into the legend?

    Thanks,
    - Tom

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

    Default Re: Legend identifiers disappear after inserting widget

    There is only one widget allowed ( it can be a composite one ) , that represents a plot item on the legend. Inserting a QLabel replaces but doesn't add something. Of course you could use a dummy plot item ( maybe a NULL works too - but I have never tries this ).

    Also consider to reimplement YourCurve::legendItem/updateLegend instead of inserting a label manually.

    Uwe

  3. #3
    Join Date
    Apr 2011
    Posts
    9
    Thanks
    1

    Default Re: Legend identifiers disappear after inserting widget

    Also consider to reimplement YourCurve::legendItem/updateLegend instead of inserting a label manually.
    I'll have to try that, thanks.

    There is only one widget allowed ( it can be a composite one ) , that represents a plot item on the legend. Inserting a QLabel replaces but doesn't add something.
    At one point I (accidentally) had multiple labels being displayed for the same plot item. I didn't try updating them all, but I had several labels per plot item all with the same text. Maybe I'll try reproducing that.

    Thanks,
    - Tom

  4. #4
    Join Date
    Apr 2011
    Posts
    9
    Thanks
    1

    Default Re: Legend identifiers disappear after inserting widget

    I tried subclassing QwtPlotCurve, and it seems to ALMOST be working. Here's my code for MyQwtPlotCurve:

    Qt Code:
    1. // MyQwtPlotCurve.h //
    2. class MyQwtPlotCurve : public QwtPlotCurve
    3. {
    4. public:
    5. MyQwtPlotCurve();
    6. MyQwtPlotCurve(const QString& title);
    7.  
    8. void updateLegend(QwtLegend* legend);
    9. void enable_legend_value(bool on);
    10. void set_legend_value(double value);
    11.  
    12. private:
    13. bool m_value_enabled;
    14. double m_value;
    15. };
    To copy to clipboard, switch view to plain text mode 

    and the relevant part of the cpp file:

    Qt Code:
    1. void MyQwtPlotCurve::updateLegend(QwtLegend* legend)
    2. {
    3. QwtPlotCurve::updateLegend(legend);
    4.  
    5. printf("Update legend!\n")
    6.  
    7. if (m_value_enabled)
    8. {
    9. QWidget *widget = legend->find(this);
    10. if ( !widget || !widget->inherits("QwtLegendItem") )
    11. return;
    12.  
    13. QwtLegendItem* legendItem = (QwtLegendItem*)widget;
    14.  
    15. QString text = legendItem->text().text();
    16. QString new_text = text + " " + QString().setNum(m_value);
    17. printf("Trying to set marker text: '%s'\n", new_text.toLocal8Bit().constData());
    18. legendItem->setText(new_text);
    19.  
    20. legendItem->update();
    21. }
    22. }
    To copy to clipboard, switch view to plain text mode 

    That first printf is never printed unless I call updateLegend explicitly. I can then see a flicker as my extra text appears, but it seems to be immediately overwritten.

    I'm only using instances of MyQwtPlotCurve, so why isn't my updateLegend() being used?

    I'm using Qwt 5.2.1.

    Thanks,
    - Tom

    EDIT: Found my error! Forgot the "const" modifier on MyQwtPlotCurve::updateLegend().
    Last edited by orignihn; 3rd June 2011 at 15:31.

Similar Threads

  1. Can't get title bar to disappear from widget
    By MattPhillips in forum Qt Programming
    Replies: 11
    Last Post: 2nd November 2010, 14:41
  2. Replies: 2
    Last Post: 2nd September 2010, 19:52
  3. QWidget - Q_OBJECT makes the widget disappear
    By Sir Rogers in forum Qt Programming
    Replies: 6
    Last Post: 25th January 2010, 23:39
  4. QLabel::setText causes part of widget to disappear
    By Pieter from Belgium in forum Qt Programming
    Replies: 4
    Last Post: 6th July 2007, 09:22
  5. Replies: 9
    Last Post: 8th May 2006, 14:21

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.