Qt World Summit: Submit your Presentation

How to implement TextInput drag and drop

  • I am making a simple text editor using QML. I want to make feature that select text by mouse and drag to move the text(Like the modern text editors working).
    But there seems no properties or methods for the selected area rectangle.

    TextInput {
      id: textInput
      width: 100
      height: 50
      selectByMouse: true

    This code can make select text by mouse but when I try drag this, the previous selection is removed and new selection is started.
    My idea was using MouseArea, but I can't calculate the position of the selected text area rectangle.
    How can I do this? Is it imposible?

  • You are trying to do 2 things that require the same actions. Drag and drop is not built into the TextInput object. So you need to learn how drag and drop works.

    Next, since you need to be able to change the mode of your TextInput you need to control when a selection can occur. Say for instance if text is selected that you do NOT want selection motion to work. Then use the selectedText property to control the selectByMouse property:

    selectByMouse: selectedText <= 0

    I do not know if this will lock you into that mode. So you may need a way to back out of a selection. What happens when you click the text again? Does it deselect the text?

Log in to reply