PDA

View Full Version : How to allow the user to access specific sections of a file in Qt



Nicklax
5th November 2016, 22:47
Hello there, I've coded a bestiary in C++ that allows users to enter in their own monsters and then access those monsters stats through a database but I am having trouble implementing the search functionality in Qt .


string search;
int stop(0);
getline (cin, search);
ifstream File ("creatures.txt");
if(File.is_open())
{
string line;
int curLine = 0;
while(getline(File, line))
{
curLine++;

if(line.find(search,0)!=string::npos)
{
for(int line_num = 0; line_num = curLine; line_num++)
{

if(getline(File, line) && (line_num == curLine))

{
stop++;
if (stop==16)
break;
cout<<line<< "\n";
}
else

break;

This code is supposed to allow the user to enter in the name of a monster and then return its stat block and basically i just want the user to do the same thing in Qt except they type the name into a line edit and the stat block appears in another box close by but i cannot figure out what the equivalent code would be to do this in qt so any help would be appreciated.

anda_skoa
6th November 2016, 12:47
Instead of line 3 you would get the text from the line edit.

Instead of line 24 you could call append() on a QTextEdit.

Cheers,
_

Nicklax
7th November 2016, 00:46
Thank you, could you also tell me what i would replace lines 9, 13 , and 18 with.

anda_skoa
7th November 2016, 08:25
Well, you don't necessarily have to change these lines at all, if you want to:
getline -> QTextStream::readLine()
string::find() -> QString::indexOf()

Cheers,
_