PDA

View Full Version : QTextEdit - disable paste()



PePeL
31st January 2008, 17:46
Hi all,

For 2 days I'm fighting with QTextEdit to disable pasting. I disabled Ctrl+P by keyPressEvent().
I don't know how to grab Middle Mouse Button (linux shortcut fo paste).


void Edytor::mousePressEvent ( QMouseEvent * e ) {

if(e->button()==(Qt::MidButton)){
qDebug()<<"middle";
e->accept();
return;
};
QTextEdit::mousePressEvent(e);
};


When I use above it shows 'middle' but also takes default action (pastes text).
Any idea?

Gopala Krishna
31st January 2008, 18:15
There is a chance the following works.

if(event->buttons().test(Qt::MidButton)) {
return;
}

Otherwise you can reimplement QTextEdit::insertFromMimeData like this

class Edit : public QTextEdit
{
public:
//the following effectively disables paste (as well as drop)
void insertFromMimeData(const QMimeData *src) {}
};


Edit:
However note that the above will also disable text drop. You can tackle that by setting a flag in dragEnterEvent and checking the same in the reimplemented insertFromMimeData method.
Do read the docs for clear picture.

PePeL
31st January 2008, 19:05
Thank you for reply.
First is same I did.

I've also tried the second before

bool Edytor::canInsertFromMimeData (const QMimeData*){
qDebug()<<"CanInsert";
return false;
};

void Edytor::insertFromMimeData (QMimeData*){
qDebug()<<"Insert";
return;
};


But it doesn't work too.

I checked qt source code.


void QTextEdit::paste()
{
Q_D(QTextEdit);
d->control->paste();
}


d is instance of QTextEdit.
Line d->control->paste() invokes paste method of QTextControl class.
Which looks like below:

void QTextControl::paste()
{
const QMimeData *md = QApplication::clipboard()->mimeData();
if (md)
insertFromMimeData(md);
}

It invokes insertFromMimeData(QMimeData *src) but not from QTextEdit but from QTextControl to which I have no access.

Am I wrong somewhere?

frohro
23rd December 2011, 08:51
Any further developments here? I also tried sub-classing QTextEdit and overriding canInsertFromMimeData (const QMimeData*) as the docs seem to indicate should work, but if doesn't for me. I also tried overriding canPaste() without success. Any other ideas?

Thanks,

Rob