1 Attachment(s)
QApplication::clipboard not read textCursor cut
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...
Code:
void TextEdit
::IndentText(const QString text ,
int x
) {
for (int i = 0; i < line.size(); ++i) {
newline.append(onel);
}
qDebug() << "### line!!! " << line.size(); /* not correct */
const QString reformat
= newline.
join(Nline
);
netag->clear();
netag->setText(reformat);
paste();
}
{
if (e->key() == Qt::Key_Tab) {
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;
}
}
Re: QApplication::clipboard not read textCursor cut
I am having similar problems, trying to write a routine to turn a block of text, with multiple lines, into a HTML list:
Code:
QString listString
= EDITOR
->textCursor
().
selectedText();
if( !listString.isEmpty() ) {
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:
Code:
QString listString
= EDITOR
->textCursor
().
selectedText();
if( !listString.isEmpty() ) {
listString.prepend( "<ol><li>" );
listString.
replace( 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?
Re: QApplication::clipboard not read textCursor cut
This seems to work:
Code:
#include <QString>
#include <QStringList>
#include <QApplication>
#include <QTextEdit>
#include <QClipboard>
#include <QTextDocumentFragment>
#include <QMimeData>
Q_OBJECT
public:
connect(this, SIGNAL(selectionChanged()), SLOT(updateClipboard()));
}
private slots:
void updateClipboard(){
qDebug("CALLED");
QString text
= textCursor
().
selection().
toPlainText();
result+="<li>"+elem.trimmed()+"</li>\n";
}
result+="</ul>";
data->setHtml(result);
data->setText(result);
clip->setMimeData(data);
}
};
#include "main.moc"
int main(int argc, char **argv){
TextEdit t;
t.show();
return app.exec();
}
Maybe the difference is that I use selection() instead of selectedText().