PDA

View Full Version : Qt4/Win32 - Reimplementing Copy



gesslar
22nd March 2006, 03:41
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.



void GDoc::copy() {
QTextEdit::copy();
QClipboard *clip = QApplication::clipboard();
QString work = clip->text();

if(work.length() <= 79) return ;
QStringList blocks = work.split("\n");
bool done = false;
for(int i = 0; i < blocks.count(); i++) {
int left = 0;
int right = 79;
QStringList inner;
if(blocks[i].length() <= 79) continue;
do {
QString sec = blocks[i].mid(left, right);
int pos = sec.lastIndexOf(QChar(32));
if(pos == -1) {
inner.append(sec.trimmed());
blocks[i].remove(0,79);
} else {
inner.append(sec.mid(0,pos).trimmed());
blocks[i].remove(0,pos+1);
}
if(blocks[i].length() < 79) {
inner.append(blocks[i]);
blocks[i] = inner.join("\n");
done = true;
}
} while ( !done );
done = false;
}
clip->setText(blocks.join("\n"));
}


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!



void GDoc::keyPressEvent( QKeyEvent *e) {
if(e->key() == Qt::Key_Tab && !(e->modifiers() | Qt::NoModifier)) {
insertPlainText(pad()); // Replace tab character with 4 spaces
e->accept();
return ;
} else if (e->key() == Qt::Key_C && (e->modifiers() |
Qt::ControlModifier)) {
copy();
e->accept();
}
QTextEdit::keyPressEvent(e);
}


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.

gesslar
22nd March 2006, 06:32
Damnit...ok, easy. I forgot to put a return in the if statement which handles the ctrl-c