You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

qmlscene中鼠标滚轮滚动速度更快,如何让Qt Quick应用同步此速度?

Qt Quick应用鼠标滚轮滚动速度慢的解决方法

环境与问题

  • 环境:Qt 6.6.0,Linux Manjaro KDE
  • 问题:自行编译的Qt Quick应用中,ListView的鼠标滚轮滚动速度极慢;但使用qmlscene main.qml直接运行QML文件时,滚动速度正常。需求是让编译后的应用滚轮速度与qmlscene保持一致。

相关代码如下:

main.qml

import QtQuick 2.15
import QtQuick.Controls 2.15

ApplicationWindow {
    visible: true
    width: 400
    height: 400
    title: "List"

    ListView {
        width: parent.width
        height: parent.height

        model: 50

        delegate: Item {
            width: if (parent) parent.width
            height: 50

            Rectangle {
                width: parent.width
                height: 50

                Text {
                    anchors.centerIn: parent
                    text: "Text"
                }
            }
        }
    }
}

main.cpp

#include <QGuiApplication>
#include <QQmlApplicationEngine>
#include <QQmlContext>

int main(int argc, char *argv[])
{
    QGuiApplication app(argc, argv);
    QQmlApplicationEngine engine;

    const QUrl url(QStringLiteral("qrc:/qt/qml/main/main.qml"));
    QObject::connect(&engine, &QQmlApplicationEngine::objectCreated,
                     &app, [url](QObject *obj, const QUrl &objUrl) {
        if (!obj && url == objUrl)
            QCoreApplication::exit(-1);
    }, Qt::QueuedConnection);
    engine.load(url);

    return app.exec();
}

CMakeLists.txt

cmake_minimum_required(VERSION 3.16)

project(qml-list LANGUAGES CXX)

find_package(Qt6 6.2 COMPONENTS Quick Gui REQUIRED)

qt_standard_project_setup(REQUIRES 6.5)

find_package(Qt6 6.2 COMPONENTS Quick Gui REQUIRED)

qt_add_executable(${PROJECT_NAME} main.cpp)
target_link_libraries(${PROJECT_NAME} PRIVATE Qt6::Gui Qt6::Quick)

qt_add_qml_module(${PROJECT_NAME}
    URI main
    QML_FILES
        main.qml
)

解决方法

方法一:自定义ListView滚轮事件处理

直接在QML中给ListView添加鼠标滚轮事件处理,手动调整滚动增量,匹配qmlscene的速度:

修改后的main.qml:

import QtQuick 2.15
import QtQuick.Controls 2.15

ApplicationWindow {
    visible: true
    width: 400
    height: 400
    title: "List"

    ListView {
        id: listView
        width: parent.width
        height: parent.height

        model: 50

        delegate: Item {
            width: parent.width
            height: 50

            Rectangle {
                width: parent.width
                height: 50

                Text {
                    anchors.centerIn: parent
                    text: "Text"
                }
            }
        }

        // 自定义滚轮处理,可调整系数0.4来适配速度
        MouseArea {
            anchors.fill: parent
            acceptedButtons: Qt.NoButton // 不拦截鼠标按键操作
            onWheel: {
                listView.contentY += wheel.delta * 0.4;
            }
        }
    }
}

方法二:使用ScrollView包裹ListView

ScrollView默认适配系统的输入设置(包括滚轮速度),替换直接使用ListView的方式:

修改后的main.qml:

import QtQuick 2.15
import QtQuick.Controls 2.15

ApplicationWindow {
    visible: true
    width: 400
    height: 400
    title: "List"

    ScrollView {
        width: parent.width
        height: parent.height

        ListView {
            width: parent.width
            model: 50

            delegate: Item {
                width: parent.width
                height: 50

                Rectangle {
                    width: parent.width
                    height: 50

                    Text {
                        anchors.centerIn: parent
                        text: "Text"
                    }
                }
            }
        }
    }
}

方法三:启用系统风格适配

在C++代码中设置Qt Quick Controls使用系统风格,让应用遵循系统的滚轮配置:

修改后的main.cpp:

#include <QGuiApplication>
#include <QQmlApplicationEngine>
#include <QQmlContext>
#include <QQuickStyle>

int main(int argc, char *argv[])
{
    QGuiApplication app(argc, argv);
    // 设置为KDE系统风格,自动适配系统滚轮设置
    QQuickStyle::setStyle("org.kde.desktop");

    QQmlApplicationEngine engine;

    const QUrl url(QStringLiteral("qrc:/qt/qml/main/main.qml"));
    QObject::connect(&engine, &QQmlApplicationEngine::objectCreated,
                     &app, [url](QObject *obj, const QUrl &objUrl) {
        if (!obj && url == objUrl)
            QCoreApplication::exit(-1);
    }, Qt::QueuedConnection);
    engine.load(url);

    return app.exec();
}

原因说明

qmlscene作为Qt官方的QML预览工具,默认启用了适配系统的输入处理逻辑,包括对滚轮速度的优化;而自行编译的应用默认使用基础控件配置,未自动适配系统滚轮设置,因此会出现速度差异。上述三种方法均可让应用的滚轮速度与qmlscene保持一致。

内容的提问来源于stack exchange,提问作者Velkan

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.04 17:29:58