Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Problem with QTextEdit copy function after assigning it a QAction
Forum Updated to NodeBB v4.3 + New Features

Problem with QTextEdit copy function after assigning it a QAction

Scheduled Pinned Locked Moved Solved General and Desktop
6 Posts 4 Posters 580 Views 3 Watching
  • 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.
  • L Offline
    L Offline
    lansing
    wrote on last edited by
    #1

    I have a QTextEdit and a QPlainTextEdit in the same widget. They cope with each others well with the "copy-to-clipboard" function. When I make a text selection and pressed Ctrl+C, the corresponded TextEdit would do the copying.

    But then I'm having trouble trying to add a button for the command. I created a QAction * m_pActionCopy and connect it to a slot

    // m_textEditor created from QPlainTextEdit
    // m_lineText created from QTextEdit
    
    void MainWindow::slotCopy()
    {  
        m_textEditor->copy();
    }
    

    Now the QPlainTextEdit is taking control of the copy function. When I highlight text and copy from the QTextEdit, the program was still listening to the QPlainTextEdit widget. How do I fix this?

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi,

      What type of button ?
      How are you creating your action ?

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      L 1 Reply Last reply
      0
      • SGaistS SGaist

        Hi,

        What type of button ?
        How are you creating your action ?

        L Offline
        L Offline
        lansing
        wrote on last edited by
        #3

        @SGaist

        The button is a push button. I also added the action into a menu.

        QAction * m_pActionCopy  = new QAction(icon, "Copy", this);
        m_pActionCopy->setShortcuts(QKeySequence::Copy);
        m_pActionCopy->setCheckable(false);
        
        connect(m_pActionCopy, SIGNAL(triggered()), this, SIGNAL(slotCopy()));
        
        pEditMenu->addAction(m_pActionCopy);
        
        JonBJ 1 Reply Last reply
        0
        • L lansing

          @SGaist

          The button is a push button. I also added the action into a menu.

          QAction * m_pActionCopy  = new QAction(icon, "Copy", this);
          m_pActionCopy->setShortcuts(QKeySequence::Copy);
          m_pActionCopy->setCheckable(false);
          
          connect(m_pActionCopy, SIGNAL(triggered()), this, SIGNAL(slotCopy()));
          
          pEditMenu->addAction(m_pActionCopy);
          
          JonBJ Offline
          JonBJ Offline
          JonB
          wrote on last edited by
          #4

          @lansing
          I'm afraid I do not understand what you are saying at all. But your slot has m_textEditor->copy();, so it will always copy from your QPlainTextEdit. If you want something to copy from QTextEdit, you will need a a slot containing m_lineText->copy();.

          M 1 Reply Last reply
          1
          • JonBJ JonB

            @lansing
            I'm afraid I do not understand what you are saying at all. But your slot has m_textEditor->copy();, so it will always copy from your QPlainTextEdit. If you want something to copy from QTextEdit, you will need a a slot containing m_lineText->copy();.

            M Offline
            M Offline
            mpergand
            wrote on last edited by mpergand
            #5

            @JonB said in Problem with QTextEdit copy function after assigning it a QAction:

            If you want something to copy from QTextEdit, you will need a a slot containing m_lineText->copy();.

            ah, now I maybe understand what @lansing want to do.

            void MainWindow::slotCopy()
            {  
              if( focusWidget() == m_textEditor) m_textEditor->copy();
              else if( focusWidget() == m_lineEdit) m_lineEdit->copy();
            }
            
            L 1 Reply Last reply
            2
            • M mpergand

              @JonB said in Problem with QTextEdit copy function after assigning it a QAction:

              If you want something to copy from QTextEdit, you will need a a slot containing m_lineText->copy();.

              ah, now I maybe understand what @lansing want to do.

              void MainWindow::slotCopy()
              {  
                if( focusWidget() == m_textEditor) m_textEditor->copy();
                else if( focusWidget() == m_lineEdit) m_lineEdit->copy();
              }
              
              L Offline
              L Offline
              lansing
              wrote on last edited by
              #6

              @mpergand

              Yes, that's what I want. Thanks. I tested it and it works.

              1 Reply Last reply
              0

              • Login

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