加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
TreeWidgetEventFilter.cpp 1.65 KB
一键复制 编辑 原始数据 按行查看 历史
箜栢 提交于 2020-03-02 09:14 . 第一次提交
#include "TreeWidgetEventFilter.h"
#include <QTreeWidgetItem>
#include <qevent.h>
#include <qdebug.h>
#include <QHeaderView>
#include <QMainWindow>
bool TreeWidgetEventFilter::eventFilter(QObject* watched, QEvent* event) {
QTreeWidget* widget = qobject_cast<QTreeWidget*>(watched);
static QPoint oldPoint;
static QPoint newPoint;
QEvent::Type eventType = event->type( );
if( eventType == QEvent::MouseButtonPress ) {
newPoint = QCursor::pos( );
qDebug( ) << newPoint;
}
if( eventType == QEvent::MouseButtonRelease ) {
oldPoint = newPoint;
}
if( eventType == QEvent::MouseMove && oldPoint != newPoint ) {
qDebug( ) << "产生了移动";
if( horScrollBar->isEnabled( ) ) {
if( oldPoint.x( ) > newPoint.x( ) ) {
int horScrollBarValue = horScrollBar->value( );
int newValue = horScrollBarValue + 2;
int maximum = horScrollBar->maximum( );
if( newValue >= maximum ) {
horScrollBar->setValue(newValue);
} else {
horScrollBar->setValue(maximum);
}
}
if( oldPoint.x( ) < newPoint.x( ) ) {
int horScrollBarValue = horScrollBar->value( );
int newValue = horScrollBarValue - 2;
int minimum = horScrollBar->minimum( );
if( newValue >= minimum ) {
horScrollBar->setValue(newValue);
} else {
horScrollBar->setValue(minimum);
}
}
}
oldPoint = newPoint;
}
return QObject::eventFilter(watched, event);
}
TreeWidgetEventFilter::TreeWidgetEventFilter(QTreeWidget* parent): QObject(parent) {
horScrollBar = parent->header( )->horizontalScrollBar( );
verScrollBar = parent->header( )->verticalScrollBar( );
parent->installEventFilter(this);
}
TreeWidgetEventFilter::~TreeWidgetEventFilter( ) {}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化