PDA

View Full Version : QApplication::clipboard not read textCursor cut



patrik08
23rd May 2007, 20:43
1- If i cut text on QTextEdit subclass ...
QClipboard *netag = QApplication::clipboard();
QString textclip = netag->text(QClipboard::Selection);

QClipboard cant not read null string .... why?

2- If i take text from TextEdit::keyPressEvent(QKeyEvent *e)

QTextCursor tabact = textCursor();
QString actual = tabact.selectedText();

can read is ok but on console newline ("\n") are on unknow char (?) and

QStringList line = actual.split("\n"); can not split . why? only one line result if select 2 or more line...







void TextEdit::IndentText(const QString text , int x )
{
const QChar Nline('\n');

QClipboard *netag = QApplication::clipboard();
QString textclip = netag->text(QClipboard::Selection);

QStringList line = textclip.split(Nline);
QStringList newline;
for (int i = 0; i < line.size(); ++i) {
QString onel = QString("\t%1").arg(line.at(i));
newline.append(onel);
}

qDebug() << "### line!!! " << line.size(); /* not correct */
const QString reformat = newline.join(Nline);

netag->clear();
netag->setText(reformat);
paste();
}

void TextEdit::keyPressEvent(QKeyEvent *e)
{

if (e->key() == Qt::Key_Tab) {

QTextCursor tabact = textCursor();
int stabstart = tabact.selectionStart();
int stabstop = tabact.selectionEnd();
const QString actual = tabact.selectedText();
if (actual.size() > 3) {
IndentText(actual,1);
cut();
qDebug() << "### stabstart " << stabstart;
qDebug() << "### stabstop " << stabstart;
return;
}
}

Matt Smith
27th May 2007, 21:10
I am having similar problems, trying to write a routine to turn a block of text, with multiple lines, into a HTML list:


QString listString = EDITOR->textCursor().selectedText();

if( !listString.isEmpty() ) {
QStringList stringList = listString.split( QChar( QChar::LineSeparator ) );
for( int a = 0; a < stringList.count(); a++ ) {
stringList[a].prepend( "<li>" );
stringList[a].append( "</li>" );
}
listString = stringList.join( "\n" );
listString.prepend( "<ul>" );
listString.append( "</ul" );
EDITOR->insertPlainText( listString );
}

This also didn't work:


QString listString = EDITOR->textCursor().selectedText();

if( !listString.isEmpty() ) {
listString.prepend( "<ol><li>" );
listString.replace( QChar( QChar::LineSeparator ),
QString( "</li>%1<li>" ).arg( QChar( QChar::LineSeparator ) ) );
listString.append( "</li></ol>" );
EDITOR->insertPlainText( listString );
}


It treats the whole block as a single line, so that the list is opened and closed as expected, but contains only one element, i.e. the whole selection. However, the newlines themselves do appear when the listString is inserted back into the QTextEdit.

Does anyone know the cause of this, and if there is any way round it?

wysota
27th May 2007, 22:17
This seems to work:

#include <QString>
#include <QStringList>
#include <QApplication>
#include <QTextEdit>
#include <QClipboard>
#include <QTextDocumentFragment>
#include <QMimeData>

class TextEdit : public QTextEdit {
Q_OBJECT
public:
TextEdit(QWidget *parent=0) : QTextEdit(parent){
connect(this, SIGNAL(selectionChanged()), SLOT(updateClipboard()));
}
private slots:
void updateClipboard(){
qDebug("CALLED");
QClipboard *clip = QApplication::clipboard();
QString text = textCursor().selection().toPlainText();
QStringList slist = text.split("\n");
QString result = "<ul>\n";
foreach(QString elem, slist){
result+="<li>"+elem.trimmed()+"</li>\n";
}
result+="</ul>";
QMimeData *data = new QMimeData;
data->setHtml(result);
data->setText(result);
clip->setMimeData(data);
}
};

#include "main.moc"

int main(int argc, char **argv){
QApplication app(argc, argv);
TextEdit t;
t.show();
return app.exec();
}

Maybe the difference is that I use selection() instead of selectedText().