【问题标题】:qml Stackview: pop a page which is in another fileqml Stackview:弹出另一个文件中的页面
【发布时间】:2020-06-16 14:52:50
【问题描述】:

我的问题是我无法从堆栈中弹出()页面。 让我稍微解释一下:

我有一个页面显示了几个项目 每个项目都有一个按钮,可以在 stackview 顶部打开一个新页面,我可以在其中编辑我的项目。 这个 EditPage 有一个关闭按钮 - 它应该:是的,你明白了:关闭页面 xD

表示我要从堆栈中弹出页面

MainView.qml

// ...

StackView {
    id: _myStack
    initialItem: _myView

    GridView {
      id: _myView       
      model: // comes from c++
      delegate: MyItem{}
    }
}

MyItem.qml

Item{
  id: _myItem

  Button{
    id: _buttonEdit
    text: "Edit"
    onClicked{
      _stackview.push("../components/Edit.qml, {some properties I pass through}, MainView.Immediate)   
       // Edit.qml is in another folder, import is done
    }
  }
}

Edit.qml

Page{
  id: _editPage

  // some code

  Button {
    id: _closeButton

    onClicked{
      //
      // here I want to pop this window out of the stack (it is the top item on the stack)
      // _myStack.pop() doesn't work - because _myStack is not visible
    }
}

我能做什么?我已经在 Edit.qml 中尝试过这个:我卡在这一点上 - 在 MyItem.qml 或哪里绑定信号?

Page{
  id: _editPage

  signal closeEditPage

  Button {
    id: _closeButton

    onClicked{
       closeEditPage()
    }
}

【问题讨论】:

  • 您有StackView.view 访问视图控件。但这些问题通常表明应用程序架构中存在错误。 QML 是声明性语言,但您尝试使用命令式风格。
  • 这大概就是我经常走入死胡同的原因吧。除此之外,我无法访问 _myStack.view。所以我的“中间”页面必须以某种方式从 MainView.qml 获取视图或从 editPage 获取信号,对吗?我必须像链子一样传递信息?

标签: qt qml stackview


【解决方案1】:

重新绑定附加属性StackView.view 以便于访问。

Page{
  id: _editPage

  property StackView _myStack: StackView.view

  Button {
    id: _closeButton

    onClicked{
       _myStack.pop()
    }
}

【讨论】:

  • 最后我采用了你的方法 eri,而不是通过 closePage 信号。谢谢
猜你喜欢
  • 2014-04-05
  • 2018-06-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-02-09
  • 2018-12-06
相关资源
最近更新 更多