PDA

View Full Version : QT Console app



yorkshireflatcap
21st June 2010, 15:37
Hi All,

Apologies if this question has been asked a thousand times before but I've been scouring the inter-web to no avail. How do you get getline/cin to work? In the Application Output window, I have created a prompt that allows me to enter some data. When I press return to complete my data input, my application does not respond :



while (std::getline(std::cin, buffer))
{
// do some work here NOTE: I can enter data in Eclipse IDE, but not in QT 4.
}


Any ideas as to how I can enter data in the application console?

Regards

John

high_flyer
21st June 2010, 16:09
if you just put a qDebug() message in the while loop, does it show up?

Zlatomir
21st June 2010, 16:18
The problem is not with the while loop itself
Is somewhere else, maybe in the code within. Use the debugger to see where it fails.

Are you using that while loop as the "event loop"?

yorkshireflatcap
21st June 2010, 16:45
if you just put a qDebug() message in the while loop, does it show up?

Thanks for the reply. I don't have a problem with cout as I am able to display messages in the Application Output window, but I am having problems with while (std::getline(std::cin, buffer))

In Eclipse, I am able to enter data, press return, and the code will process my input, but with QT, I enter data in the Application Output window, press return and nothing happens.

ASIDE:my console application works in Eclipse.

Cheers

John

high_flyer
21st June 2010, 17:10
why don't you try what I asked you?
comment out whats in your while loop and put a qDebug() message instead.
that would be a good first step.

yorkshireflatcap
21st June 2010, 17:58
why don't you try what I asked you?
comment out whats in your while loop and put a qDebug() message instead.
that would be a good first step.

It does not get past while (std::getline(std::cin, buffer)) as it's waiting for input. so putting a qDebug() will not achieve anything. I just want to be able to enter data in the Application Output window, but it's not happening. I enter abc press return, and it does not return from std::getline(std::cin, buffer)

Regards

tbscope
21st June 2010, 18:40
This is basic C++


char buffer[256];

while (buffer != "quit") {
getline(buffer, 256);
}

Is the above code something you want?

yorkshireflatcap
21st June 2010, 19:54
Hi,

the code I've got is fine, it works in Eclipse (and I have 15 years C++). The problem is getline is not returning after pressing return. I did something similar to the code that you posted. All I want to do is enter a string (in the console Application Output window), press return, and hey presto it continues with executing the rest of the code:



std::string buffer;
std::getline(std::cin, buffer); // I enter 'hello' in the 'Application Output' window then press return...

std::cout << "Received " << buffer << std::endl; // DOES NOT GET HERE. Where do I enter the text in QT to get this to work??


Regards

John

Zlatomir
21st June 2010, 20:14
Post a simple project or complete/compilable code that reproduce the problem. Because the std::getline is working with Qt

yorkshireflatcap
21st June 2010, 20:25
I know that std::getline is working in QT, but where do you enter the text??



int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);

CClient client;

client.create();

std::cout << "input a number (-1 to quit) " << std::endl;

int nRetVal = 0;
std::string buffer;

while (std::getline(std::cin, buffer)) // Where do I enter text within the console app??
{
if (buffer.compare("-1") == 0)
{
break;
}
client.Send(buffer.c_str());
nRetVal = client.Recieve();

std::cout << "Received " << nRetVal << " from server." << std::endl << std::endl;

std::cout << "input a number (-1 to quit) ";*/
}
std::cout << "Client says bye bye!!" << std::endl;
return a.exec();
}

Zlatomir
21st June 2010, 20:42
try it straight c++, and if you are on windows make sure you have this line: CONFIG += console in your project file


int main(int argc, char *argv[])
{
//QCoreApplication a(argc, argv);

CClient client;

client.create();

std::cout << "input a number (-1 to quit) " << std::endl;

int nRetVal = 0;
std::string buffer;

while (std::getline(std::cin, buffer)) // Where do I enter text within the console app??
{
if (buffer.compare("-1") == 0)
{
break;
}
client.Send(buffer.c_str());
nRetVal = client.Recieve();

std::cout << "Received " << nRetVal << " from server." << std::endl << std::endl;

std::cout << "input a number (-1 to quit) ";*/
}
std::cout << "Client says bye bye!!" << std::endl;
//return a.exec();
return 0;
}

yorkshireflatcap
21st June 2010, 20:53
Already tried that and my .pro file has CONFIG+=console.

Where do you enter text within the QT IDE for console apps?

Zlatomir
22nd June 2010, 09:31
Where do you enter text within the QT IDE for console apps?

Go in the "Project" part on the left of QtCreator and then in the "Run Settings" tab, click on Show Details button, and there is a Arguments line edit. (I think this is what you meant by that question, sorry if i misunderstood)

Anyway, my opinion is that your problem has nothing to do with arguments, it's somewhere else in the code, so use the debugger and see where your code fails.

fezvez
22nd June 2010, 12:53
Hi there!

I think I understand your problem.

Go to Project (left menu) then go to "Run Settings"

There should be a checkbox named "Run in terminal", just check it.

I've tested this in Qt Creator 1.3, and it works =)

yorkshireflatcap
22nd June 2010, 20:57
Thats it!! Thanks Fezvez... Thats worked a charm!!!!!
Regards

John