Results 1 to 12 of 12

Thread: I want to get lines of text

  1. #1
    Join Date
    Jul 2008
    Posts
    25
    Thanks
    4
    Qt products
    Qt4

    Default I want to get lines of text

    I wrote opening file and showing subject of file in textedit - function:
    Qt Code:
    1. void MyWindow::Open ()
    2. {
    3. QString nameFile = QFileDialog::getOpenFileName (this);
    4.  
    5. if (!nameFile.isEmpty ())
    6. {
    7. QFile file (nameFile);
    8.  
    9. if (file.open (QFile::ReadOnly | QFile::Text))
    10. {
    11. QTextStream in (&file);
    12. textEdit1 -> setPlainText (in.readAll ());
    13. file.close();
    14. }
    15.  
    16. }
    17. }
    To copy to clipboard, switch view to plain text mode 

    But I want to take one line of text. How can I take one line of text ? Because I want to sort lines of text, for example - first:

    bbb bbb aaa
    bbb aaa cc
    aaa gg

    Then it will be:

    aaa gg
    bbb aaa cc
    bbb bbb aaa

    I need to take one line of text - here it is "bbb bbb aaa", then I will trow it into vector (from library STL from c++), then I will use function sort() (from library STL from c++) and I will get sorted lines of text.

    BUT HOW CAN I TAKE ONE LINE OF TEXT FROM FILE ?
    Last edited by jacek; 28th July 2008 at 23:12. Reason: changed [qtclass] to [code]

  2. #2
    Join Date
    Sep 2007
    Location
    Rome, GA
    Posts
    199
    Thanks
    14
    Thanked 41 Times in 35 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default Re: I want to get lines of text

    QTextStream::readLine()
    Learn to use the QtAssistant. Its a wonderful resource for questions like this.

  3. #3
    Join Date
    Jul 2008
    Posts
    25
    Thanks
    4
    Qt products
    Qt4

    Default Re: I want to get lines of text

    Thx, now I have problem with conversion:
    invalid conversion from `char*' to `char'

    Qt Code:
    1. void MyWindow::Open ()
    2. {
    3. QString nameFile = QFileDialog::getOpenFileName (this);
    4.  
    5. if (!nameFile.isEmpty ())
    6. {
    7. QFile file (nameFile);
    8. char buff[255];
    9.  
    10. if (file.open (QFile::ReadOnly | QFile::Text))
    11. {
    12. std::vector<char> v;
    13.  
    14. while (!file.atEnd() )
    15. {
    16. file.readLine(buff, 200);
    17. v.push_back(buff); <-------there is a problem with conversion
    18. }
    19.  
    20. sort( v.begin(), v.end() );
    21.  
    22. for( int i = 0; i < v.size(); i++ )
    23. {
    24. buff=v[i];
    25. textEdit1->append(buff);
    26. }
    27.  
    28.  
    29. QTextStream in (&file);
    30. textEdit1 -> setPlainText (in.readAll ());
    31. file.close();
    32. }
    33.  
    34. }
    35. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by jacek; 28th July 2008 at 23:12. Reason: changed [qtclass] to [code]

  4. #4
    Join Date
    Sep 2007
    Location
    Rome, GA
    Posts
    199
    Thanks
    14
    Thanked 41 Times in 35 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default Re: I want to get lines of text

    Well, you create a vector of chars, but you are pushing back an array of chars. That's not going to work. If you want to keep the same structure in place, you'd need to iterate over the returned char * pushing back the individual chars. You can see how many bytes were read by the return value of readLine():

    Qt Code:
    1. quint64 num_bytes_read = file.readLine(buff, 200);
    2. for(int i = 0; i < num_bytes_read; i++)
    3. {
    4. v.push_back(buff[i]);
    5. }
    To copy to clipboard, switch view to plain text mode 

    That said, there's probably an easier way to get things done, using Qt data structures and QChar.

  5. #5
    Join Date
    Jul 2008
    Posts
    25
    Thanks
    4
    Qt products
    Qt4

    Default Re: I want to get lines of text

    I made it that:

    Qt Code:
    1. std::vector<std::string> v;
    2. std::string s;
    3. char buff[255];
    4.  
    5. while (!file.atEnd() )
    6. {
    7. file.readLine(buff, 200);
    8. s=buff;
    9. v.push_back(s);
    10. }
    11.  
    12. sort( v.begin(), v.end() );
    13.  
    14. int number=v.size();
    15.  
    16. for( int i = 0; i < number; i++ )
    17. {
    18. textEdit1->append(v[i]); <------now there is a problem !!!!!!
    19. }
    To copy to clipboard, switch view to plain text mode 


    no matching function for call to `QTextEdit::append(std::basic_string<char, std::char_traits<char>, std::allocator<char> >&)'
    candidates are: void QTextEdit::append(const QString&)

    Wrrrrr Qt !!
    Last edited by jacek; 28th July 2008 at 23:13. Reason: changed [qtclass] to [code]

  6. #6
    Join Date
    Sep 2007
    Location
    Rome, GA
    Posts
    199
    Thanks
    14
    Thanked 41 Times in 35 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default Re: I want to get lines of text

    You are trying to append a std::string, but QTextEdit::append() takes a QString. As a fix you can say:

    Qt Code:
    1. textEdit1->append(QString::fromStdString(v[i]));
    To copy to clipboard, switch view to plain text mode 

    But Qt has its own data structures and string class. I think you'll be happier in the long run if you learn to use those. They are easier anyway.

    EDIT:

    For example,

    Qt Code:
    1. while(!file.atEnd())
    2. {
    3. QString line = file.readLine(200);
    4. list.push_back(line);
    5. }
    6.  
    7. list.sort();
    8.  
    9. for(int i = 0; i < list.count(); i++)
    10. {
    11. textEdit1->append(list.at(i));
    12. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by JimDaniel; 28th July 2008 at 22:14.

  7. The following user says thank you to JimDaniel for this useful post:

    newplayer (29th July 2008)

  8. #7
    Join Date
    Oct 2006
    Location
    New Delhi, India
    Posts
    2,467
    Thanks
    8
    Thanked 334 Times in 317 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: I want to get lines of text

    But Qt has its own data structures and string class. I think you'll be happier in the long run if you learn to use those. They are easier anyway.
    True indeed ... Qt is Cute

  9. The following user says thank you to aamer4yu for this useful post:

    newplayer (29th July 2008)

  10. #8
    Join Date
    Jul 2008
    Posts
    25
    Thanks
    4
    Qt products
    Qt4

    Default Re: I want to get lines of text

    Thx you very much JimDaniel, I didn't know about 'QStringList' in Qt Thx !!


    I have a little problem with ENTER's ( '\n' ) and function 'append()'. When I have for example:
    a
    b
    c
    d

    After used 'append' I see in my textbox:
    a

    b

    c

    d

    Why ? Is it any function in Qt which resolve this problem ?

  11. #9
    Join Date
    Oct 2006
    Location
    New Delhi, India
    Posts
    2,467
    Thanks
    8
    Thanked 334 Times in 317 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: I want to get lines of text

    What code are u using to append ?? You are probably appending '\n' too.

  12. #10
    Join Date
    Jul 2008
    Posts
    25
    Thanks
    4
    Qt products
    Qt4

    Default Re: I want to get lines of text


  13. #11
    Join Date
    Oct 2006
    Location
    New Delhi, India
    Posts
    2,467
    Thanks
    8
    Thanked 334 Times in 317 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: I want to get lines of text

    You shouldnt probably be getting that..
    From the docs -
    QString QTextStream::readLine ( qint64 maxlen = 0 )
    Reads one line of text from the stream, and returns it as a QString. The maximum allowed line length is set to maxlen. If the stream contains lines longer than this, then the lines will be split after maxlen characters and returned in parts.
    If maxlen is 0, the lines can be of any length. A common value for maxlen is 75.
    The returned line has no trailing end-of-line characters ("\n" or "\r\n"), so calling QString::trimmed() is unnecessary.
    Hence \n must not be present in the string list.
    Can you manually check the QString -> list.at(i) .... what does it contain ?

  14. The following user says thank you to aamer4yu for this useful post:

    newplayer (29th July 2008)

  15. #12
    Join Date
    Jul 2008
    Posts
    25
    Thanks
    4
    Qt products
    Qt4

    Default Re: I want to get lines of text

    Thx - trimmed() helped me

Similar Threads

  1. Unhandled exception in qatomic
    By NewGuy in forum Qt Programming
    Replies: 14
    Last Post: 23rd July 2013, 10:49
  2. Replies: 4
    Last Post: 25th May 2008, 21:01
  3. Match the text beetween two string
    By dreamer in forum Qt Programming
    Replies: 4
    Last Post: 20th May 2008, 15:48
  4. QTextEdit slow to insert text
    By thomaspu in forum Qt Programming
    Replies: 4
    Last Post: 10th January 2008, 13:05
  5. Editable text in QGraphicsView
    By wysota in forum Qt Programming
    Replies: 8
    Last Post: 24th February 2007, 16:30

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.