Search code examples
qmlqtquickcontrolsqtquickcontrols2

ApplicationWindow with Toolbar cannot hold SwipeView because of import errors


I am using Qt Opensource 5.10.0 with Qt Creator 4.5.0 based on ArchLinux 64bit. and the docs for ApplicationWindow states it requires QtQuick.Controls 1.4:

import QtQuick 2.10
import QtQuick.Controls 1.4

ApplicationWindow
{
    width: 640
    height: 480

    visible: true

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

which compiles and runs ok. Now, I want to add SwipeView to upper ApplicationWindow, however QtQuickControls 1.4 does not recognize it, as it also states in SwipeView's docs and requires import QtQuick.Controls 2.3, so if I let import QtQuick.Controls 1.4 in main.qml:

import QtQuick 2.10
import QtQuick.Controls 1.4

ApplicationWindow
{
    width: 640
    height: 480

    visible: true

    toolBar: ToolBar
    {
    }   // ToolBar

    SwipeView
    {
    }   // SwipeView
}   // ApplicationWindow

I get error:

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

and if use 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

I get following error:

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

Now, if I include both imports:

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

I still get:

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

as in second case. The first error is logical, since in version 1.4 there was not SwipeView, however, why QtQuick.Controls 2.3 does not recognize ApplicationWindow's member/property ApplicationWindow.toolbar in second case?


Solution

  • Ok, this duality arose from the fact that there are 2 ApplicationWindow, one came from import QtQuick.Controls 1.4 and the second one come from import QtQuick.Controls 2.3. The new one have no toolBar so you get the error.

    If you still want to use the old one you can use aliasing as following:

    import QtQuick.Controls 1.4 as Old
    
    Old.ApplicationWindow {    
        toolBar: ToolBar
        {
        }
    }
    

    or you should use ApplicationWindow.header instead in the new one:

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

    I don't know why Qt changed the name from toolBar to header. For me it looks illogical.