×

Discussion Board

Results 1 to 6 of 6
  1. #1
    Registered User
    Join Date
    Jul 2011
    Posts
    69

    Unhappy positionAt in QML TextEdit

    I am trying to use the methode positionAt of the QML TextEdit object to determin the text a user clicked onto.
    The TextEdit text is HTML including a table (see http://www.ruinelli.ch/wordpress/wp-...niBible_10.png for an example).

    Sadly positionAt only gives me unusable values. When clicking in some areas of the text, it even gives the same char pos for a whole word.

    Has anybody successfully used positionAt before?
    It seems to be brocken or at least not well documented


    To use the oposit function positionToRectangle I learned that I have to drop all non visible tags of the HTML text.
    Sadly there is no note about this in the documentation nor an example...

  2. #2
    Nokia Developer Expert
    Join Date
    Oct 2007
    Posts
    441

    Re: positionAt in QML TextEdit

    Hi

    Try TextArea from QComponents it has the same method. However i doubt i behaves differently. The reason of the problem i think the text wrapping mode. Try other approach : if you need to react on user's click in the text --- use links! check TextEdit :: onLinkActivated ( string link ) from the documentation . You will need to make the links invisible in the text though, i hope styling may help.

    And as the last resort -- use list to format and display your content

  3. #3
    Registered User
    Join Date
    Jul 2011
    Posts
    69

    Re: positionAt in QML TextEdit

    Thank you for your hints.
    I will have to digg a bit more into this.
    Sadly even with the ways you suggest it does not work as desired:
    • With TextAreas, it seems to work a bit better. Now I get more or less the right position. Maybe I will have to remove the \n or other non visible elements like double spaces. (on the other hand, with TextArea somehow I can not scroll anymore, i guess it doesn’t know its content height).
    • I can not use Links, because the TextEdit has to be read only. (If not read only, the virtual keyboard gets shown).
    • I can not use lists, because I do not want to add a line wrap after each bible verse.


    What did you exactly mean with the wrapping mode issue?
    Could this somehow be worked around?

    Thank you very much for any hints!

  4. #4
    Nokia Developer Expert
    Join Date
    Oct 2007
    Posts
    441

    Re: positionAt in QML TextEdit

    Hi CaCO3

    In contrast to what the documentation says TextArea does not support scrolling -- i have checked. As workaround try this code below

    Code:
    import QtQuick 1.1
    import com.nokia.meego 1.0
    
    Page {
    
        Flickable {
                id: scrolled
    
                width: parent.width
                height: parent.height
                contentWidth: textblock.paintedWidth
                contentHeight: textblock.paintedHeight
    
                TextEdit  {
                    id: textblock
                    textFormat: Text.RichText
                    wrapMode: TextEdit.Wrap
    
                    text: "1 line <br/>"+
                          "2 line <br/>"+
                          "3 line <br/>"+
                          "4 line <br/>"+
                          "5 line <br/>"+
                          "6 - some sample text (<a href=\"verse#1\">link to verse#1</a>)<br/>"+
                          "7 line <br/>"+
                          "8 line <br/>"+
                          "9 line <br/>"+
                          "10 some other sample text (<a href=\"verse#2\">link to verse#2</a>)<br/>"+
                          "11 line <br/>"+
                          "12 line <br/>"+
                          "14 line <br/>"+
                          "15 line <br/>"+
                          "16 line <br/>"+
                          "17 line <br/>"+
                          "18 line <br/>"+
                          "19 line <br/>"+
                          "20 line <br/>"
                    onLinkActivated: console.log(link) //TODO: process 'link' variable
                                                       // note the variable is embedded to QML
                                                       // i.e. -- no declaration is needed in your code
                }
        }
    
        ScrollBar {
                scrollArea: scrolled;
                width: 10
                anchors { right: parent.right; top: parent.top; bottom: parent.bottom }
             }
    }
    and you will need ScrollBar.qml definition. i took it from Qt source code gitorius repo. You need to create ScrollBar.qml and put the following into it:

    Code:
    // import QtQuick 1.0 // to target S60 5th Edition or Maemo 5
    import QtQuick 1.1
    
    /****************************************************************************
    **
    ** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
    ** All rights reserved.
    ** Contact: Nokia Corporation (qt-info@nokia.com)
    **
    ** This file is part of the QtDeclarative module of the Qt Toolkit.
    **
    ** $QT_BEGIN_LICENSE:LGPL$
    ** No Commercial Usage
    ** This file contains pre-release code and may not be distributed.
    ** You may use this file in accordance with the terms and conditions
    ** contained in the Technology Preview License Agreement accompanying
    ** this package.
    **
    ** GNU Lesser General Public License Usage
    ** Alternatively, this file may be used under the terms of the GNU Lesser
    ** General Public License version 2.1 as published by the Free Software
    ** Foundation and appearing in the file LICENSE.LGPL included in the
    ** packaging of this file.  Please review the following information to
    ** ensure the GNU Lesser General Public License version 2.1 requirements
    ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
    **
    ** In addition, as a special exception, Nokia gives you certain additional
    ** rights.  These rights are described in the Nokia Qt LGPL Exception
    ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
    **
    ** If you have questions regarding the use of this file, please contact
    ** Nokia at qt-info@nokia.com.
    **
    **
    **
    **
    **
    **
    **
    **
    ** $QT_END_LICENSE$
    **
    ****************************************************************************/
    
    import QtQuick 1.0
    
    Item {
        id: container
    
        property variant scrollArea
        property variant orientation: Qt.Vertical
    
        opacity: 0
    
        function position()
        {
            var ny = 0;
            if (container.orientation == Qt.Vertical)
                ny = scrollArea.visibleArea.yPosition * container.height;
            else
                ny = scrollArea.visibleArea.xPosition * container.width;
            if (ny > 2) return ny; else return 2;
        }
    
        function size()
        {
            var nh, ny;
    
            if (container.orientation == Qt.Vertical)
                nh = scrollArea.visibleArea.heightRatio * container.height;
            else
                nh = scrollArea.visibleArea.widthRatio * container.width;
    
            if (container.orientation == Qt.Vertical)
                ny = scrollArea.visibleArea.yPosition * container.height;
            else
                ny = scrollArea.visibleArea.xPosition * container.width;
    
            if (ny > 3) {
                var t;
                if (container.orientation == Qt.Vertical)
                    t = Math.ceil(container.height - 3 - ny);
                else
                    t = Math.ceil(container.width - 3 - ny);
                if (nh > t) return t; else return nh;
            } else return nh + ny;
        }
    
        Rectangle { anchors.fill: parent; color: "Black"; opacity: 0.5 }
    
        BorderImage {
            source: "scrollbar.png"
            border { left: 1; right: 1; top: 1; bottom: 1 }
            x: container.orientation == Qt.Vertical ? 2 : position()
            width: container.orientation == Qt.Vertical ? container.width - 4 : size()
            y: container.orientation == Qt.Vertical ? position() : 2
            height: container.orientation == Qt.Vertical ? size() : container.height - 4
        }
    
        states: State {
            name: "visible"
            when: container.orientation == Qt.Vertical ? scrollArea.movingVertically : scrollArea.movingHorizontally
            PropertyChanges { target: container; opacity: 1.0 }
        }
    
        transitions: Transition {
            from: "visible"; to: ""
            NumberAnimation { properties: "opacity"; duration: 600 }
        }
    }
    in addition to that create a png picture sizeof 10 X 10 pixels save it as "scrollbar.png" and put to QML source so ScrollBar.qml can find that.

    The demo is working for me as for Meego as Symbian

    Regards,
    Igor

  5. #5
    Registered User
    Join Date
    Jul 2011
    Posts
    69

    Re: positionAt in QML TextEdit

    Hi izinin

    Thank you very much for taking your time to look into this.
    How ever I am a bit confused about your example code.
    You show how to use a TextEdit with links and a self made scroll bar.
    How ever, as I stated, I can not use TextEdit with links, because the virtual keyboard would be shown. As long as there is no way to hide the keyboard permanently, links in TextEdit are no option.
    As you stated (and many other people also figured out), TextArea does not support scrolling. Now, if we could get it scrolling somehow, I think I could use it to get the clicked position in the text.

    Would there be an option to create myself a QML component like the TextEdit but having the positionAt function as in the TextArea?
    Note: since I am not using C++, I am not able to do it in there.
    There is an other app, MeeBible which somehow solved this issue with C++, but that is not an option for me.

  6. #6
    Registered User
    Join Date
    Jul 2011
    Posts
    69

    Re: positionAt in QML TextEdit

    All right, I got it working now.

    To set the TextArea to the right size (so all contained text is visible), you have to do this:
    Code:
    myTextArea.height= myTextArea.implicitHeight
    Also, the whole block is in a flickable and I also have a scrollbar:
    Code:
    ScrollDecorator { 
    	    flickableItem: flickable 	  
    	}
    After removing all double whitespaces and new lines, the positionAt now seems to more or less return the right position.

    @izinin:
    Thank you very much in any way, you brought me to the right path!

Similar Threads

  1. how do i rotate textedit
    By kusumk in forum Qt
    Replies: 1
    Last Post: 2012-06-05, 10:58
  2. How to insert a text in TextEdit
    By ggamagi in forum [Archived] Qt Quick
    Replies: 3
    Last Post: 2011-12-24, 14:08
  3. TextEdit OnTextChanged question?
    By ram4soft in forum [Archived] Qt Quick
    Replies: 3
    Last Post: 2011-09-20, 10:31
  4. QSyntaxHighlighter on a TextEdit
    By gravity0 in forum Qt
    Replies: 2
    Last Post: 2010-09-07, 12:17

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
×