Results 1 to 10 of 10

Thread: QwtPlot is broken (size constraints disregarded)

  1. #1
    Join Date
    Mar 2016
    Posts
    16
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default QwtPlot is broken (size constraints disregarded)

    I've been breaking my head over this. My application works fine until I add QwtPlot. It seems like QwtPlot likes to be 200x200 no matter what. Stretch factors don't seem to work with it, size hints don't seem to affect it, size policies have no effect... This widget is broken and I don't know how to fix it.

    Here are some examples!

    QwtPlot resizes as it pleases, disregarding layout constraints.

    Open QtDesigner, add an MDI area, add two sub-windows and add two QwtPlots in each (with layouts, so everything resizes when stretching the window). When switching between the two tabs, the QwtPlot will decide to resize to 200x200 and push the other plot out of view:
    https://youtu.be/pXkut7SwKxY

    The same is true if you build this structure in code. The following video shows the same issue in my application:
    https://youtu.be/-oMESWIeEhM


    QwtPlot doesn't obey stretch factors (or it acts weirdly). This next video requires some explanation: I want the plot at the very bottom to have a fixed Y size. The other two plots at the top should scale evenly when the window is resized. This works fine when QwtPlot is not added to the layout. When I add QwtPlot, it no longer does what it should. Notice how the plot at the top actually stretches larger than the one in the middle. Why? Why is this happening?

    https://youtu.be/MBIITNObzzg


    Things I've tried

    Setting row stretch factors does not fix the second issue. QwtPlot ignores these.
    Setting the size policy to "Minimum" (or anything else for that matter) has no effect on QwtPlot. It doesn't care.
    Manually calling resize() on the main window whenever a subwindow is activated doesn't help. QwtPlot will resize however it sees fit.

    I'm out of ideas and would really appreciate some help here.

    "People who get offended should be offended" -- Linus Torvalds

  2. #2
    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: QwtPlot is broken (size constraints disregarded)

    A plot widget never changes its geometry on its own. In general the geometries are the job for the layout - not the elements being inside of the layout.

    The elements inside of the layout provide some hints ( sizePolicy(), sizeHint() ) and that's it. So if your layout code assigns a bad geometry to its elements you have to fix your layout code.

    Uwe

  3. #3
    Join Date
    Mar 2016
    Posts
    16
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QwtPlot is broken (size constraints disregarded)

    I'm not sure I understand. If I add any other widget (let's say QPushButton) in place of QwtPlot, all of the widgets resize correctly as expected. If, however, I use QwtPlot, that's when things start behaving wrong. So it must have something to do with QwtPlot. Or have I misunderstood what you mean?

    Have you tried the thing I explained in QtDesigner with the two mdi subwindows? That shows the exact behaviour I'm talking about. I've attached a ui file for convenience: qwtplot_issues.ui

    Switch between the mdi tabs and you'll see that the QwtPlot resizes to something it shouldn't.

    Also stretch the window on the Y axis and observe how the plots unevenly stretch. Why is this happening?

    If you replace the QwtPlot widgets with any other widget it works.

    [EDIT] OK so this is really strange. I re-opened the ui file and now I can't resize the window any smaller than 200x400. It seems you have to create the QwtPlot widgets in a window that is smaller than the initial 200x200 plot widget size.

    Either way, the resize behaviour of QwtPlot isn't doing what it's supposed to and I don't think it's due to my layout code.
    Last edited by TheComet; 15th August 2016 at 16:20.
    "People who get offended should be offended" -- Linus Torvalds

  4. #4
    Join Date
    Mar 2016
    Posts
    16
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QwtPlot is broken (size constraints disregarded)

    I have come closer to the actual problem.

    If I add widgets to my layout in the constructor, everything works (even with QwtPlot). If I add my widgets to the layout later on (e.g. after loading a file) that's when the layout constraints are ignored.

    What do you think could be causing this?
    "People who get offended should be offended" -- Linus Torvalds

  5. #5
    Join Date
    Mar 2016
    Posts
    16
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QwtPlot is broken (size constraints disregarded)

    Alright I have an example that reproduces the problem I'm having.

    layoutproblem.h
    Qt Code:
    1. #ifndef LAYOUTPROBLEM_H
    2. #define LAYOUTPROBLEM_H
    3.  
    4. #include <QWidget>
    5.  
    6. class QMdiArea;
    7.  
    8. class LayoutProblem : public QWidget
    9. {
    10. Q_OBJECT
    11.  
    12. public:
    13. explicit LayoutProblem(QWidget *parent = 0);
    14. virtual ~LayoutProblem();
    15.  
    16. private slots:
    17. void createUI();
    18.  
    19. private:
    20. QMdiArea* mdiArea_;
    21. };
    22.  
    23. #endif // LAYOUTPROBLEM_H
    To copy to clipboard, switch view to plain text mode 

    layoutproblem.cpp
    Qt Code:
    1. #include "layoutproblem.h"
    2.  
    3. #include <QVBoxLayout>
    4. #include <QGridLayout>
    5. #include <QMdiArea>
    6. #include <QTimer>
    7. #include <QTabWidget>
    8. #include <QApplication>
    9.  
    10. #include <qwt_plot.h>
    11.  
    12.  
    13. LayoutProblem::LayoutProblem(QWidget *parent) :
    14. QWidget(parent),
    15. mdiArea_(new QMdiArea)
    16. {
    17. setLayout(new QGridLayout);
    18. layout()->addWidget(mdiArea_);
    19. mdiArea_->setViewMode(QMdiArea::TabbedView);
    20.  
    21. resize(200, 200);
    22.  
    23. QTimer::singleShot(500, this, SLOT(createUI()));
    24. // createUI();
    25. }
    26.  
    27. LayoutProblem::~LayoutProblem()
    28. {
    29. }
    30.  
    31. void LayoutProblem::createUI()
    32. {
    33. for(unsigned i = 0; i != 3; ++i)
    34. {
    35. QWidget* window = new QWidget;
    36. window->setLayout(new QVBoxLayout);
    37. window->layout()->addWidget(new QwtPlot);
    38. window->layout()->addWidget(new QwtPlot);
    39.  
    40. QTabWidget* tab = new QTabWidget;
    41. tab->addTab(window, "tab " + QString::number(i));
    42.  
    43. mdiArea_->addSubWindow(tab);
    44. tab->showMaximized();
    45. }
    46. }
    47.  
    48. int main(int argc, char *argv[])
    49. {
    50. QApplication a(argc, argv);
    51. LayoutProblem w;
    52. w.show();
    53.  
    54. return a.exec();
    55. }
    To copy to clipboard, switch view to plain text mode 

    Run this code and you will notice that you can scale the widget much smaller than the minimum size hint. Next, try commenting the QTimer line and call createUI() directly:

    Qt Code:
    1. // QTimer::singleShot(500, this, SLOT(createUI()));
    2. createUI();
    To copy to clipboard, switch view to plain text mode 

    Re-run the code and now everything works as expected.

    How can I add widgets to my layouts dynamically without Qt ignoring the minimum size hints?
    "People who get offended should be offended" -- Linus Torvalds

  6. #6
    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: QwtPlot is broken (size constraints disregarded)

    Quote Originally Posted by TheComet View Post
    Alright I have an example that reproduces the problem I'm having.
    Sorry, but your problem is a general layout issue - obviously unrelated to Qwt. Better create a new request ( replacing QwtPlot by a standard QWidget ) in the programming section.

    Uwe

  7. #7
    Join Date
    Mar 2016
    Posts
    16
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QwtPlot is broken (size constraints disregarded)

    "People who get offended should be offended" -- Linus Torvalds

  8. #8
    Join Date
    Mar 2016
    Posts
    16
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QwtPlot is broken (size constraints disregarded)

    Quote Originally Posted by Uwe View Post
    Sorry, but your problem is a general layout issue - obviously unrelated to Qwt. Better create a new request ( replacing QwtPlot by a standard QWidget ) in the programming section.
    No, there is definitely a problem with Qwt as well. If I set the row stretch factors to "1, 1, 0" (I want the widget at the very bottom to always have minimum height), QwtPlot doesn't obey.

    The stretch factors work fine with any Qt widget. Here's a QPushButton:
    1.png

    The stretch factors DON'T work with QwtPlot:
    2.png

    QwtPlot tries to resize to 200x200 and disregards the stretch factors. Am I doing something wrong?

    qwtplotproblem.h
    Qt Code:
    1. #ifndef QWTPLOTPROBLEM_H
    2. #define QWTPLOTPROBLEM_H
    3.  
    4. #include <QWidget>
    5.  
    6. class QwtPlotProblem : public QWidget
    7. {
    8. Q_OBJECT
    9.  
    10. public:
    11. explicit QwtPlotProblem(QWidget *parent = 0);
    12. ~QwtPlotProblem();
    13.  
    14. private slots:
    15. void createUI();
    16. };
    17.  
    18. #endif // QWTPLOTPROBLEM_H
    To copy to clipboard, switch view to plain text mode 

    qwtplotproblem.cpp
    Qt Code:
    1. #include "qwtplotproblem.h"
    2.  
    3. #include <QGridLayout>
    4. #include <QPushButton>
    5. #include <QTimer>
    6.  
    7. #include "qwt_plot.h"
    8.  
    9. QwtPlotProblem::QwtPlotProblem(QWidget *parent) :
    10. QWidget(parent)
    11. {
    12. setLayout(new QGridLayout);
    13. // QTimer::singleShot(500, this, SLOT(createUI()));
    14. createUI();
    15. }
    16.  
    17. QwtPlotProblem::~QwtPlotProblem()
    18. {
    19. }
    20.  
    21. void QwtPlotProblem::createUI()
    22. {
    23. QwtPlot* plot = new QwtPlot;
    24. plot->setMinimumSize(40, 40);
    25. plot->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Preferred);
    26. layout()->addWidget(plot);
    27.  
    28. plot = new QwtPlot;
    29. plot->setMinimumSize(40, 40);
    30. plot->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Preferred);
    31. layout()->addWidget(plot);
    32.  
    33. plot = new QwtPlot;
    34. plot->setMinimumSize(40, 40);
    35. plot->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Preferred);
    36. layout()->addWidget(plot);
    37.  
    38. QGridLayout* l = static_cast<QGridLayout*>(layout());
    39. l->setRowStretch(0, 1);
    40. l->setRowStretch(1, 1);
    41. l->setRowStretch(2, 0);
    42. }
    To copy to clipboard, switch view to plain text mode 
    "People who get offended should be offended" -- Linus Torvalds

  9. #9
    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: QwtPlot is broken (size constraints disregarded)

    No, there is definitely a problem with Qwt as well. If I set the row stretch factors to "1, 1, 0" (I want the widget at the very bottom to always have minimum height), QwtPlot doesn't obey.
    There is no "obeying": if you ( or your layout ) set a geometry to a plot it has this geometry, if you set the geometry being below the minimum size hint things will be cut off. That's it - the plot widget doesn't refuse any geometries.

    Your problem seems to be, that the geometries calculated by your layout are not those you want to have. But this is again unrelated to Qwt - it is your layout code.

    Here's a QPushButton:
    A pushbutton has different size hints - the hints for a plot depend on the space, that is needed to display the labels.

    I recommend to spend some time on trying to understand how the QLayout system works: f.e. your layout first distributes the width/height according to the size hints of the widgets - what are different values. Only when there is more space available it decides according to the stretch factors ( and size policies ) how to distribute the rest.

    To make it short: overload QwtPlot::sizeHint() returning something else and see how it works.

    Uwe

  10. #10
    Join Date
    Mar 2016
    Posts
    16
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QwtPlot is broken (size constraints disregarded)

    Quote Originally Posted by Uwe View Post
    To make it short: overload QwtPlot::sizeHint() returning something else and see how it works.
    Thanks, this is the solution I went with.

    Qt Code:
    1. class MyPlot : public QwtPlot {
    2. protected:
    3. virtual QSize sizeHint() const { return QSize(1, 1); }
    4. };
    To copy to clipboard, switch view to plain text mode 

    In conjunction with:
    Qt Code:
    1. MyPlot* plot = new MyPlot;
    2. plot->setMinimumSize(X, Y); // replace X and Y with something suitable
    3. plot->setSizePolicy(QSizePolicy::Minimum, QSizePolicy::Minimum);
    To copy to clipboard, switch view to plain text mode 
    "People who get offended should be offended" -- Linus Torvalds

Similar Threads

  1. Size / Resize on qwtplot
    By carre in forum Qwt
    Replies: 1
    Last Post: 16th July 2015, 06:21
  2. Set size of QwtPlot/canvas
    By Hudo in forum Qwt
    Replies: 0
    Last Post: 23rd August 2012, 19:10
  3. Replies: 0
    Last Post: 26th October 2010, 17:59
  4. Main Window minimal size constraints
    By psih128 in forum Qt Programming
    Replies: 1
    Last Post: 3rd August 2010, 17:06
  5. Applying size constraints to dock widgets
    By fullmetalcoder in forum Qt Programming
    Replies: 4
    Last Post: 23rd October 2007, 13:22

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.