QTextEdit - disable paste()
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).
Code:
if(e->button()==(Qt::MidButton)){
qDebug()<<"middle";
e->accept();
return;
};
};
When I use above it shows 'middle' but also takes default action (pastes text).
Any idea?
Re: QTextEdit - disable paste()
There is a chance the following works.
Code:
if(event->buttons().test(Qt::MidButton)) {
return;
}
Otherwise you can reimplement QTextEdit::insertFromMimeData like this
Code:
{
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.
Re: QTextEdit - disable paste()
Thank you for reply.
First is same I did.
I've also tried the second before
Code:
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.
Code:
{
d->control->paste();
}
d is instance of QTextEdit.
Line d->control->paste() invokes paste method of QTextControl class.
Which looks like below:
Code:
void QTextControl::paste()
{
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?
Re: QTextEdit - disable paste()
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