Results 1 to 4 of 4

Thread: QTextEdit - disable paste()

  1. #1
    Join Date
    Jan 2008
    Posts
    2

    Default 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).
    Qt Code:
    1. void Edytor::mousePressEvent ( QMouseEvent * e ) {
    2.  
    3. if(e->button()==(Qt::MidButton)){
    4. qDebug()<<"middle";
    5. e->accept();
    6. return;
    7. };
    8. QTextEdit::mousePressEvent(e);
    9. };
    To copy to clipboard, switch view to plain text mode 

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

  2. #2
    Join Date
    Aug 2006
    Location
    Bangalore,India
    Posts
    419
    Thanks
    37
    Thanked 53 Times in 40 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

    Default Re: QTextEdit - disable paste()

    There is a chance the following works.
    Qt Code:
    1. if(event->buttons().test(Qt::MidButton)) {
    2. return;
    3. }
    To copy to clipboard, switch view to plain text mode 

    Otherwise you can reimplement QTextEdit::insertFromMimeData like this
    Qt Code:
    1. class Edit : public QTextEdit
    2. {
    3. public:
    4. //the following effectively disables paste (as well as drop)
    5. void insertFromMimeData(const QMimeData *src) {}
    6. };
    To copy to clipboard, switch view to plain text mode 

    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.
    Last edited by Gopala Krishna; 31st January 2008 at 18:34.
    The biggest difference between time and space is that you can't reuse time.
    -- Merrick Furst

  3. #3
    Join Date
    Jan 2008
    Posts
    2

    Default Re: QTextEdit - disable paste()

    Thank you for reply.
    First is same I did.

    I've also tried the second before
    Qt Code:
    1. bool Edytor::canInsertFromMimeData (const QMimeData*){
    2. qDebug()<<"CanInsert";
    3. return false;
    4. };
    5.  
    6. void Edytor::insertFromMimeData (QMimeData*){
    7. qDebug()<<"Insert";
    8. return;
    9. };
    To copy to clipboard, switch view to plain text mode 

    But it doesn't work too.

    I checked qt source code.

    Qt Code:
    1. void QTextEdit::paste()
    2. {
    3. Q_D(QTextEdit);
    4. d->control->paste();
    5. }
    To copy to clipboard, switch view to plain text mode 

    d is instance of QTextEdit.
    Line d->control->paste() invokes paste method of QTextControl class.
    Which looks like below:
    Qt Code:
    1. void QTextControl::paste()
    2. {
    3. const QMimeData *md = QApplication::clipboard()->mimeData();
    4. if (md)
    5. insertFromMimeData(md);
    6. }
    To copy to clipboard, switch view to plain text mode 
    It invokes insertFromMimeData(QMimeData *src) but not from QTextEdit but from QTextControl to which I have no access.

    Am I wrong somewhere?

  4. #4
    Join Date
    Dec 2011
    Posts
    1
    Qt products
    Qt3
    Platforms
    MacOS X

    Default 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

Similar Threads

  1. Problem pasting text into a QTextEdit
    By Spockmeat in forum Qt Programming
    Replies: 8
    Last Post: 14th March 2009, 15:36
  2. QTextEdit slow to insert text
    By thomaspu in forum Qt Programming
    Replies: 4
    Last Post: 10th January 2008, 13:05
  3. Disable Return on QTextEdit
    By raphaelf in forum Newbie
    Replies: 3
    Last Post: 23rd May 2007, 09:04
  4. QTextEdit API questions (plain text)
    By Gaspar in forum Qt Programming
    Replies: 4
    Last Post: 16th May 2006, 07:03

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.