zhuzichu520 / FluentUI

FluentUI for QML
MIT License
3.48k stars 455 forks source link

仿照example的项目,FluNavigationView显示不了条目 #338

Closed skyw8 closed 10 months ago

skyw8 commented 10 months ago

应该是是import有问题,花了很长时间但不知道如何解决 项目地址 使用Qt6.60 MSVC2019 AdminWindow.qml

import QtQuick
import QtQuick.Window
import QtQuick.Controls
import QtQuick.Layouts
import Qt.labs.platform
import FluentUI
//下面两行竟然对运行不产生影响
//import "qrc:/meeting_room_rsv/qml/global"
//import "qrc:/meeting_room_rsv/qml/viewmodel"
import meeting_room_rsv
FluWindow {

    id:window
    title: "会议室预约系统"
    width: 1000
    height: 640
    minimumWidth: 520
    minimumHeight: 200
    launchMode: FluWindowType.SingleTask
    appBar: undefined

    ...

    FluNavigationView{
        id:nav_view
        width: parent.width
        height: parent.height
        z:999
        //Stack模式,每次切换都会将页面压入栈中,随着栈的页面增多,消耗的内存也越多,内存消耗多就会卡顿,这时候就需要按返回将页面pop掉,释放内存。该模式可以配合FluPage中的launchMode属性,设置页面的启动模式
        //                pageMode: FluNavigationViewType.Stack
        //NoStack模式,每次切换都会销毁之前的页面然后创建一个新的页面,只需消耗少量内存,可以配合FluViewModel保存页面数据(推荐)
        pageMode: FluNavigationViewType.NoStack
        items: ItemsAdmin
        footerItems: ItemsFooter
        topPadding: FluTools.isMacos() ? 20 : 0
        displayMode: viewmodel_settings.displayMode
        logo: "qrc:meeting_room_rsv/res/rsv.png"
        title: "会议室预约系统"
        Component.onCompleted: {
            ItemsAdmin.navigationView = nav_view
            ItemsAdmin.paneItemMenu = nav_item_right_menu
            ItemsFooter.navigationView = nav_view
            ItemsFooter.paneItemMenu = nav_item_right_menu
            setCurrentIndex(0)
            console.log(footerItems)
        }
    }

ItemsFooter.qml

pragma Singleton

import QtQuick
import FluentUI

FluObject{

    property var navigationView
    property var paneItemMenu

    id:footer_items

    FluPaneItemSeparator{}

    FluPaneItem{
        title:"设置"
        menuDelegate: paneItemMenu
        icon:FluentIcons.Settings
        url:"qrc:meeting_room_rsv/qml/page/SettingsPage.qml"
        onTap:{
            navigationView.push(url)
        }
        Component.onCompleted: {
            console.log("settings page")
        }
    }

}

运行后打印不了settings page,

        items: ItemsAdmin
        footerItems: ItemsFooter

赋值有问题,footerItems是null

下面是自己仿照写的cmake

cmake_minimum_required(VERSION 3.16)

project(meeting_room_rsv VERSION 0.1 LANGUAGES CXX)

set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(FLUENTUI_BUILD_EXAMPLES OFF)
set(FLUENTUI_BUILD_FRAMELESSHELPER OFF)
find_package(Qt6  REQUIRED COMPONENTS Quick)

qt_standard_project_setup()

qt_add_executable(meeting_room_rsv
    src/main.cpp
)

set(source_files
    src/main.cpp
    src/SettingsHelper.cpp
    src/SettingsHelper.h
    src/singleton.h
    src/AppInfo.cpp
    src/AppInfo.h
    src/stdafx.h
    src/Version.h
    src/CircularReveal.cpp
    src/CircularReveal.h
    src/FileWatcher.cpp
    src/FileWatcher.h
    src/FpsItem.cpp
    src/FpsItem.h
)
# 遍历QML文件
file(GLOB_RECURSE qml_paths "qml/*.qml")
foreach(qml_path ${qml_paths})
    string(REPLACE "${CMAKE_CURRENT_SOURCE_DIR}/" "" rel_qml_path ${qml_path})
#    get_filename_component(file_name ${qml_path} NAME)
    set_source_files_properties(${qml_path} PROPERTIES QT_RESOURCE_ALIAS ${rel_qml_path})
endforeach(qml_path)

file(GLOB_RECURSE res_paths "res/*.png" "res/*.jpg" "res/*.svg" "res/*.ico" "res/*.ttf" "res/*.webp" "res/*.obj")
foreach(res_path ${res_paths})
    string(REPLACE "${CMAKE_CURRENT_SOURCE_DIR}/" "" rel_res_path ${res_path})
    set_source_files_properties(${res_path} PROPERTIES QT_RESOURCE_ALIAS ${rel_res_path})
endforeach(res_path)

qt_add_qml_module(meeting_room_rsv
    URI meeting_room_rsv
    VERSION 1.0
    RESOURCE_PREFIX "/"
    SOURCES ${source_files}
    QML_FILES ${qml_paths}
    RESOURCES ${res_paths}
)

# Qt for iOS sets MACOSX_BUNDLE_GUI_IDENTIFIER automatically since Qt 6.1.
# If you are developing for iOS or macOS you should consider setting an
# explicit, fixed bundle identifier manually though.
set_target_properties(meeting_room_rsv PROPERTIES
    MACOSX_BUNDLE_GUI_IDENTIFIER my.meeting_room_rsv.com
    MACOSX_BUNDLE_BUNDLE_VERSION ${PROJECT_VERSION}
    MACOSX_BUNDLE_SHORT_VERSION_STRING ${PROJECT_VERSION_MAJOR}.${PROJECT_VERSION_MINOR}
    MACOSX_BUNDLE TRUE
    WIN32_EXECUTABLE TRUE
)

target_link_libraries(meeting_room_rsv
    PRIVATE Qt6::Quick
    fluentuiplugin
)

include(GNUInstallDirs)
install(TARGETS meeting_room_rsv
    BUNDLE DESTINATION .
    LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
    RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
)

add_subdirectory(FluentUI)
zhuzichu520 commented 10 months ago

你的ItemsAdmin.qml不是单例。添加一个qmldir文件。https://github.com/zhuzichu520/FluentUI/blob/dfb80e70eefe81091f76d2b4e137e41c3af8d782/example/qml/global/qmldir#L1

zhuzichu520 commented 10 months ago

如果你不使用单例的话,可以将ItemsAdmin.qml代码放到AdminWindow.qml中,然后用id添加到FluNavigationView的items上

skyw8 commented 10 months ago

qmldir

singleton ItemsAdmin 1.0 ItemsAdmin.qml
singleton ItemsUsr 1.0 ItemsUsr.qml
singleton ItemsFooter 1.0 ItemsFooter.qml

添加了之后似乎不管用 经过反复测试 , 我主要发现这个问题

import "qrc:/meeting_room_rsv/qml/global"
import "qrc:/meeting_room_rsv/qml/viewmodel"

我注释掉这两行运行没有任何提示报错

但是在example中注释掉 import "qrc:/example/qml/global" 会报错

qrc:/example/qml/window/MainWindow.qml:221: ReferenceError: ItemsOriginal is not defined
qrc:/example/qml/window/MainWindow.qml:180: ReferenceError: Lang is not defined
qrc:/example/qml/window/MainWindow.qml:197: ReferenceError: ItemsFooter is not defined
qrc:/example/qml/window/MainWindow.qml:196: ReferenceError: ItemsOriginal is not defined

注释掉 import "qrc:///example/qml/viewmodel" 会报错

QList(qrc:/example/qml/window/MainWindow.qml:23:5: SettingsViewModel is not a type
        SettingsViewModel{
        ^)