QTextStream::readLine()
Learn to use the QtAssistant. Its a wonderful resource for questions like this.
QTextStream::readLine()
Learn to use the QtAssistant. Its a wonderful resource for questions like this.
Thx, now I have problem with conversion:
invalid conversion from `char*' to `char'
Qt Code:
void MyWindow::Open () { if (!nameFile.isEmpty ()) { char buff[255]; { std::vector<char> v; while (!file.atEnd() ) { file.readLine(buff, 200); v.push_back(buff); <-------there is a problem with conversion } sort( v.begin(), v.end() ); for( int i = 0; i < v.size(); i++ ) { buff=v[i]; textEdit1->append(buff); } textEdit1 -> setPlainText (in.readAll ()); file.close(); } } }To copy to clipboard, switch view to plain text mode
Last edited by jacek; 28th July 2008 at 22:12. Reason: changed [qtclass] to [code]
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:
quint64 num_bytes_read = file.readLine(buff, 200); for(int i = 0; i < num_bytes_read; i++) { v.push_back(buff[i]); }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.
I made it that:
Qt Code:
std::vector<std::string> v; std::string s; char buff[255]; while (!file.atEnd() ) { file.readLine(buff, 200); s=buff; v.push_back(s); } sort( v.begin(), v.end() ); int number=v.size(); for( int i = 0; i < number; i++ ) { textEdit1->append(v[i]); <------now there is a problem !!!!!! }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 22:13. Reason: changed [qtclass] to [code]
You are trying to append a std::string, but QTextEdit::append() takes a QString. As a fix you can say:
Qt Code:
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:
QStringList list; while(!file.atEnd()) { list.push_back(line); } list.sort(); for(int i = 0; i < list.count(); i++) { textEdit1->append(list.at(i)); }To copy to clipboard, switch view to plain text mode
Last edited by JimDaniel; 28th July 2008 at 21:14.
newplayer (29th July 2008)
True indeed ... Qt is CuteBut 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.![]()
newplayer (29th July 2008)
Thx you very much JimDaniel, I didn't know about 'QStringList' in QtThx !!
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 ?
What code are u using to append ?? You are probably appending '\n' too.
I am using:
textEdit1->append(list.at(i));
You shouldnt probably be getting that..
From the docs -
Hence \n must not be present in the string list.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.
Can you manually check the QString -> list.at(i) .... what does it contain ?
newplayer (29th July 2008)
Thx - trimmed() helped me![]()
Bookmarks