Results 1 to 8 of 8

Thread: Vertically aligning multiple plots

  1. #1
    Join Date
    Jan 2014
    Posts
    36
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Vertically aligning multiple plots

    I've got multiple plots that I'm attempting to line up vertically in a QVBoxLayout. Each plot has one curve, and I've got the legend turned on. The problem appears to be that layout space is allocated to the legend first based on the length of the legend items' text, and the canvas gets whatever is left. Since my data has different names, the legend associated with each plot takes up a different amount of horizontal space, which causes the plot canvases to have different widths from each other.

    I've tried calling plot->legend()->setFixedWidth(), but that doesn't seem to cause the plot to resize.

  2. #2
    Join Date
    Dec 2013
    Location
    Toronto, Canada
    Posts
    62
    Thanked 16 Times in 15 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Vertically aligning multiple plots

    I've tried calling plot->legend()->setFixedWidth(), but that doesn't seem to cause the plot to resize.
    try....
    static_cast<QwtLegend*>(plot->legend())->contentsWidget()->setFixedWidth(...);

    contentsWidget() returns the parent (widget) of all legend items

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

    Default Re: Vertically aligning multiple plots

    There are different ways to implement this - one is to use a QGridLayout for plots and legends. Instead of QwtPlot::insertLegend() you would have to do something like this:

    Qt Code:
    1. QwtLegend *legend = new QwtLegend;
    2. legend->setMaxColumns( 1 );
    3.  
    4. connect( plot, SIGNAL( legendDataChanged( const QVariant &, const QList<QwtLegendData> & ) ),
    5. legend, SLOT( updateLegend( const QVariant &, const QList<QwtLegendData> & ) ) );
    To copy to clipboard, switch view to plain text mode 
    Uwe

  4. #4
    Join Date
    Jan 2014
    Posts
    36
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Vertically aligning multiple plots

    Thanks Uwe, that seems to work, mostly! The only issue I'm having now is that some of the legends that have longer strings are getting a horizontal scrollbar showing up, so I think I need to modify the minimum width of all the legends to be the appropriate size for the longest string. The good news is that the strings don't changed for the life of the application.

    What's the easiest way to figure out the size I need? I feel like I'm looking in QwtLegend for some function that returns a list of QwtLegendLabels like
    Qt Code:
    1. QList< QWidget * > legendWidgets (const QVariant &) const
    To copy to clipboard, switch view to plain text mode 
    but I guess I'm not sure what I'm supposed to put in for the QVariant object?

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

    Default Re: Vertically aligning multiple plots

    When you see a scroll bar it is from an internal QScrollArea, where your legend items is inside. In your situation the scroll area seems to be too small to show the labels - changing the attributes of the labels won't help as they have their correct size.

    After putting some debug statements into your code: is the width of the legend below its sizeHint ? If yes what happens when assign a different horizontal QSizePolicy ( should be QSizePolicy::MinimumExpanding )?

    Uwe

    By the way: you could also derive from QwtAbstractLegend and implement your own legend, where you simply put QwtLegendLabels into a vertical layout. ( f.e. the stockchart example shows a completely different type of legend ).
    Last edited by Uwe; 30th January 2014 at 07:05.

  6. #6
    Join Date
    Jan 2014
    Posts
    36
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Vertically aligning multiple plots

    When you see a scroll bar it is from an internal QScrollArea, where your legend items is inside. In your situation the scroll area seems to be too small to show the labels - changing the attributes of the labels won't help as they have their correct size.
    I didn't mean to change the attribute of the label itself, I meant to query the each label to find out what size it wanted to be, then calling setFixedWidth() on each legend with based on the maximum label size.

    After putting some debug statements into your code: is the width of the legend below its sizeHint ?
    The size hint width is -1 for some reason? Here's how I'm creating the legends:

    Qt Code:
    1. QwtLegend* legend = new QwtLegend();
    2. legends.append(legend);
    3. legend->setMinimumHeight(0);
    4. legend->setMaxColumns(1);
    5. connect(plot, SIGNAL(legendDataChanged(QVariant,QList<QwtLegendData>)),
    6. legend, SLOT(updateLegend(QVariant,QList<QwtLegendData>)));
    7. grid->addWidget(legend, i, 1);
    To copy to clipboard, switch view to plain text mode 
    And then in my mainWidget::resizeEvent() I've got:
    Qt Code:
    1. foreach (QwtLegend* legend, legends)
    2. {
    3. qDebug("legend width %d, size hint width %d", legend->width(), legend->sizeHint().width());
    4. }
    To copy to clipboard, switch view to plain text mode 
    Which produces:
    legend width 437, size hint width -1

    If yes what happens when assign a different horizontal QSizePolicy ( should be QSizePolicy::MinimumExpanding )?
    But I don't think I want MinimumExpanding for the legends, do I? I've got MinimumExpanding for the QwtPlots in column 0, if I set MinimumExpanding on the QwtLegends the QGridLayout gives 50% width to each column, which isn't what I want. Although I guess could play around with QGridLayout::setColumnStretch(). It seems like I want MinimumExpanding on the plots and Maximum or Preferred on the legends, so the legends get just enough horizontal space to not have a scrollbar, but the plots get the majority of the screen real estate. But putting Maximum on makes it a little too small and then the scrollbars show up.

  7. #7
    Join Date
    Jan 2014
    Posts
    36
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Vertically aligning multiple plots

    I've attached a quick example that shows where I'm struggling. If you look in the MainWindow constructor I've got a
    Qt Code:
    1. #if true
    2. ...
    3. #else
    4. ...
    5. #endif
    To copy to clipboard, switch view to plain text mode 
    sequence. When that preprocessor directive is true, the application looks pretty good, all the plots are lined up, and all the legend items are fully displayed, with no scroll bars showing. If you switch the true to a false then the plots get curves loaded in a slot (which is closer to my applications actual behavior) and in that version the longer strings don't fit as well within their respective legends and some scrollbars are visible.

    I haven't tried inheriting from QwtAbstractLegend yet, I've been trying too many variations on this theme, but I might give it a shot...
    Attached Files Attached Files

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

    Default Re: Vertically aligning multiple plots

    The size hint width is -1 for some reason?
    Probably you checked too early - before the labels have been inserted. Without a valid sizeHint() the layout can't calculate anything useful.
    What is the situation, when you resize the main window ( = forcing a recalculation of the layout ) later, after all legend items are inserted.

    But I don't think I want MinimumExpanding for the legends, do I?
    Of course you want to: it tells the layout not to shrink the legend below its sizeHint(). As you want to have all additional space for the plots you do it with layout->selColumnStretch( 0, 10 ),

    Uwe

    PS: try to feed your layout in a proper way - using setFixedSize is almost always an indication of doing it the wrong way !

Similar Threads

  1. Aligning 2 plots
    By jerrychan in forum Qwt
    Replies: 2
    Last Post: 25th February 2013, 08:12
  2. Multiple Plots in One Window
    By cookie0427 in forum Qwt
    Replies: 3
    Last Post: 4th June 2010, 15:14
  3. Reg multiple plots in Qwt
    By Tavit in forum Qwt
    Replies: 4
    Last Post: 23rd June 2008, 13:43
  4. Lining up multiple plots
    By joel in forum Qwt
    Replies: 2
    Last Post: 20th June 2008, 00:25
  5. Replies: 4
    Last Post: 25th October 2007, 12:23

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.