qcustomplot的事件过滤器



我在注册scrollarea的起动器中很难带有qcustomplot的小部件。我尝试安装事件过滤器,但似乎它在其他滚动区域起作用,除了自己的滚动区域。关于在qcustomplot/scrollarea中修复此/注册或过滤事件的任何建议吗?附件是我的CPP和UI ...

#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "qcustomplot.h"
#include <QWidget>
MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    QWidget plotWidget;
    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);
        customPlot->plotLayout()->addElement(i, 0, ar);
    }
    customPlot->setMinimumWidth(1800);
    customPlot->installEventFilter(this);
    this->setWindowState(Qt::WindowMaximized);
    ui->plotArea_SA->setWidget(customPlot);
    qInfo() << ui->plotArea_SA->verticalScrollBar()->value();
    ui->plotArea_SA->widget()->installEventFilter(this);
    ui->plotArea_SA->viewport()->installEventFilter(this);
}
MainWindow::~MainWindow()
{
    delete ui;
}
bool MainWindow::eventFilter(QObject* /*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();

}

和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>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="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>

近6岁的问题,但是我遇到了同样的问题...:(

我能够通过禁用WA_NoMousePropagation属性来解决问题:

customPlot->setAttribute(Qt::WA_NoMousePropagation, false);

相关内容

  • 没有找到相关文章

最新更新