2017-02-25 91 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

相關問題