如何替换剪贴板中QTextEdit复制的文本? [英] How can I replace the copied text from QTextEdit in my clipboard?

查看:403
本文介绍了如何替换剪贴板中QTextEdit复制的文本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个QTextEdit,其中包含文本:

 这是测试。 

我想选择此文本并使用Ctrl + C将其复制到剪贴板中,但是我想在剪贴板中将测试替换为良好。



我的意思是:从 QTextEdit 复制原始文本后,我想在剪贴板中得到它:

 很好。 

注意:仅从QTextEdit复制文本时,我想替换剪贴板,但我不想

谢谢。

解决方案

最好使用Signals / Slots将剪贴板中的更改与您在QTextEdit字段中实际执行的 进行同步,以避免未定义的行为和意外地修改任务范围之外的内容。为了做到这一点,当您 highlight 这个特定的QTextEdit字段时,发出一个信号,该信号确保您可以复制突出显示的文本 QTextEdit :: copyAvailable(bool yes) .. 表示突出显示文本的可用性。



最重要的是,通过附加到信号 QClipboard,确保仅在CTRL + C从QTextEdit字段中突出显示文本时才访问全局剪贴板。 :dataChanged 表示您复制了文本...然后仅修改了文本。


要对此进行测试代码:写下您的句子..突出显示它..使用CTRL + C
复制到剪贴板并对其进行修改。


示例:
类文件如下所示:

  .h 
{
专用插槽:
void textSelected(bool yes);
void changeTextCopiedToCB();

私人:
QClipboard * clipboard;
};

Class .cpp

  MainWindow :: MainWindow(QWidget * parent):
QMainWindow(parent),
ui(new Ui :: MainWindow)
{
ui-> setupUi(this);
connect(this-> ui-> textEdit,& QTextEdit :: copyAvailable,this,& MainWindow :: textSelected); //当选择文本
剪贴板= QApplication :: clipboard();时发出
}

void MainWindow :: textSelected(bool yes)//仅当您在字段中选择文本时才调用槽
{
if(yes){
qDebug()<< this-> ui-> textEdit-> toPlainText();
connect(clipboard,& QClipboard :: dataChanged,this,& MainWindow :: changeTextCopiedToCB); //等待tor CTRL + C
}
}
void MainWindow :: changeTextCopiedToCB()//一旦CTRL + C ..剪贴板中的数据更改..那就是我的数据
{
QString text =剪贴板-> text();
text.replace(QString( test),QString( good)));
剪贴板-> setText(text);
断开连接(剪贴板,& QClipboard :: dataChanged,this,& MainWindow :: changeTextCopiedToCB); //从该字段复制完后,不要理会剪贴板!
qDebug()<<剪贴板-> text();
}


I have a QTextEdit, which contains the text:

It's test.

I want to select this text and copy it to my clipboard using Ctrl+C, but I want to replace "test" with "good" in my clipboard.

I mean: I want to get this in my clipboard after copying original text from QTextEdit:

It's good.

Note: I want to replace clipboard when I have copied text from QTextEdit only, I don't want to replace clipboard when any copy action done.

Thanks.

解决方案

It would be better to use Signals/Slots to synchronize what to change in clipboard with what you are actually doing in QTextEdit field to avoid undefined behaviors and accidentally modifying things outside the scope of your task. in order to do that catch a signal emitted when you highlight this particular QTextEdit field, that signal insures you you can copy the highlighted text QTextEdit::copyAvailable(bool yes) .. yes indicates availability of a highlighted text.

Most importantly, make sure you are accessing global clipboard only when you CTRL+C the highlighted text from your QTextEdit field, by attaching to the signal QClipboard::dataChanged which indicates that you copied the text ... then only modify the text.

To test this code: write your sentence .. highlight it .. use CTRL+C to copy to clipboard and its modified.

Example: class files can look like this:

.h
{
private slots:
    void textSelected(bool yes);
    void changeTextCopiedToCB();

private:
    QClipboard *clipboard;
};

Class .cpp

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    connect(this->ui->textEdit, &QTextEdit::copyAvailable, this, &MainWindow::textSelected); // emmited when you select the text
    clipboard = QApplication::clipboard();
}

void MainWindow::textSelected(bool yes) // Slot called only when you select text in your field
{
    if (yes){
        qDebug() << this->ui->textEdit->toPlainText();
        connect(clipboard, &QClipboard::dataChanged, this, &MainWindow::changeTextCopiedToCB); // wait tor CTRL+C
    }
}
void MainWindow::changeTextCopiedToCB() // Once CTRL+C .. the data in clipboard changes..thats my data
{
    QString text = clipboard->text();
    text.replace(QString("test"), QString("good"));
    clipboard->setText(text);
    disconnect(clipboard, &QClipboard::dataChanged, this, &MainWindow::changeTextCopiedToCB); // after copy from this field, leave clipboard alone!
    qDebug() << clipboard->text();
}

这篇关于如何替换剪贴板中QTextEdit复制的文本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