November 25, 2010

doforumda doforumda
Lab Rat
161 posts

help with text in lineEdit

 

I need help in my code. What i am trying to do is simply user enter some text in lineEdit then after clicking on the button it should get the text from lineEdit and display it in display rectangle.

here is my code

  1. import Qt 4.7
  2.  
  3. Rectangle {
  4.     width: 300
  5.     height: 300
  6.     Rectangle {
  7.         id: lineEdit
  8.         width: 200
  9.         height: 50
  10.         anchors.horizontalCenter: parent.horizontalCenter
  11.         anchors.verticalCenter: parent.verticalCenter
  12.         color: "lightblue"
  13.         border.color: "green"
  14.         TextInput {
  15.             id: textField
  16.             anchors.left: parent.left; y: 16
  17.             anchors.right: parent.right
  18.             //border.color: "green"
  19.             text: "field 1"
  20.             font.pixelSize: 32
  21.             //color: focus ? "black" : "gray"
  22.         }
  23.     }
  24.     Rectangle {
  25.         id: button
  26.         radius: 5
  27.         width: lineEdit.width
  28.         height: lineEdit.height
  29.         border.color: "black"
  30.         //color: "lightblue"
  31.         x: 50; y: 180
  32.         Text {
  33.             id: buttonText
  34.             anchors.horizontalCenter: parent.horizontalCenter
  35.             anchors.verticalCenter: parent.verticalCenter
  36.             text: "Click Me"
  37.             font.pixelSize: 30
  38.         }
  39.  
  40.         color: buttonRollOver.containsMouse ? "green" : "lightblue"
  41.         MouseArea {
  42.             id: buttonRollOver
  43.             anchors.fill: parent
  44.             hoverEnabled: true
  45.         }
  46.     }
  47.  
  48.     Rectangle {
  49.         id: display
  50.         border.color: "blue"
  51.         width: lineEdit.width
  52.         height: lineEdit.height
  53.         x: button.x;
  54.         y: 70
  55.     }
  56. }

7 replies

November 25, 2010

Cochise Cochise
Lab Rat
30 posts

Well…

Create a state for button when the property buttonText.text changes to textField.text and create a trigger “onClickerd” on buttonRollOver what chenges the state of buttonText

 Signature 

http://lafactoria.com.br

December 2, 2010

xsacha xsacha
Lab Rat
517 posts

I have done this exact procedure a few times, so I’ll show you how easy it is:
Inside the buttonRollOver MouseArea:

  1. onClicked: displaytext.text = textField.text

Simply move the text you have in textField to a text element inside display.

Inside the display Rectangle:

  1. Text {
  2.         id: displaytext
  3.         anchors.left: parent.left; y: 16
  4.         anchors.right: parent.right
  5.         font.pixelSize: 32
  6.      }

A text element to display the text.

 Signature 

- Sacha

December 2, 2010

QtK QtK
Lab Rat
1140 posts

Just tried it out works fine.

  1. import Qt 4.7
  2.  
  3. Rectangle {
  4.     width: 300
  5.     height: 300
  6.     Rectangle {
  7.         id: lineEdit
  8.         width: 200
  9.         height: 50
  10.         anchors.horizontalCenter: parent.horizontalCenter
  11.         anchors.verticalCenter: parent.verticalCenter
  12.         color: "lightblue"
  13.         border.color: "green"
  14.         TextInput {
  15.             id: textField
  16.             anchors.left: parent.left; y: 16
  17.             anchors.right: parent.right
  18.             //border.color: "green"
  19.             text: "field 1"
  20.             font.pixelSize: 32
  21.             //color: focus ? "black" : "gray"
  22.         }
  23.     }
  24.     Rectangle {
  25.         id: button
  26.         radius: 5
  27.         width: lineEdit.width
  28.         height: lineEdit.height
  29.         border.color: "black"
  30.         //color: "lightblue"
  31.         x: 50; y: 180
  32.         Text {
  33.             id: buttonText
  34.             anchors.horizontalCenter: parent.horizontalCenter
  35.             anchors.verticalCenter: parent.verticalCenter
  36.             text: "Click Me"
  37.             font.pixelSize: 30
  38.         }
  39.  
  40.         color: buttonRollOver.containsMouse ? "green" : "lightblue"
  41.         MouseArea {
  42.             id: buttonRollOver
  43.             anchors.fill: parent
  44.             hoverEnabled: true
  45.            onClicked: showText.text = textField.text
  46.         }
  47.     }
  48.  
  49.     Rectangle {
  50.         id: display
  51.         border.color: "blue"
  52.         width: lineEdit.width
  53.         height: lineEdit.height
  54.         x: button.x;
  55.         y: 70
  56.         Text {
  57.             id: showText
  58.             anchors.horizontalCenter: parent.horizontalCenter
  59.             anchors.verticalCenter: parent.verticalCenter
  60.             font.pixelSize: 30
  61.         }
  62.  
  63.     }
  64. }

December 2, 2010

QtK QtK
Lab Rat
1140 posts

How can we have a longer text entered wrapped inside the rectangle display.

December 2, 2010

2beers 2beers
Lab Rat
149 posts
QtK wrote:
How can we have a longer text entered wrapped inside the rectangle display.

maybe you are looking for this: wrapMode [doc.qt.nokia.com]
and you anchor the text field to the entire rectangle area

December 2, 2010

xsacha xsacha
Lab Rat
517 posts

QtK wrote:
How can we have a longer text entered wrapped inside the rectangle display.

Here’s an example of how to do that:

  1. import QtQuick 1.0
  2.  
  3. FocusScope {
  4.     width: 200; height: 200
  5.     Rectangle {
  6.         property int margins: 20
  7.         x: margins; y: margins
  8.         width: parent.width - margins*2; height: parent.height - margins*2
  9.         border.color: "black"; border.width: 2; radius: 10
  10.         color: "#23896363"
  11.         Flickable {
  12.             id: flick
  13.             width: parent.width - 10; height: parent.height;
  14.             clip: true
  15.             function ensureVisible(r)
  16.             {
  17.                 if (contentY >= r.y)
  18.                     contentY = r.y;
  19.                 else if (contentY+height <= r.y+r.height)
  20.                     contentY = r.y+r.height-height;
  21.             }
  22.             TextEdit {
  23.                 id: message
  24.                 x: 5;
  25.                 width: parent.width; height: parent.height;
  26.                 wrapMode: "WrapAtWordBoundaryOrAnywhere"
  27.                 onCursorRectangleChanged: flick.ensureVisible(cursorRectangle)
  28.                 font.pixelSize: 16
  29.             }
  30.         }
  31.     }
  32. }

 Signature 

- Sacha

December 2, 2010

QtK QtK
Lab Rat
1140 posts

Thanks its working.

 
  ‹‹ Strings in QML/QtQuick/Qt      (Solved) Transparent Windows ››

You must log in to post a reply. Not a member yet? Register here!