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. automatically copy selected text

automatically copy selected text

Scheduled Pinned Locked Moved General and Desktop
qtexteditselection
12 Posts 4 Posters 9.0k 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.
  • R Offline
    R Offline
    ravas
    wrote on 20 Jun 2015, 19:00 last edited by ravas
    #1

    How would you automatically copy the selection in a QTextEdit?

    I thought I found the easy way:

    void Notepad::on_textEdit_copyAvailable(bool b)
    {
        if (b)
        {
            ui->textEdit->copy();
        }
    }
    

    But what it copies doesn't seem consistent. Most of the time it only copies the first letter of the selection.

    I also thought to try mouseReleaseEvent, but I don't yet fully understand QT / C++ inheritance.

    What I'm working from: http://doc.qt.io/qt-5/gettingstartedqt.html

    notepad.h

    #define NOTEPAD_H
    
    #include <QMainWindow>
    #include <QTextEdit>
    
    namespace Ui {
    class Notepad;
    }
    
    //class textEdit;
    
    class Notepad : public QMainWindow
    {
        Q_OBJECT
    
    public:
        explicit Notepad(QWidget *parent = 0);
        ~Notepad();
    
    private slots:
        void on_quitButton_clicked();
    
        void on_actionOpen_triggered();
    
        void on_actionSave_triggered();
    
        void on_textEdit_copyAvailable(bool b);
    
    private:
        Ui::Notepad *ui;
    };
    
    //class textEdit : public QTextEdit
    //{
    //    Q_OBJECT
    
    //public:
    //    void mouseReleaseEvent(QMouseEvent *event) Q_DECL_OVERRIDE;
    
    //};
    
    #endif // NOTEPAD_H
    

    notepad.cpp

    #include "notepad.h"
    #include "ui_notepad.h"
    #include <QFileDialog>
    #include <QFile>
    #include <QMessageBox>
    #include <QTextStream>
    
    Notepad::Notepad(QWidget *parent) :
        QMainWindow(parent),
        ui(new Ui::Notepad)
    {
        ui->setupUi(this);
    }
    
    Notepad::~Notepad()
    {
        delete ui;
    }
    
    void Notepad::on_quitButton_clicked()
    {
        qApp->quit();
    }
    
    void Notepad::on_actionOpen_triggered()
    {
        QString fileName = QFileDialog::getOpenFileName(this, tr("Open File"), QString(),
                tr("Text Files (*.txt);;C++ Files (*.cpp *.h)"));
    
        if (!fileName.isEmpty()) {
            QFile file(fileName);
            if (!file.open(QIODevice::ReadOnly)) {
                QMessageBox::critical(this, tr("Error"), tr("Could not open file"));
                return;
            }
            QTextStream in(&file);
            ui->textEdit->setText(in.readAll());
            file.close();
        }
    }
    
    void Notepad::on_actionSave_triggered()
    {
        QString fileName = QFileDialog::getSaveFileName(this, tr("Save File"), QString(),
                tr("Text Files (*.txt);;C++ Files (*.cpp *.h)"));
    
        if (!fileName.isEmpty()) {
            QFile file(fileName);
            if (!file.open(QIODevice::WriteOnly)) {
                // error message
            } else {
                QTextStream stream(&file);
                stream << ui->textEdit->toPlainText();
                stream.flush();
                file.close();
            }
        }
    }
    
    //void textEdit::mouseReleaseEvent(QMouseEvent *event)
    //{
    //    ui->textEdit->copy();
    //}
    
    
    void Notepad::on_textEdit_copyAvailable(bool b)
    {
        if (b)
        {
            ui->textEdit->copy();
        }
    }
    
    1 Reply Last reply
    0
    • S Offline
      S Offline
      SGaist
      Lifetime Qt Champion
      wrote on 20 Jun 2015, 21:29 last edited by
      #2

      Hi and welcome to devnet,

      There's no need for that, have a look at copyAvailable and copy

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

      R 1 Reply Last reply 20 Jun 2015, 21:49
      0
      • S SGaist
        20 Jun 2015, 21:29

        Hi and welcome to devnet,

        There's no need for that, have a look at copyAvailable and copy

        R Offline
        R Offline
        ravas
        wrote on 20 Jun 2015, 21:49 last edited by
        #3

        @SGaist I read those pages, and posted my code based on those functions, and noted a problem.

        1 Reply Last reply
        0
        • S Offline
          S Offline
          SGaist
          Lifetime Qt Champion
          wrote on 20 Jun 2015, 21:52 last edited by
          #4

          Sorry, I misunderstood that. What version of Qt 5 are you using ?

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

          R 1 Reply Last reply 20 Jun 2015, 21:53
          0
          • S SGaist
            20 Jun 2015, 21:52

            Sorry, I misunderstood that. What version of Qt 5 are you using ?

            R Offline
            R Offline
            ravas
            wrote on 20 Jun 2015, 21:53 last edited by ravas
            #5

            @SGaist 5.4.1 community (windows 7)

            1 Reply Last reply
            0
            • R Offline
              R Offline
              ravas
              wrote on 20 Jun 2015, 22:57 last edited by
              #6

              When I double click on a word it will copy the whole word.
              When I start on the right of the word and drag to select,
              it will copy the right most character;
              starting on the left it will copy the left most character.

              1 Reply Last reply
              0
              • JKSHJ Offline
                JKSHJ Offline
                JKSH
                Moderators
                wrote on 21 Jun 2015, 14:13 last edited by
                #7

                Hi @ravas ,

                Use the selectionChanged() signal instead.

                The copyAvailable() signal is only emitted when you change from available to not-available (or the other way round). It is not emitted when you add more characters to your selection.

                Qt Doc Search for browsers: forum.qt.io/topic/35616/web-browser-extension-for-improved-doc-searches

                R 1 Reply Last reply 21 Jun 2015, 18:28
                1
                • JKSHJ JKSH
                  21 Jun 2015, 14:13

                  Hi @ravas ,

                  Use the selectionChanged() signal instead.

                  The copyAvailable() signal is only emitted when you change from available to not-available (or the other way round). It is not emitted when you add more characters to your selection.

                  R Offline
                  R Offline
                  ravas
                  wrote on 21 Jun 2015, 18:28 last edited by ravas
                  #8

                  @JKSH I get an error with:

                  void Notepad::on_textEdit_selectionChanged()
                  {
                      ui->textEdit->copy();
                  }
                  

                  "OleSetClipboard: Failed to set mime data (text/plain, text/html, application/vnd.oasis.opendocument.text) on clipboard: COM error 0x800401d0 (Unknown error 0x0ffffffff800401d0) (The parameter is incorrect.)"

                  I tried another way:

                  void Notepad::on_textEdit_selectionChanged()
                  {
                      QTextCursor cursor(ui->textEdit->textCursor());
                      const QString sel = cursor.selectedText();
                      QClipboard *myClip = qApp->clipboard();
                      myClip->setText(sel);
                  }
                  

                  This gives the error "invalid use of incomplete type class QClipboard"...
                  I'm not sure how to complete it... ;-]

                  R JKSHJ 2 Replies Last reply 21 Jun 2015, 19:06
                  0
                  • R ravas
                    21 Jun 2015, 18:28

                    @JKSH I get an error with:

                    void Notepad::on_textEdit_selectionChanged()
                    {
                        ui->textEdit->copy();
                    }
                    

                    "OleSetClipboard: Failed to set mime data (text/plain, text/html, application/vnd.oasis.opendocument.text) on clipboard: COM error 0x800401d0 (Unknown error 0x0ffffffff800401d0) (The parameter is incorrect.)"

                    I tried another way:

                    void Notepad::on_textEdit_selectionChanged()
                    {
                        QTextCursor cursor(ui->textEdit->textCursor());
                        const QString sel = cursor.selectedText();
                        QClipboard *myClip = qApp->clipboard();
                        myClip->setText(sel);
                    }
                    

                    This gives the error "invalid use of incomplete type class QClipboard"...
                    I'm not sure how to complete it... ;-]

                    R Offline
                    R Offline
                    ravas
                    wrote on 21 Jun 2015, 19:06 last edited by
                    #9

                    I forgot #include <QClipboard>

                    However there is still an error...

                    "OleSetClipboard: Failed to set mime data (text/plain) on clipboard: COM error 0x800401d0 (Unknown error 0x0ffffffff800401d0) (The parameter is incorrect.)"

                    1 Reply Last reply
                    0
                    • R ravas
                      21 Jun 2015, 18:28

                      @JKSH I get an error with:

                      void Notepad::on_textEdit_selectionChanged()
                      {
                          ui->textEdit->copy();
                      }
                      

                      "OleSetClipboard: Failed to set mime data (text/plain, text/html, application/vnd.oasis.opendocument.text) on clipboard: COM error 0x800401d0 (Unknown error 0x0ffffffff800401d0) (The parameter is incorrect.)"

                      I tried another way:

                      void Notepad::on_textEdit_selectionChanged()
                      {
                          QTextCursor cursor(ui->textEdit->textCursor());
                          const QString sel = cursor.selectedText();
                          QClipboard *myClip = qApp->clipboard();
                          myClip->setText(sel);
                      }
                      

                      This gives the error "invalid use of incomplete type class QClipboard"...
                      I'm not sure how to complete it... ;-]

                      JKSHJ Offline
                      JKSHJ Offline
                      JKSH
                      Moderators
                      wrote on 22 Jun 2015, 00:03 last edited by
                      #10

                      @ravas said:

                      void Notepad::on_textEdit_selectionChanged()
                      {
                          ui->textEdit->copy();
                      }
                      

                      This code is correct.

                      I tested your code, and it works for me. I used Qt 5.4.1, both MinGW 4.9.1 and MSVC 2013. My OS is Windows 8.1 Pro x64.

                      "OleSetClipboard: Failed to set mime data (text/plain, text/html, application/vnd.oasis.opendocument.text) on clipboard: COM error 0x800401d0 (Unknown error 0x0ffffffff800401d0) (The parameter is incorrect.)"

                      This is an error with your Windows clipboard, not with Qt. Make sure that other programs are not trying to use the clipboard at the same time. Try rebooting your computer and see if that helps.

                      Qt Doc Search for browsers: forum.qt.io/topic/35616/web-browser-extension-for-improved-doc-searches

                      R 1 Reply Last reply 22 Jun 2015, 00:14
                      1
                      • JKSHJ JKSH
                        22 Jun 2015, 00:03

                        @ravas said:

                        void Notepad::on_textEdit_selectionChanged()
                        {
                            ui->textEdit->copy();
                        }
                        

                        This code is correct.

                        I tested your code, and it works for me. I used Qt 5.4.1, both MinGW 4.9.1 and MSVC 2013. My OS is Windows 8.1 Pro x64.

                        "OleSetClipboard: Failed to set mime data (text/plain, text/html, application/vnd.oasis.opendocument.text) on clipboard: COM error 0x800401d0 (Unknown error 0x0ffffffff800401d0) (The parameter is incorrect.)"

                        This is an error with your Windows clipboard, not with Qt. Make sure that other programs are not trying to use the clipboard at the same time. Try rebooting your computer and see if that helps.

                        R Offline
                        R Offline
                        ravas
                        wrote on 22 Jun 2015, 00:14 last edited by
                        #11

                        @JKSH I closed all programs and opened Qt creator again; and now it works... :D
                        Thanks

                        1 Reply Last reply
                        0
                        • C Offline
                          C Offline
                          CathyG
                          wrote on 13 Oct 2015, 22:28 last edited by
                          #12

                          I got the Clipboard error when I had RealVncViewer running at the same time. I closed VNC & then the application ran without the clipboard error.

                          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