在ScrollArea中嵌入QCustomPlot

时间:2017-04-29 00:55:01

标签: qt qt5 qcustomplot

我对qcustomplot和qt相当新(从pyqt + matplotlib切换到性能升级/学习)。我一直在研究qcustomplot的教程,但所有都围绕着绘制一个用Qwidget升级到QCustomPlot的UI,这很棒但是如果我想要多个Qcustomplot / subplots with fixed / view-able,我想要手动绘图尺寸。到目前为止,我一直在尝试创建一个具有多个轴的customplot,以查看轴是否在滚动区域内改变大小,但到目前为止还没有运气。任何帮助,将不胜感激。

标题文件

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>

namespace Ui {
class MainWindow;
}

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    explicit MainWindow(QWidget *parent = 0);
    ~MainWindow();

private:
    Ui::MainWindow *ui;
};

#endif // MAINWINDOW_H

cpp如下:

#include "ui_mainwindow.h"
#include "qcustomplot.h"
#include <QWidget>

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    QWidget plotWidget;
    QVBoxLayout layout;
    QCustomPlot customPlot;
    for(int i = 0; i < 10; i++)
    {
        customPlot.plotLayout()->insertRow(i);
        QCPAxisRect *ar = new QCPAxisRect(&customPlot);
        customPlot.plotLayout()->addElement(i, 0, ar);
    }
    /*
    layout.addWidget(customPLot);
    plotWidget.setLayout(layout);
    */
    ui->plotArea_SA->setWidget(customPlot);
}

MainWindow::~MainWindow()
{
    delete ui;
}

和UI

<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
 <class>MainWindow</class>
 <widget class="QMainWindow" name="MainWindow">
  <property name="geometry">
   <rect>
    <x>0</x>
    <y>0</y>
    <width>1269</width>
    <height>454</height>
   </rect>
  </property>
  <property name="windowTitle">
   <string>MainWindow</string>
  </property>
  <widget class="QWidget" name="centralWidget">
   <widget class="QScrollArea" name="plotArea_SA">
    <property name="geometry">
     <rect>
      <x>169</x>
      <y>9</y>
      <width>1091</width>
      <height>381</height>
     </rect>
    </property>
    <property name="horizontalScrollBarPolicy">
     <enum>Qt::ScrollBarAlwaysOff</enum>
    </property>
    <property name="widgetResizable">
     <bool>false</bool>
    </property>
    <widget class="QWidget" name="scrollAreaWidgetContents">
     <property name="geometry">
      <rect>
       <x>0</x>
       <y>0</y>
       <width>1089</width>
       <height>379</height>
      </rect>
     </property>
    </widget>
   </widget>
   <widget class="QScrollArea" name="searchTable_SA">
    <property name="geometry">
     <rect>
      <x>0</x>
      <y>10</y>
      <width>161</width>
      <height>151</height>
     </rect>
    </property>
    <property name="widgetResizable">
     <bool>true</bool>
    </property>
    <widget class="QWidget" name="scrollAreaWidgetContents_2">
     <property name="geometry">
      <rect>
       <x>0</x>
       <y>0</y>
       <width>159</width>
       <height>149</height>
      </rect>
     </property>
    </widget>
   </widget>
   <widget class="QScrollArea" name="displayTable_SA">
    <property name="geometry">
     <rect>
      <x>0</x>
      <y>170</y>
      <width>161</width>
      <height>221</height>
     </rect>
    </property>
    <property name="widgetResizable">
     <bool>true</bool>
    </property>
    <widget class="QWidget" name="scrollAreaWidgetContents_3">
     <property name="geometry">
      <rect>
       <x>0</x>
       <y>0</y>
       <width>159</width>
       <height>219</height>
      </rect>
     </property>
    </widget>
   </widget>
  </widget>
  <widget class="QMenuBar" name="menuBar">
   <property name="geometry">
    <rect>
     <x>0</x>
     <y>0</y>
     <width>1269</width>
     <height>21</height>
    </rect>
   </property>
  </widget>
  <widget class="QToolBar" name="mainToolBar">
   <attribute name="toolBarArea">
    <enum>TopToolBarArea</enum>
   </attribute>
   <attribute name="toolBarBreak">
    <bool>false</bool>
   </attribute>
  </widget>
  <widget class="QStatusBar" name="statusBar"/>
 </widget>
 <layoutdefault spacing="6" margin="11"/>
 <resources/>
 <connections/>
</ui>

1 个答案:

答案 0 :(得分:0)

setWidget()函数需要一个继承自QWidget的对象的指针作为参数。我建议你创建一个指针。

您必须更改为:

QCustomPlot *customPlot = new QCustomPlot;
for(int i = 0; i < 10; i++)
{
    customPlot->plotLayout()->insertRow(i);
    QCPAxisRect *ar = new QCPAxisRect(customPlot);
    customPlot->plotLayout()->addElement(i, 0, ar);
}
ui->plotArea_SA->setWidget(customPlot);