Results 1 to 3 of 3

Thread: QApplication::clipboard not read textCursor cut

  1. #1
    Join Date
    May 2006
    Posts
    788
    Thanks
    49
    Thanked 48 Times in 46 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default 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...



    Qt Code:
    1. void TextEdit::IndentText(const QString text , int x )
    2. {
    3. const QChar Nline('\n');
    4.  
    5. QClipboard *netag = QApplication::clipboard();
    6. QString textclip = netag->text(QClipboard::Selection);
    7.  
    8. QStringList line = textclip.split(Nline);
    9. QStringList newline;
    10. for (int i = 0; i < line.size(); ++i) {
    11. QString onel = QString("\t%1").arg(line.at(i));
    12. newline.append(onel);
    13. }
    14.  
    15. qDebug() << "### line!!! " << line.size(); /* not correct */
    16. const QString reformat = newline.join(Nline);
    17.  
    18. netag->clear();
    19. netag->setText(reformat);
    20. paste();
    21. }
    22.  
    23. void TextEdit::keyPressEvent(QKeyEvent *e)
    24. {
    25.  
    26. if (e->key() == Qt::Key_Tab) {
    27.  
    28. QTextCursor tabact = textCursor();
    29. int stabstart = tabact.selectionStart();
    30. int stabstop = tabact.selectionEnd();
    31. const QString actual = tabact.selectedText();
    32. if (actual.size() > 3) {
    33. IndentText(actual,1);
    34. cut();
    35. qDebug() << "### stabstart " << stabstart;
    36. qDebug() << "### stabstop " << stabstart;
    37. return;
    38. }
    39. }
    To copy to clipboard, switch view to plain text mode 
    Attached Files Attached Files

  2. #2
    Join Date
    Jan 2006
    Location
    New Malden (near London)
    Posts
    32
    Thanks
    3
    Thanked 2 Times in 2 Posts
    Qt products
    Qt3 Qt4
    Platforms
    MacOS X Unix/X11

    Default 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:

    Qt Code:
    1. QString listString = EDITOR->textCursor().selectedText();
    2.  
    3. if( !listString.isEmpty() ) {
    4. QStringList stringList = listString.split( QChar( QChar::LineSeparator ) );
    5. for( int a = 0; a < stringList.count(); a++ ) {
    6. stringList[a].prepend( "<li>" );
    7. stringList[a].append( "</li>" );
    8. }
    9. listString = stringList.join( "\n" );
    10. listString.prepend( "<ul>" );
    11. listString.append( "</ul" );
    12. EDITOR->insertPlainText( listString );
    13. }
    To copy to clipboard, switch view to plain text mode 

    This also didn't work:

    Qt Code:
    1. QString listString = EDITOR->textCursor().selectedText();
    2.  
    3. if( !listString.isEmpty() ) {
    4. listString.prepend( "<ol><li>" );
    5. listString.replace( QChar( QChar::LineSeparator ),
    6. QString( "</li>%1<li>" ).arg( QChar( QChar::LineSeparator ) ) );
    7. listString.append( "</li></ol>" );
    8. EDITOR->insertPlainText( listString );
    9. }
    To copy to clipboard, switch view to plain text mode 

    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?

  3. #3
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QApplication::clipboard not read textCursor cut

    This seems to work:
    Qt Code:
    1. #include <QString>
    2. #include <QStringList>
    3. #include <QApplication>
    4. #include <QTextEdit>
    5. #include <QClipboard>
    6. #include <QTextDocumentFragment>
    7. #include <QMimeData>
    8.  
    9. class TextEdit : public QTextEdit {
    10. Q_OBJECT
    11. public:
    12. TextEdit(QWidget *parent=0) : QTextEdit(parent){
    13. connect(this, SIGNAL(selectionChanged()), SLOT(updateClipboard()));
    14. }
    15. private slots:
    16. void updateClipboard(){
    17. qDebug("CALLED");
    18. QClipboard *clip = QApplication::clipboard();
    19. QString text = textCursor().selection().toPlainText();
    20. QStringList slist = text.split("\n");
    21. QString result = "<ul>\n";
    22. foreach(QString elem, slist){
    23. result+="<li>"+elem.trimmed()+"</li>\n";
    24. }
    25. result+="</ul>";
    26. QMimeData *data = new QMimeData;
    27. data->setHtml(result);
    28. data->setText(result);
    29. clip->setMimeData(data);
    30. }
    31. };
    32.  
    33. #include "main.moc"
    34.  
    35. int main(int argc, char **argv){
    36. QApplication app(argc, argv);
    37. TextEdit t;
    38. t.show();
    39. return app.exec();
    40. }
    To copy to clipboard, switch view to plain text mode 

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

  4. The following 2 users say thank you to wysota for this useful post:

    Matt Smith (28th May 2007), patrik08 (28th May 2007)

Similar Threads

  1. QIODevice read()
    By ShaChris23 in forum Newbie
    Replies: 1
    Last Post: 3rd May 2007, 00:29
  2. How to Read and display BMP image using QT
    By agsrinivasan in forum Qt Programming
    Replies: 3
    Last Post: 29th January 2007, 07:14
  3. Increasing QProcess read performance
    By mcostalba in forum Qt Programming
    Replies: 9
    Last Post: 4th December 2006, 20:12
  4. How to read line from file
    By Krishnacins in forum Newbie
    Replies: 10
    Last Post: 1st June 2006, 23:14
  5. Replies: 13
    Last Post: 1st June 2006, 14:01

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.