2017-02-25 79 views
0

由于初始发帖不足导致编辑。多项短划线

嗨,

感谢您的帮助!

你说得对,我想最好是包括整个文件,尽管大小:

import QtQuick 2.5 
import QtQuick.LocalStorage 2.0 
import QtQuick.Dialogs 1.2 
import QtQuick.Controls 2.1 
import QtQuick.Controls.Styles 1.4 
import QtQuick.Window 2.0 
import QtQuick.Controls.Material 2.1 
import "./database.js" as Database 

ApplicationWindow { 
    visible: true 
    width: 640 
    height: 480 
    id: appWindow 
    x: Screen.width/2 - width/2 
    y: Screen.height/2 - height/2 
    title: qsTr("Project Stats") 
    Material.theme: Material.Dark 

    ListModel { 
     id: projectModel 
     ListElement { 
      projectID: "123654" 
      manager: "Schneider" 
      sponsor: "3466" 
     } 
    } 

    Component { 
     id: projectDelegate 
     SwipeDelegate { 
      id: projectSwipeDelegate 
      width: parent.width 
      height: projectDelegateItem.implicitHeight 
      anchors.horizontalCenter: parent.horizontalCenter 
      spacing: 10 

      contentItem: Item { 
       id: projectDelegateItem 
       Text { 
        id: projectID_text 
        text: "Project ID: " + projectID 
        font.pointSize: 20 
        anchors.horizontalCenter: parent.horizontalCenter 
        font.weight: Font.Black 
        color: "white" 
       } 

       Text { 
        id: manager_text 
        text: 'Manager: ' + manager + " Sponsor: " + sponsor 
        anchors.top: projectID_text.bottom 
        anchors.horizontalCenter: parent.horizontalCenter 
        font.weight: Font.Thin 
        color: "lightgrey" 
       } 
      } 


      onClicked: { 
       console.log(index, projectModel.get(index).projectID) 
       if (swipe.complete) 
        projectModel.remove(index) 
       else { 
        //var component= Qt.createComponent("timepointsstackview.qml") 
        //var loadwin = component.createObject(appWindow) 
        //loadwin.selected_project = projectModel.get(index).projectID 
        //     stackView.push(Qt.resolvedUrl("timepointsstackview.qml"), {properties: {selected_project: projectModel.get(index).projectID}}) 
        stackView.push(component, {properties: {selected_project: projectModel.get(index).projectID}}) 
       } 
      } 
      swipe.right: Label { 
       id: deleteLabel 
       text: qsTr("Delete") 
       color: "white" 
       verticalAlignment: Label.AlignVCenter 
       padding: 12 
       height: parent.height 
       anchors.right: parent.right 

       SwipeDelegate.onClicked: projectListView.model.remove(index) 

       background: Rectangle { 
        color: deleteLabel.SwipeDelegate.pressed ? Qt.darker("tomato", 1.1) : "tomato" 
       } 
      } 
     } 
    } 

    Item { 
     Component.onCompleted: { 
      Database.getDatabase() 
      Database.getProjects() 
     } 
    } 

    StackView { 
     id: stackView 
     anchors.fill: parent 
     // Implements back key navigation 
     focus: true 
     Keys.onReleased: if (event.key === Qt.Key_Back && stackView.depth > 1) { 
          stackView.pop(); 
          event.accepted = true; 
         } 
     initialItem: Item { 
      width: parent.width 
      height: parent.height 

      ListView { 
       id: projectListView 
       anchors.fill: parent 
       clip: true 

       model: projectModel 
       delegate: projectDelegate 
      } 
     } 

    } 

    onClosing: { 
     if (Qt.platform.os == "android") { 
      close.accepted = false; 
      //  if (stack.depth > 1) stack.pop(); 
     } 
    } 
} 

同时我已经删除了该行/列的东西,我把在得到它以某种方式工作,虽然我没有它开始。

我也在实际发布前试验过隐式高度,但可惜无济于事。以上是我当前的代码,但在现场投入

height: projectDelegateItem.implicitHeight 

(probabaly不正确的或错误的参考?只好把它从你的建议改变,因为我已经拿出了行)导致渲染只有一个点。

感谢您的时间,到目前为止,也如果你仍然有耐心给我一个线索在哪里拐弯螺丝...

+0

