Results 1 to 13 of 13

Thread: Characters QTextEdit,QText Stream

  1. #1

    Default Characters QTextEdit,QText Stream

    Hi

    How to display the characters of constant width in QtextEdit window

    Now In the QtextEdit is below format

    USERno :0
    Family :0
    Model :4
    SubModel :1

    Hex format of text (all lines has the same number of Char)

    555345526e6f2020202020202020203a300d
    46616d696c792020202020202020203a300d
    4d6f64656c202020202020202020203a340d
    5375624d6f64656c202020202020203a310d

    QTextStream problem

    Why if I use if (!file.open(QIODevice::ReadOnly | QIODevice::Text))

    In QTextEdit is only one line
    USERno :0Family :0Model :4SubModel :1

    but at the end of each line there is 0x0d

    Platform : Windows 7

    Regards
    Artur

  2. #2
    Join Date
    Apr 2013
    Location
    Prague
    Posts
    258
    Thanks
    3
    Thanked 65 Times in 59 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Characters QTextEdit,QText Stream

    The problem seems to consist in the newline character. Your hex output corresponds to certain Macs versions or to some old systems like ZX Spectrum. In windows, the newline is 0x0D 0x0A (2 characters). Therefore, your file needs to contain:
    Qt Code:
    1. 555345526e6f2020202020202020203a300d0a
    2. 46616d696c792020202020202020203a300d0a
    3. 4d6f64656c202020202020202020203a340d0a
    4. 5375624d6f64656c202020202020203a310d0a
    To copy to clipboard, switch view to plain text mode 
    in windows, if you want 4 lines. Note that the newline is different in Linux. Even then, it is not clear, which evil has taken all the blanks in the file.

    If you want to process text files produced by any (most of) systems, you need to read in binary mode and parse the lines yourself. You must be prepared to
    (a) newlines:
    - 0x0D - Mac and some others
    - 0x0A - Linux
    - 0x0D 0x0A - windows, DOS
    (b) code pages

  3. #3

    Default Re: Characters QTextEdit,QText Stream

    If I use the following instruction : "if (!file.open(QIODevice::ReadOnly)"
    instead of "if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) "

    then I have four lines but I do not uderstand why.

  4. #4
    Join Date
    Apr 2013
    Location
    Prague
    Posts
    258
    Thanks
    3
    Thanked 65 Times in 59 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Characters QTextEdit,QText Stream

    The QIODevice::Text processes newlines. Because it finds none (in windows), you get one line. Without QIODevice::Text, you are reading in binary mode and the lines are made by QTextEdit, which seems to be content with 0x0D. In general, text files should be read with QIODevice::Text. Counting on "something else" doing the work is a bad idea.

  5. #5

    Default Re: Characters QTextEdit,QText Stream

    Another question

    How to take the number of line which the cursor is on.

    Regards
    Artur
    Last edited by arturs; 3rd April 2015 at 23:15.

  6. #6

    Default Re: Characters QTextEdit,QText Stream

    Hi

    My idea:
    signals:

    connect(ui->plainTextEdit,SIGNAL(cursorPositionChanged()),thi s,SLOT(sygnal()));

    slots:
    void signal (){
    QTextCursor cursor1 = QTextCursor(ui->plainTextEdit->textCursor());
    qDebug () << (cursor1.blockNumber()+1);
    }

    Above code works (If I move cursor then in debug window I see proper number of line ) but I am not sure if it is good way because when the signal is received It is created QTextCursor object.


    Each advice would be appreciated.

    Regards
    Artur

  7. #7
    Join Date
    Apr 2013
    Location
    Prague
    Posts
    258
    Thanks
    3
    Thanked 65 Times in 59 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Characters QTextEdit,QText Stream

    The idea works as long as "line" is the thing between carriage returns and the text does not contain specialties (URLs, images, etc.) Each carriage return creates a new block and blockNuber() increases. You need to be prepared on line breaking because the line is too long. Such "line" can occupy several lines but it is the same "line".

    As to the text cursor, IMO, it is not critical. But creating cursor1 is not needed. cursor1 is a copy of plainTextEdit->textCursor(), nothing more. You can:
    Qt Code:
    1. int line = ui->plainTextEdit->textCursor().blockNumber() + 1;
    2. qDebug () << line;
    To copy to clipboard, switch view to plain text mode 

  8. #8

    Default Re: Characters QTextEdit,QText Stream

    I need use function replace from QString Class .
    I would like to replace 'A' or 'B' for "AB"
    something like this

    replace ('A' || 'B' , "AB") but It does not work.
    I cannot replace first 'A' later 'B' because after first step there will be more 'B' character than before replacing 'A' for AB.

    How to do it ?

    Regards
    Artur

  9. #9
    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: Characters QTextEdit,QText Stream

    Use a regular expression.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  10. #10

    Default Re: Characters QTextEdit,QText Stream

    Thank you for information

    I have last problem with QText Edit I described it in first post.


    Improper format in QTextEdit:
    USERno :0
    Family :0
    Model :4
    SubModel :1

    Hex Code:
    555345526e6f2020202020202020203a300d0a
    46616d696c792020202020202020203a300d0a
    4d6f64656c202020202020202020203a340d0a
    5375624d6f64656c202020202020203a310d0a

    I would like to have last column in the same line.
    I do not have any idea how to do it.

    Regards
    Artur
    Last edited by arturs; 12th April 2015 at 15:42.

  11. #11
    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: Characters QTextEdit,QText Stream

    You need to set a fixed-width font.

    Cheers,
    _

  12. #12
    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: Characters QTextEdit,QText Stream

    Use a fixed width font.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  13. #13

    Default Re: Characters QTextEdit,QText Stream

    Hi

    I have another question How to change colour of one line in QPlainTextEdit.
    I found the following example:

    QTextCursor cursor(linie->textCursor());
    QTextCharFormat format;
    format.setForeground( QBrush( QColor( "red" ) ) );
    cursor.setCharFormat( format );

    but it seems to be quite complicated and it changes colour all text (when I want black text I have to write the same code once again )

    Regards
    Artur
    Last edited by arturs; 17th April 2015 at 18:35.

Similar Threads

  1. Update characters QTextCharFormat in QTextEdit
    By Suppaman in forum Qt Programming
    Replies: 2
    Last Post: 27th July 2013, 10:31
  2. QTextEdit not display new line characters
    By aruval3 in forum Qt Programming
    Replies: 2
    Last Post: 30th November 2011, 04:46
  3. QTextEdit from cout stream not displayed at once
    By nina1983 in forum Qt Programming
    Replies: 3
    Last Post: 8th June 2011, 23:18
  4. QTextEdit and Chinese characters on a Mac
    By jupi32000 in forum Qt Programming
    Replies: 0
    Last Post: 17th February 2010, 01:02
  5. read an xml file with Asian characters in QTextEdit
    By Boris Liao in forum Qt Programming
    Replies: 1
    Last Post: 23rd January 2010, 22:56

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.