我在为其小部件使用 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();
}
和用户界面
<?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>