QCustomPlot Discussion and Comments

QCustomPlot and QScrollArea event filterReturn to overview

Hello, i'm having trouble registering the scroll event in a QCustomPlot composed of many QCPRect. What i'm trying to do is if the mousewheel is scrolled in the scrollArea that contains the customplot, it will change the scrollbar value, but so far I have no luck doing that. Any help would be greatly appreciated. My cpp and my ui code follows

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

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    QCustomPlot* customPlot = new QCustomPlot;
    int x = ui->plotArea_SA->width();
    int y = ui->plotArea_SA->height();

    for(int i = 0; i < 100; i++)
    {
        customPlot->plotLayout()->insertRow(i);
        QCPAxisRect *ar = new QCPAxisRect(customPlot);
        ar->setMinimumSize(x, y);
        ar->installEventFilter(this);
        customPlot->plotLayout()->addElement(i, 0, ar);

    }
    customPlot->setMinimumWidth(1800);

    this->setWindowState(Qt::WindowMaximized);


    qInfo() << ui->plotArea_SA->verticalScrollBar()->value();

    ui->plotArea_SA->installEventFilter(this);
    ui->plotArea_SA->setWidget(customPlot);
}

MainWindow::~MainWindow()
{
    delete ui;
}
bool MainWindow::eventFilter(QCustomPlot* /*obj*/, QEvent* evt)
{
            if (evt->type() == QEvent::Wheel)
            {
                // ignore the event (this effectively
                // makes it "skip" one object)
                evt->ignore();
            }
            // return false to continue event propagation
            // for all events
            return false;
}

void MainWindow::wheelEvent(QWheelEvent* event)
{

    int numDegrees = event->delta() / 8;
    qInfo() << numDegrees;
    ui->plotArea_SA->verticalScrollBar()->setValue(ui->plotArea_SA->verticalScrollBar()->value() - numDegrees);
    qInfo() << "got here";
    event->accept();


}

<?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>1214</width>
    <height>638</height>
   </rect>
  </property>
  <property name="sizePolicy">
   <sizepolicy hsizetype="Maximum" vsizetype="Maximum">
    <horstretch>0</horstretch>
    <verstretch>0</verstretch>
   </sizepolicy>
  </property>
  <property name="cursor">
   <cursorShape>ArrowCursor</cursorShape>
  </property>
  <property name="windowTitle">
   <string>MainWindow</string>
  </property>
  <property name="autoFillBackground">
   <bool>false</bool>
  </property>
  <widget class="QWidget" name="centralWidget">
   <layout class="QGridLayout" name="gridLayout">
    <item row="0" column="0">
     <widget class="QScrollArea" name="searchTable_SA">
      <property name="maximumSize">
       <size>
        <width>300</width>
        <height>16777215</height>
       </size>
      </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>298</width>
         <height>279</height>
        </rect>
       </property>
       <zorder>displayTable_SA</zorder>
      </widget>
     </widget>
    </item>
    <item row="1" column="0">
     <widget class="QScrollArea" name="displayTable_SA">
      <property name="maximumSize">
       <size>
        <width>300</width>
        <height>16777215</height>
       </size>
      </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>298</width>
         <height>278</height>
        </rect>
       </property>
       <zorder>searchTable_SA</zorder>
      </widget>
     </widget>
    </item>
    <item row="0" column="1" rowspan="2">
     <widget class="QScrollArea" name="plotArea_SA">
      <property name="minimumSize">
       <size>
        <width>0</width>
        <height>90</height>
       </size>
      </property>
      <property name="focusPolicy">
       <enum>Qt::NoFocus</enum>
      </property>
      <property name="verticalScrollBarPolicy">
       <enum>Qt::ScrollBarAlwaysOn</enum>
      </property>
      <property name="horizontalScrollBarPolicy">
       <enum>Qt::ScrollBarAlwaysOff</enum>
      </property>
      <property name="sizeAdjustPolicy">
       <enum>QAbstractScrollArea::AdjustIgnored</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>1000</width>
         <height>1000</height>
        </rect>
       </property>
       <property name="sizePolicy">
        <sizepolicy hsizetype="Preferred" vsizetype="Preferred">
         <horstretch>0</horstretch>
         <verstretch>2</verstretch>
        </sizepolicy>
       </property>
       <property name="minimumSize">
        <size>
         <width>1000</width>
         <height>1000</height>
        </size>
       </property>
       <zorder>displayTable_SA</zorder>
      </widget>
     </widget>
    </item>
   </layout>
  </widget>
  <widget class="QMenuBar" name="menuBar">
   <property name="geometry">
    <rect>
     <x>0</x>
     <y>0</y>
     <width>1214</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>

so the most obvious problem is that your event filter method doesnt match the required format. it actually does have to be QObject *, not just a type derived from QObject.

secondly: event->ignore does not do what you think it does. event->ignore means that your event filter is ignoring the event(thus propagating back up. if you want to stop the event from doing stuff, you need to use event->accept.

From the docs:

void QEvent::ignore()
Clears the accept flag parameter of the event object, the equivalent of calling setAccepted(false).
Clearing the accept parameter indicates that the event receiver does not want the event. Unwanted events might be

 propagated to the parent widget
.

Thank you Ian, this helps me understand a lot. What I decided to do was subclass Qcustomplot as a QObject. From there I could easily use signal and slot to override some events for the future.

QCustomPlot is already a QObject.