关于 qt qml 报错 QtQuick.Controls 2 没有被注册

qml 报错无法加载 QtQuick.Controls

背景

一个简单的 qtcreater demo,qml 文件如下

import QtQuick 2.15
import QtQuick.Window 2.15


Window {
    visible: true
    width: 640
    height: 480
    title: qsTr("Hello World")

    // 创建一个红色的矩形
    Rectangle {
        id: redRect
        width: 100
        height: 100
        color: "red"
        anchors.centerIn: parent
    }

    // 创建一个按钮
    Button {
        text: "Click Me"
        anchors.top: redRect.bottom
        anchors.horizontalCenter: parent.horizontalCenter
        onClicked: {
            console.log("Button clicked!")
        }
    }
}

问题1

问题描述: Button is not a type

解决方案:

添加导入
import QtQuick.Controls 2.15

问题2

问题描述:编译后运行报错:

qrc:/main.qml:3:1: plugin cannot be loaded for module "QtQuick.Controls": Cannot protect module QtQuick.Controls 2 as it was never registered

解决方案:

未下载该模块
sudo apt install qml-module-qtquick-controls2

posted @ 2024-12-31 15:41  王清河  阅读(472)  评论(0)    收藏  举报