Results 1 to 2 of 2

Thread: Qt4/Win32 - Reimplementing Copy

  1. #1
    Join Date
    Jan 2006
    Posts
    13
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Qt4/Win32 - Reimplementing Copy

    I have a GDoc which is a subclass of QTextEdit.

    I want to re-implement copy/paste functionality of it because I want to manipulate the data before releasing it.

    Qt Code:
    1. void GDoc::copy() {
    2. QTextEdit::copy();
    3. QClipboard *clip = QApplication::clipboard();
    4. QString work = clip->text();
    5.  
    6. if(work.length() <= 79) return ;
    7. QStringList blocks = work.split("\n");
    8. bool done = false;
    9. for(int i = 0; i < blocks.count(); i++) {
    10. int left = 0;
    11. int right = 79;
    12. QStringList inner;
    13. if(blocks[i].length() <= 79) continue;
    14. do {
    15. QString sec = blocks[i].mid(left, right);
    16. int pos = sec.lastIndexOf(QChar(32));
    17. if(pos == -1) {
    18. inner.append(sec.trimmed());
    19. blocks[i].remove(0,79);
    20. } else {
    21. inner.append(sec.mid(0,pos).trimmed());
    22. blocks[i].remove(0,pos+1);
    23. }
    24. if(blocks[i].length() < 79) {
    25. inner.append(blocks[i]);
    26. blocks[i] = inner.join("\n");
    27. done = true;
    28. }
    29. } while ( !done );
    30. done = false;
    31. }
    32. clip->setText(blocks.join("\n"));
    33. }
    To copy to clipboard, switch view to plain text mode 

    Simply, the above takes a bunch of text, and forces line-wrapping at a specified column and then sends that to the clipboard ready to be pasted as prescribed.

    If I call the copy() function from my application, say from a menu, or toolbar it works. When I hit Ctlr-C, it does not work.

    So! I had this brilliant idea of capturing the Ctrl-C event!

    Qt Code:
    1. void GDoc::keyPressEvent( QKeyEvent *e) {
    2. if(e->key() == Qt::Key_Tab && !(e->modifiers() | Qt::NoModifier)) {
    3. insertPlainText(pad()); // Replace tab character with 4 spaces
    4. e->accept();
    5. return ;
    6. } else if (e->key() == Qt::Key_C && (e->modifiers() |
    7. Qt::ControlModifier)) {
    8. copy();
    9. e->accept();
    10. }
    11. QTextEdit::keyPressEvent(e);
    12. }
    To copy to clipboard, switch view to plain text mode 

    Now, I put a test QMessageBox::information throughout the Copy code and it DOES get executed, but when I paste anywhere the data is unchanged. I think that the clipboard is getting over-written afterwards, it's the only explanation. I honestly don't know, but would SERIOUSLY appreciate some help.

    I have some code I want to do for pasting, but if it's going to behave like this I might just forget it.

  2. #2
    Join Date
    Jan 2006
    Posts
    13
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Qt4/Win32 - Reimplementing Copy

    Damnit...ok, easy. I forgot to put a return in the if statement which handles the ctrl-c

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
  •  
Qt is a trademark of The Qt Company.