首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >qml如何在tabview中使用加载程序

qml如何在tabview中使用加载程序
EN

Stack Overflow用户
提问于 2014-02-06 07:27:53
回答 1查看 3.8K关注 0票数 5

我找不到一种方法来使用加载程序填充一个选项卡在一个表视图中。加载程序在TabView之外时工作正常(也就是说,如果我移除mainTrial.qml顶部的多行注释字符,而加载器加上连接位于顶部的话)。但是,如果我将装载机按选项卡子列的形式移动,就会得到一个错误,“无法为单个属性分配多个值。我也不能使用menuLoader.source或column1.menuLoader.source或其他变体来定位位于menuLoader.source或column1.menuLoader.source之外的装载机。这是主文件。(还包括了定义信号的另外两个文件,以便您可以看到信号正常工作)。我做错了什么?

编辑:(排列和组合的好的ole法则)我发现,如果我做了连接声明,在标签上的装载机的一个子程序,问题就消失了。我将在选项卡上分配一个“值”--即Loader,现在我可以在每个选项卡上加载一个qml项。

//主审

代码语言:javascript
运行
复制
import QtQuick 2.2
import QtQuick.Controls 1.1
import QtQuick.Layouts 1.1 


Item {
    id: trial
    width: 360
    height: 360
    ColumnLayout {
        id: column1
        spacing:150
        Item {Layout.fillHeight: true}
        TabView {
            id: tabPages
            Tab {
                id: welcomePage
                title: "Welcome"
                // /*
                Loader{
                    id: menuLoader
                    source: "mainTrial1.qml"
                    anchors.top: parent.top
                    Connections {
                        id: menuConnection
                        ignoreUnknownSignals: true
                        target: menuLoader.item
                        onMainTrial3: {
                            menuLoader.source = "mainTrial3.qml"
                            console.log("originated from " + origin)
                        }
                        onMainTrial1: {
                            menuLoader.source = "mainTrial1.qml"
                            console.log("originated from " + origin)
                        }
                    }
                }        
            }
            Tab {
                id: statusPage
                title: "Status"
            }
        }
    }
}

// mainTrial1 (有一个信号定义)

该文件定义了一个用于加载下一个qml对象的信号。我把它放在这里,这样你就可以看到信号起作用了)。

代码语言:javascript
运行
复制
import QtQuick 2.2
import QtQuick.Controls 1.1
import QtQuick.Layouts 1.1

Item {
    id: page1
    property string origin: "Hello"
    signal mainTrial3(string origin)
    ColumnLayout {
        spacing: 15
        Rectangle {
            width: 100
            height: 62
            color: "red"

            MouseArea {
                anchors.fill: parent
                onClicked: {
                    mainTrial3("origin = from MouseArea")
                }
            }
        }

        Button {
            text: "Sorting and scanning"
            onClicked: {
                mainTrial3(origin = "from Button")
            }
        }
    }
}

//mainTrial3 3(与其他信号定义一起)

该文件定义了用于重新加载前一个qml对象的其他信号。我把它放在这里,这样你就可以看到,当你点击矩形时,这两个信号都能工作。

代码语言:javascript
运行
复制
import QtQuick 2.2
import QtQuick.Controls 1.1
import QtQuick.Layouts 1.1

Item {
    id: page3
    property string origin: "Hello"
    signal mainTrial1(string origin)
    GridLayout {
        columnSpacing: 5
        rowSpacing: 5
        columns: 1
        Rectangle {
            width: 100
            height: 62
            color: "blue"

            MouseArea{
                anchors.fill: parent
                onClicked: {
                    mainTrial1(origin = "from MouseArea1")
                }
            }
        }
        Rectangle {
            width: 100
            height: 62
            color: "blue"

            MouseArea{
                anchors.fill: parent
                onClicked: {
                    mainTrial1("from MouseArea2")
                }
            }
        }
    }
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-02-07 04:39:42

好的,我取了您的代码,使用了一些,然后定制了其他选项卡,以加载一个选项卡,从而使围绕它的讨论变得更容易一些。这并不完全是你发布的内容,但它应该向你展示如何:

  1. 当你的标签内有东西被点击时,接收信号。
  2. 在哪里以及如何加载标签。

有了这个,您应该有坚实的基础,以进一步定制和添加二,三,等等。选项卡,并根据我的示例中的信号动态地更改它们。

重要的是,Tab本身就是一个Loader,因此不需要定义内部Loader。接下来,我还不完全熟悉QML (更多的是QWidget),但是您正在使用的Connections似乎会导致一些问题,我不确定它们是否完全需要,所以我从我的示例中删除了它们。

在我的示例中,我只是在一个选项卡中加载一个QML文件,并在单击刚刚加载的选项卡内的蓝色矩形或红色矩形时发送一个信号。

您将能够在控制台中看到打印结果。我在信号处理程序中留下了一个注释,您可以在其中更改TabTab并动态加载下一个Tab

享受吧!

TabViewTest.qml

代码语言:javascript
运行
复制
import QtQuick 2.2
import QtQuick.Controls 1.1
import QtQuick.Layouts 1.1


Item {
    id: tabViewTest
    width: 360
    height: 360

    TabView {
        id: tabView
        width: parent.width
        height: parent.height

        Tab {
            title: "Welcome"
            id: dynamicTab
            anchors.top: parent.top

            // [1] Specify the source URL to load the content.
            source: "RectanglesTab.qml"

            // [2] Define a signal within the Tab that will
            // be connected to the 'contentsClicked' signal
            // in each tab QML file.
            signal tabContentsClicked( string rectColor )

            // [3] Implement the signal handler.
            onTabContentsClicked: {
                console.log( "Clicked on a", rectColor, "rectangle." )

                // [4] Now that the user has clicked somewhere within the tab
                // you could optionally the source or sourceComponent here like
                // the comment below.
                //
                // Just make sure it also has a signal called 'contentsClicked'
                //
                // dynamicTab.source = "someOtherTab.qml"
            }

            onLoaded: {
                console.debug( "Loaded", source );

                // [4] Here's the key action, connect the signal 
                // 'contentsClicked' to our signal 
                // 'tabContentsClicked' from the loaded item 
                // i.e. RectanglesTab.qml
                dynamicTab.item.contentsClicked.connect(tabContentsClicked)
            }
        }

        Tab {
            id: statusTab
            title: "Status"
        }
    }
}

RectanglesTab.qml

代码语言:javascript
运行
复制
import QtQuick 2.0
import QtQuick.Layouts 1.1

Rectangle {
    id: rectanglesTab
    width: parent.width
    height: parent.height
    color: "black"

    signal contentsClicked( string rectColor )

    GridLayout {
        width: parent.width
        height: parent.height
        columnSpacing: 5
        rowSpacing: 5
        columns: 1
        Rectangle {
            width: parent.width
            height: 120
            color: "blue"

            MouseArea{
                anchors.fill: parent
                onClicked: {
                    contentsClicked( "blue" )
                }
            }
        }
        Rectangle {
            width: parent.width
            height: 120
            color: "red"

            MouseArea{
                anchors.fill: parent
                onClicked: {
                    contentsClicked( "red" )
                }
            }
        }
    }
}
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/21596592

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档