Hi,

I'm trying to vertically align two QwtPlot. The upper plot has multiple Y-axis so these two plots should share the same X-axis position a limits.
I've seen other solution but here is how I want to do that:
QBoxLayout allows to insert spacing insertSpacing(int index, int size). So I get the x-canvas position of upper plot and of lower plot. Then calculate the difference between the and add this difference to lower plot via insertSpacing(int index, int size)
the problem is that it works only if multiply this difference by 1.3 coeff then no matter how many Y-axis has upper plot, both plots "share the same X-axis". Why units of plot->canvas()->geometry() and QBoxLayout::insertSpacing(int index, int size) are different (why I need to multiply it?)?
Qt Code:
  1. #include "mainwindow.h"
  2. #include "ui_mainwindow.h"
  3.  
  4. #include <QLayout>
  5. #include <qwt_plot.h>
  6.  
  7. MainWindow::MainWindow(QWidget *parent)
  8. : QMainWindow(parent)
  9. , ui(new Ui::MainWindow)
  10. {
  11. ui->setupUi(this);
  12.  
  13. upperPlot = new QwtPlot(this);
  14. lowerPlot = new QwtPlot(this);
  15. upperPlot->setAxesCount(0, 4);
  16. upperPlot->setMinimumSize(10,10);
  17. lowerPlot->setMinimumSize(10,10);
  18.  
  19. QWidget* upperWidget = new QWidget;
  20. QVBoxLayout* mainLayout = new QVBoxLayout(upperWidget);
  21.  
  22. QWidget* lowerWidget = new QWidget;
  23. lowerLayout = new QHBoxLayout(lowerWidget);
  24. lowerLayout->setContentsMargins(0, 0, 0, 0);
  25. lowerLayout->addWidget(lowerPlot);
  26.  
  27. mainLayout->addWidget(upperPlot);
  28. mainLayout->addWidget(lowerWidget);
  29.  
  30. setCentralWidget(upperWidget);
  31. }
  32.  
  33. MainWindow::~MainWindow()
  34. {
  35. delete ui;
  36. }
  37.  
  38. void MainWindow::showEvent(QShowEvent *event){
  39. int dx = upperPlot->canvas()->x() - lowerPlot->canvas()->x();
  40. lowerLayout->insertSpacing(0, 1.3*dx);
  41. }
To copy to clipboard, switch view to plain text mode 
1.jpg