由于导入错误,带工具栏的 ApplicationWindow 无法保存 SwipeView

ApplicationWindow with Toolbar cannot hold SwipeView because of import errors

我正在使用 Qt Opensource 5.10.0 with Qt Creator 4.5.0 based on ArchLinux 64bit. and the docs for ApplicationWindow 说明它需要 QtQuick.Controls 1.4:

import QtQuick 2.10
import QtQuick.Controls 1.4

ApplicationWindow
{
    width: 640
    height: 480

    visible: true

    toolBar: ToolBar
    {
    }   // ToolBar
}   // ApplicationWindow

编译运行正常。现在,我想添加 SwipeView to upper ApplicationWindow, however QtQuickControls 1.4 does not recognize it, as it also states in SwipeView's docs 并要求 import QtQuick.Controls 2.3,所以如果我让 import QtQuick.Controls 1.4main.qml 中:

import QtQuick 2.10
import QtQuick.Controls 1.4

ApplicationWindow
{
    width: 640
    height: 480

    visible: true

    toolBar: ToolBar
    {
    }   // ToolBar

    SwipeView
    {
    }   // SwipeView
}   // ApplicationWindow

我收到错误:

Starting /mnt/projects/build-test12-Desktop_Qt_5_10_0_GCC_64bit-Debug/test12...
QML debugging is enabled. Only use this in a safe environment.
QQmlApplicationEngine failed to load component
qrc:/main.qml:15 SwipeView is not a type

/mnt/projects/build-test12-Desktop_Qt_5_10_0_GCC_64bit-Debug/test12 exited with code 255

如果使用 import QtQuick.Controls 2.3:

import QtQuick 2.10
import QtQuick.Controls 2.3

ApplicationWindow
{
    width: 640
    height: 480

    visible: true

    toolBar: ToolBar
    {
    }   // ToolBar

    SwipeView
    {
    }   // SwipeView
}   // ApplicationWindow

我收到以下错误:

Starting /mnt/projects/build-test12-Desktop_Qt_5_10_0_GCC_64bit-Debug/test12...
QML debugging is enabled. Only use this in a safe environment.
QQmlApplicationEngine failed to load component
qrc:/main.qml:11 Cannot assign to non-existent property "toolBar"

/mnt/projects/build-test12-Desktop_Qt_5_10_0_GCC_64bit-Debug/test12 exited with code 255

现在,如果我包括两个进口:

import QtQuick 2.10
import QtQuick.Controls 1.4
import QtQuick.Controls 2.3

ApplicationWindow
{
    width: 640
    height: 480

    visible: true

    toolBar: ToolBar
    {
    }   // ToolBar

    SwipeView
    {
    }   // SwipeView
}   // ApplicationWindow

我仍然得到:

Starting /mnt/projects/build-test12-Desktop_Qt_5_10_0_GCC_64bit-Debug/test12...
QML debugging is enabled. Only use this in a safe environment.
QQmlApplicationEngine failed to load component
qrc:/main.qml:12 Cannot assign to non-existent property "toolBar"

/mnt/projects/build-test12-Desktop_Qt_5_10_0_GCC_64bit-Debug/test12 exited with code 255

如第二种情况。 第一个错误是合乎逻辑的,因为在版本 1.4 中没有 SwipeView,然而,为什么 QtQuick.Controls 2.3 无法识别 ApplicationWindow 的 member/property ApplicationWindow.toolbar 第二种情况?

好吧,这种二元性是因为有 2 个 ApplicationWindow,一个来自 import QtQuick.Controls 1.4,第二个来自 import QtQuick.Controls 2.3。新的没有 toolBar 所以你得到错误。

如果您仍想使用旧版本,可以按如下方式使用别名:

import QtQuick.Controls 1.4 as Old

Old.ApplicationWindow {    
    toolBar: ToolBar
    {
    }
}

或者您应该在新版本中使用 ApplicationWindow.header

ApplicationWindow {
    header: TabBar {
        // ...
    }
}

不知道Qt为什么要把名字从toolBar改成header。对我来说这看起来不合逻辑。