automatically copy selected text
-
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(); } }
-
Hi and welcome to devnet,
There's no need for that, have a look at copyAvailable and copy
-
Sorry, I misunderstood that. What version of Qt 5 are you using ?
-
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. -
@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... ;-] -
@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.