你好,欢迎来到StackOverflow。我不相信你,这是“你得到的代码”。你可能还有更多,就像我们需要测试你的代码所需要的'ListView'和'ListModel'。对于未来的问题,请不要忘记这些。阅读[this](http://stackoverflow.com/help/mcve)关于如何呈现一个好例子,我们可能会合作。如果他们能够专注于自己的问题,而其他人则更有帮助,并且不要费尽心思先复制问题。尽管如此,我希望我的回答能帮助你解决和解决你的问题。如果是这样,我会很感激,如果你将它标记为*接受* ;-) – derM

+0

现在你需要给你的文本(例如'text1'和'text2')提供'id's。然后你可以计算高度为height:text1.implicitHeight + text2.implicitHeight。问候:-) – derM

回答

0

好吧,首先: 采取严肃的警告。如果qml告诉你,你不应该尝试在行或列中使用锚点,不要这样做!

QML Row: Cannot specify left, right, horizontalCenter, fill or centerIn anchors for items inside Row. Row will not function.

QML Column: Cannot specify top, bottom, verticalCenter, fill or centerIn anchors for items inside Column. Column will not function.

而且不这样做,如果你不能看到这些警告。它会搞砸了很多。 一行自动将所有的孩子并排固定在一起。一列也是一样,只是一丝不苟。如果你搞砸了,一切都会中断。

坦率地说:我甚至不明白你为什么使用这个奇怪的行/列设置。 对于你的情况,似乎只是诉诸锚定更好。如果你有这个理由,为什么不采取网格?

其次:你需要为你的委托指定一个高度。不幸的是,它似乎并不计算隐含的高度。 SwipeDelegate根据其contentItemimplicitHeight计算自己的implcitHeight

的问题是,你没有行(其中有一个适当的implicitHeight)指定为contentItem,但其添加为一个孩子来代替。

将其指定为contentItem会为您解决该问题。

关于你的编辑和删除的行:Item你现在使用基于其子不计算implicitHeight。所以你需要自己提供你的计算。

这将为您的委托设置适当的高度,并且您的委托不会重叠。

import QtQuick 2.0 
import QtQuick.Window 2.0 
import QtQuick.Controls 2.0 
Window { 
    width: 1024 
    height: 800 
    visible: true 

    ListView { 
     width: 400 
     height: 800 

     model: ListModel { 
      ListElement { projectID: 0; manager: 'I'; sponsor: 'mom' } 
      ListElement { projectID: 1; manager: 'YOU'; sponsor: 'dad' } 
      ListElement { projectID: 1; manager: 'HE'; sponsor: 'auntie' } 
     } 

     delegate: SwipeDelegate { 
      id: projectSwipeDelegate 
      width: parent.width 
      // height: <--- provide a height, if the contentItem does not provide it. 
      contentItem: Row{ // <--- Add your content as contentItem. 
       id: rowProjectDelegate 
       anchors.horizontalCenter: parent.horizontalCenter 
       width: parent.width 
       Column { 
        id: column 
//     anchors.horizontalCenter: parent.horizontalCenter <--- Don't do that! 
        width: parent.width 
        Rectangle{ // If you don't want to have them in a column, they can't be siblings. If you want to, then you should. 
         height: 10 
         width: 250 
         color: "red" 

         Rectangle { 
          height: 10 
          width: 200 
          color: "blue" 
         } 
        } 
        Label { 
         id: projectID_text 
         text: "Project ID: " + projectID 
         font.pointSize: 20 
         font.weight: Font.Black 
         color: "white" 
        } 
        Label { 
         id: manager_text 
         text: 'Manager: ' + manager + " Sponsor: " + sponsor 
//      anchors.top: projectID_text.bottom <--- Don't do that! 
         font.weight: Font.Thin 
         color: "lightgrey" 
        } 
       } 
      } 
     } 
    } 
} 
+0

感谢您的回答,对不起,我在编辑我的主帖之后看到了代码。这个现在可以工作,我只是试图将行和列的东西分解出来。 “Bye Joost – Joost

+1

”不幸的是,它似乎并不计算隐含的高度。“ - 我不太确定:http://code.qt.io/cgit/qt/qtquickcontrols2.git/tree/src/imports/controls/SwipeDelegate.qml#n47 – Mitch

+0

是的,你是对的。我会更新我的答案。 – derM

相关问题