2011-05-07 32 views
2

我想創建一個使用QML的簡單窗口,它具有2個控件,一個TextEdit和TextInput。我試圖讓TextInput(單個)錨定到父窗口的底部,而TextEdit(多行)是TextInput上方的可調整大小的控件。單行textInput可以調整大小以適合父級的寬度,但多行TextEdit可以調整大小以填充TextInput上方的其餘屏幕。我無法在QML中創建一個可調整大小的TextEdit控件

這是我到目前爲止有:

import QtQuick 1.0 

Item { 
    id: container 
    width: 500 
    height: 400 
    TextEdit { 
     color: "red" 
     id:outputWindow 
     anchors.top: parent.bottom 
     anchors.left: parent.left 
     anchors.right: parent.right 
     anchors.bottom: inputWindow.bottom 
     wrapMode: Text.Wrap 
     text: "Welcome" 
    } 

    TextInput { 
     color:"blue" 
     id:inputWindow 
     anchors.left: parent.left 
     anchors.right: parent.right 
     //anchors.top: outputWindow.bottom 
     anchors.bottom: parent.bottom 
     text: "Input here" 
     focus:true 
    } 
} 

我想inputWindow(第二控制),可固定在底部(和左/右)的母公司,而outputWindow(第1控制)錨定在父母的頂部/左側/右側。當父級垂直調整大小時,outputWindow垂直調整大小以填充可用空間。這不會發生使用上面的代碼,我得到的是inputWindow卡在outputWindow的底部並隨之移動。

我能夠使用QT UI文件輕鬆完成此操作,但是在廣泛搜索關於如何使用QML執行此操作的任何信息之後,我必須在此處詢問。任何幫助,將不勝感激。謝謝。

回答

2

你有一些小錯誤,在outputWindow定義

TextEdit { 
    color: "red" 
    id:outputWindow 
    anchors.top: parent.top // anchor to top of parent 
    anchors.left: parent.left 
    anchors.right: parent.right 
    anchors.bottom: inputWindow.top // anchor bottom to top of input window 
    wrapMode: Text.Wrap 
    text: "Welcome" 
} 

這樣outputWindow開始在其容器的頂部和inputWindow結束。

+0

謝謝。這是我的代碼中的錯誤。但它尚未解決問題。 – Anita 2011-05-10 01:23:31

+0

那麼也許我不明白你的問題。將outputWindow組件添加到上面的代碼中讓outputWindow填充inputWindow之上的所有空間。 – blakharaz 2011-05-12 23:45:13

1

使用列儘可能多行。我發現它們在佈置各種UI元素時最爲有效。 另外,將項目錨定到同一級別上的其他項目並不總是一直工作,我認爲,我認爲它是錨定到父級的最佳實踐。

+0

按照文檔,您可以在同一父母下面同輩。 – Anita 2011-05-10 01:22:18

1
TextEdit { 
     color: "red" 
     id:outputWindow 

     anchors.top: parent.top 
     anchors.left: parent.left 
     anchors.right: parent.right 

     height : parent.height - inputWindow.height //replace bottom anchor with this 

     wrapMode: Text.Wrap 
     text: "Welcome" 
    } 
2

只要使用正確的anchors,並使用wrapModeclip

import QtQuick 1.1 

Item { 
    id: container; 
    width: 500; 
    height: 400; 

    TextEdit { 
     color: "red"; 
     id:outputWindow; 
     anchors.top: parent.top; 
     anchors.left: parent.left; 
     anchors.right: parent.right; 
     anchors.bottom: inputWindow.top; 
     wrapMode: Text.WrapAtWordBoundaryOrAnywhere; 
     text: new Array(100).join("Welcome\n"); // dumb content to show layout 
     clip: true; 
    } 
    TextInput { 
     id: inputWindow; 
     color:"blue" 
     anchors.left: parent.left 
     anchors.right: parent.right 
     anchors.bottom: parent.bottom 
     text: "Input here"; 
     focus: true; 
    } 
} 
相關問題