PDA

View Full Version : I want to get lines of text



newplayer
28th July 2008, 18:58
I wrote opening file and showing subject of file in textedit - function:


void MyWindow::Open ()
{
QString nameFile = QFileDialog::getOpenFileName (this);

if (!nameFile.isEmpty ())
{
QFile file (nameFile);

if (file.open (QFile::ReadOnly | QFile::Text))
{
QTextStream in (&file);
textEdit1 -> setPlainText (in.readAll ());
file.close();
}

}
}


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 ?

JimDaniel
28th July 2008, 19:06
QTextStream::readLine()

Learn to use the QtAssistant. Its a wonderful resource for questions like this.

newplayer
28th July 2008, 20:13
Thx, now I have problem with conversion:
invalid conversion from `char*' to `char'



void MyWindow::Open ()
{
QString nameFile = QFileDialog::getOpenFileName (this);

if (!nameFile.isEmpty ())
{
QFile file (nameFile);
char buff[255];

if (file.open (QFile::ReadOnly | QFile::Text))
{
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);
}


QTextStream in (&file);
textEdit1 -> setPlainText (in.readAll ());
file.close();
}

}
}

JimDaniel
28th July 2008, 20:41
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():



quint64 num_bytes_read = file.readLine(buff, 200);
for(int i = 0; i < num_bytes_read; i++)
{
v.push_back(buff[i]);
}


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

newplayer
28th July 2008, 20:56
I made it that:



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 !!!!!!
}



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 !!

JimDaniel
28th July 2008, 21:06
You are trying to append a std::string, but QTextEdit::append() takes a QString. As a fix you can say:



textEdit1->append(QString::fromStdString(v[i]));


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,



QStringList list;
while(!file.atEnd())
{
QString line = file.readLine(200);
list.push_back(line);
}

list.sort();

for(int i = 0; i < list.count(); i++)
{
textEdit1->append(list.at(i));
}

aamer4yu
29th July 2008, 07:10
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 ;)

newplayer
29th July 2008, 07:16
Thx you very much JimDaniel, I didn't know about 'QStringList' in Qt :D 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 ?

aamer4yu
29th July 2008, 07:35
What code are u using to append ?? You are probably appending '\n' too.

newplayer
29th July 2008, 07:39
I am using:

textEdit1->append(list.at(i));

aamer4yu
29th July 2008, 08:48
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 ?

newplayer
29th July 2008, 09:03
Thx - trimmed() helped me :)