Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. Cannot copy the text of TextField by the copy method
Qt 6.11 is out! See what's new in the release blog

Cannot copy the text of TextField by the copy method

Scheduled Pinned Locked Moved Solved QML and Qt Quick
2 Posts 1 Posters 997 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • thamT Offline
    thamT Offline
    tham
    wrote on last edited by tham
    #1

    Try to implement a right click menu like QLineEdit, but I can't make the copy method work, I guess this is because the selectedText of the TextField always empty.

    import QtQuick 2.9
    
    import QtQuick.Controls 2.2
    import QtQuick.Layouts 1.3
    
    TextField{
        id: textField
    
        height: 20
        width: 100
        selectByMouse: true
    
        MouseArea{
            anchors.fill: parent
            propagateComposedEvents: true
            acceptedButtons: Qt.RightButton
            onClicked: {
                menuDialog.open()
            }
        }
    
        Dialog {
            id: menuDialog
    
            width: 300
    
            x: (parent.width - width) / 2
            y: (parent.height - height) / 2
    
            title: qsTr("Menu")
    
            ColumnLayout{
                anchors.fill: parent
                spacing: 10
    
                Button{
                    id: copyBtn
    
                    text: qsTr("Copy")
    
                    Layout.fillWidth: true
    
                    onClicked: {
                        textField.copy() //copy do not work
                        console.log("copy text:" + textField.selectedText) //selectedText always empty
                        menuDialog.close()
                    }
                }
    
                Button{
                    id: pasteBtn
    
                    text: qsTr("Paste")
    
                    Layout.fillWidth: true
    
                    onClicked: {
                        console.log("paste text")
                        menuDialog.close()
                        textField.paste() //paste function work, copy by keyboard
                    }
                }
            }
        }
    }
    

    How could I select the text after I press on the "copy" button(opened by the right button of the mouse)?

    1 Reply Last reply
    0
    • thamT Offline
      thamT Offline
      tham
      wrote on last edited by
      #2

      Just find out why, problem is, every time I opened the Dialog, the selectionText will become empty, one of the solution is set persistentSelection to true.

      1 Reply Last reply
      2

      • Login

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • Users
      • Groups
      • Search
      • Get Qt Extensions
      • Unsolved