Results 1 to 12 of 12

Thread: Get text from QTextEdit with CR LF

  1. #1
    Join Date
    Oct 2013
    Posts
    102
    Thanked 1 Time in 1 Post
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Get text from QTextEdit with CR LF

    Hi. I'm using QTextEdit to append some logs while my gui app is running. On closing it I want to append the content this to a log file. I do something like below:
    Qt Code:
    1. QDateTime cdt = QDateTime::currentDateTime();
    2. const QString LogFileName = QApplication::applicationDirPath() +
    3. "/" + cdt.toString("yyyy-MM-dd_hh-mm-ss") + "_log.txt";
    4.  
    5. QFile LogFile(LogFileName);
    6. LogFile.open(QIODevice::WriteOnly);
    7. QTextStream outstr(&LogFile);
    8. outstr << ui->textEdit->toPlainText();
    9. LogFile.close();
    To copy to clipboard, switch view to plain text mode 
    The content of the QTextEdit is written to my file, but I would like it with CR and LF just like it is displayed in QTextEdit. How can I do that, much thanks.


    Added after 4 minutes:


    Sorry, posted to early. The text is written with new lines just as it should be. I opened the file with windows notepad, which just displayed it wrong.
    Last edited by arcull; 9th September 2015 at 07:08.

  2. #2
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Get text from QTextEdit with CR LF

    Windows notepad is a very old application, it can only deal with so-called "Windows line endings".

    Btw, very bad choice of base path, this is a read-only location once you install the application.

    Cheers,
    _

  3. #3
    Join Date
    Oct 2013
    Posts
    102
    Thanked 1 Time in 1 Post
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Get text from QTextEdit with CR LF

    Quote Originally Posted by anda_skoa View Post
    Btw, very bad choice of base path, this is a read-only location once you install the application.
    Cheers,
    _
    Yes you are right, even though I thought this app will be some kind of portable version, so there will be no installer putting this under "C:\Program Files\...", but despite that maybe it's better to put is somewhere in current user folder. How would I get then this path by the way? Do you have some short sample code? Thanks again.

  4. #4
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Get text from QTextEdit with CR LF

    Try QStandardPaths::writableLocation() with maybe DataLocation.

    Cheers,
    _

  5. #5
    Join Date
    Oct 2013
    Posts
    102
    Thanked 1 Time in 1 Post
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Get text from QTextEdit with CR LF

    According to the docs, QStandardPaths::AppDataLocation looks the right one, but is missing in class, however I can get documents location successfully. What am I missing?
    Qt Code:
    1. QStringList sps = QStandardPaths::standardLocations(QStandardPaths::DocumentsLocation);
    2. foreach (QString s,sps)
    3. qDebug() << s;
    To copy to clipboard, switch view to plain text mode 

  6. #6
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Get text from QTextEdit with CR LF

    Are you on Qt5.4 or later?

    According to the docs AppDataLocation was added at 5.4

    Cheers,
    _

  7. #7
    Join Date
    Oct 2013
    Posts
    102
    Thanked 1 Time in 1 Post
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Get text from QTextEdit with CR LF

    Ahh grrrr, I'm on 5.1. What does it take to upgrade to 5.5, just reinstall or is it more complicated?

  8. #8
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Get text from QTextEdit with CR LF

    Quote Originally Posted by arcull View Post
    Ahh grrrr, I'm on 5.1.
    Could still use the data location and create a subdir with your application name.

    Quote Originally Posted by arcull View Post
    What does it take to upgrade to 5.5, just reinstall or is it more complicated?
    No, that should be all that it takes.

    Cheers,
    _

  9. #9
    Join Date
    Oct 2013
    Posts
    102
    Thanked 1 Time in 1 Post
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Get text from QTextEdit with CR LF

    Will use data location instead of reinstall, thanks.

  10. #10
    Join Date
    Sep 2015
    Posts
    12
    Thanked 3 Times in 3 Posts
    Qt products
    Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Get text from QTextEdit with CR LF

    I think the problem is with QTextStream. If you didn't use this you would end up with something closer to what is displayed in QTextEdit

    Qt Code:
    1. QTextEdit *text_edit;
    2. ...
    3. QString text_data = text_edit->toPlainText();
    4.  
    5. QFile file(output_file_name);
    6. file.write(text_data.toLatin1());
    7. ..
    8. file.close();
    To copy to clipboard, switch view to plain text mode 

  11. #11
    Join Date
    Oct 2013
    Posts
    102
    Thanked 1 Time in 1 Post
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Get text from QTextEdit with CR LF

    Quote Originally Posted by Rondog View Post
    I think the problem is with QTextStream. If you didn't use this you would end up with something closer to what is displayed in QTextEdit

    Qt Code:
    1. QTextEdit *text_edit;
    2. ...
    3. QString text_data = text_edit->toPlainText();
    4.  
    5. QFile file(output_file_name);
    6. file.write(text_data.toLatin1());
    7. ..
    8. file.close();
    To copy to clipboard, switch view to plain text mode 
    Thanks, but it was not the problem of code page. It was the windows notepad which just displays data some weird way. If I open the file with Notepad++ for instance it is ok.

  12. #12
    Join Date
    Sep 2015
    Posts
    12
    Thanked 3 Times in 3 Posts
    Qt products
    Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Get text from QTextEdit with CR LF

    Notepad does not display files properly unless they have the \r\n line ending. It has always done this. No other editor has this problem (including Wordpad).

    When you open the file you can add the 'text' flag and Qt will convert to the proper line terminator for you based on the OS. If it sees '\n' in the file data to write it will automatically be converted to '\r\n'.

    Qt Code:
    1. QFile file.open(QIODevice::WriteOnly | QIODevice::Text);
    To copy to clipboard, switch view to plain text mode 

    Optionally you could use QString::replace() to do this as well. I should be wrapped with an #ifdef OS preprocessor and (very important) you must make sure your input data does not already have \r\n for line terminators (it will become \r\r\n after 'replace').

    Qt Code:
    1. #ifdef Q_OS_WIN
    2. text_data.replace('\n','\r\n');
    3. #endif
    To copy to clipboard, switch view to plain text mode 
    .

Similar Threads

  1. How to manipulate a text in QTextEdit
    By Ryan111 in forum Newbie
    Replies: 3
    Last Post: 29th March 2015, 10:04
  2. how to get the text from QTextEdit
    By qt_user in forum Qt Programming
    Replies: 5
    Last Post: 22nd August 2011, 00:22
  3. QTextEdit::text()
    By hazardpeter in forum Newbie
    Replies: 2
    Last Post: 5th August 2009, 21:13
  4. QTextEdit + paste rich text as plain text only
    By Yong in forum Qt Programming
    Replies: 2
    Last Post: 6th February 2008, 16:45
  5. QTextEdit -> add Text
    By ape in forum Newbie
    Replies: 16
    Last Post: 19th December 2007, 14:59

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.