Results 1 to 15 of 15

Thread: QT Console app

  1. #1
    Join Date
    Jun 2010
    Posts
    7
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default QT Console app

    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 :

    Qt Code:
    1. while (std::getline(std::cin, buffer))
    2. {
    3. // do some work here NOTE: I can enter data in Eclipse IDE, but not in QT 4.
    4. }
    To copy to clipboard, switch view to plain text mode 

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

    Regards

    John

  2. #2
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,716
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: QT Console app

    if you just put a qDebug() message in the while loop, does it show up?
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  3. #3
    Join Date
    May 2010
    Location
    Romania
    Posts
    1,021
    Thanks
    62
    Thanked 260 Times in 246 Posts
    Qt products
    Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: QT Console app

    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"?

  4. #4
    Join Date
    Jun 2010
    Posts
    7
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: QT Console app

    Quote Originally Posted by high_flyer View Post
    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

  5. #5
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,716
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: QT Console app

    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.
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  6. #6
    Join Date
    Jun 2010
    Posts
    7
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: QT Console app

    Quote Originally Posted by high_flyer View Post
    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

  7. #7
    Join Date
    Jan 2006
    Location
    Belgium
    Posts
    1,938
    Thanked 268 Times in 268 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Wiki edits
    20

    Default Re: QT Console app

    This is basic C++

    Qt Code:
    1. char buffer[256];
    2.  
    3. while (buffer != "quit") {
    4. getline(buffer, 256);
    5. }
    To copy to clipboard, switch view to plain text mode 

    Is the above code something you want?

  8. #8
    Join Date
    Jun 2010
    Posts
    7
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: QT Console app

    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:

    Qt Code:
    1. std::string buffer;
    2. std::getline(std::cin, buffer); // I enter 'hello' in the 'Application Output' window then press return...
    3.  
    4. std::cout << "Received " << buffer << std::endl; // DOES NOT GET HERE. Where do I enter the text in QT to get this to work??
    To copy to clipboard, switch view to plain text mode 

    Regards

    John

  9. #9
    Join Date
    May 2010
    Location
    Romania
    Posts
    1,021
    Thanks
    62
    Thanked 260 Times in 246 Posts
    Qt products
    Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: QT Console app

    Post a simple project or complete/compilable code that reproduce the problem. Because the std::getline is working with Qt

  10. #10
    Join Date
    Jun 2010
    Posts
    7
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: QT Console app

    I know that std::getline is working in QT, but where do you enter the text??

    Qt Code:
    1. int main(int argc, char *argv[])
    2. {
    3. QCoreApplication a(argc, argv);
    4.  
    5. CClient client;
    6.  
    7. client.create();
    8.  
    9. std::cout << "input a number (-1 to quit) " << std::endl;
    10.  
    11. int nRetVal = 0;
    12. std::string buffer;
    13.  
    14. while (std::getline(std::cin, buffer)) // Where do I enter text within the console app??
    15. {
    16. if (buffer.compare("-1") == 0)
    17. {
    18. break;
    19. }
    20. client.Send(buffer.c_str());
    21. nRetVal = client.Recieve();
    22.  
    23. std::cout << "Received " << nRetVal << " from server." << std::endl << std::endl;
    24.  
    25. std::cout << "input a number (-1 to quit) ";*/
    26. }
    27. std::cout << "Client says bye bye!!" << std::endl;
    28. return a.exec();
    29. }
    To copy to clipboard, switch view to plain text mode 

  11. #11
    Join Date
    May 2010
    Location
    Romania
    Posts
    1,021
    Thanks
    62
    Thanked 260 Times in 246 Posts
    Qt products
    Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: QT Console app

    try it straight c++, and if you are on windows make sure you have this line: CONFIG += console in your project file
    Qt Code:
    1. int main(int argc, char *argv[])
    2. {
    3. //QCoreApplication a(argc, argv);
    4.  
    5. CClient client;
    6.  
    7. client.create();
    8.  
    9. std::cout << "input a number (-1 to quit) " << std::endl;
    10.  
    11. int nRetVal = 0;
    12. std::string buffer;
    13.  
    14. while (std::getline(std::cin, buffer)) // Where do I enter text within the console app??
    15. {
    16. if (buffer.compare("-1") == 0)
    17. {
    18. break;
    19. }
    20. client.Send(buffer.c_str());
    21. nRetVal = client.Recieve();
    22.  
    23. std::cout << "Received " << nRetVal << " from server." << std::endl << std::endl;
    24.  
    25. std::cout << "input a number (-1 to quit) ";*/
    26. }
    27. std::cout << "Client says bye bye!!" << std::endl;
    28. //return a.exec();
    29. return 0;
    30. }
    To copy to clipboard, switch view to plain text mode 

  12. #12
    Join Date
    Jun 2010
    Posts
    7
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: QT Console app

    Already tried that and my .pro file has CONFIG+=console.

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

  13. #13
    Join Date
    May 2010
    Location
    Romania
    Posts
    1,021
    Thanks
    62
    Thanked 260 Times in 246 Posts
    Qt products
    Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: QT Console app

    Quote Originally Posted by yorkshireflatcap View Post
    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.
    Last edited by Zlatomir; 22nd June 2010 at 09:38.

  14. #14
    Join Date
    Jun 2010
    Location
    France
    Posts
    7
    Thanks
    3
    Thanked 1 Time in 1 Post
    Qt products
    Qt4 Qt Jambi
    Platforms
    Unix/X11 Windows

    Default Re: QT Console app

    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 =)
    Last edited by fezvez; 22nd June 2010 at 13:25.

  15. The following user says thank you to fezvez for this useful post:

    yorkshireflatcap (22nd June 2010)

  16. #15
    Join Date
    Jun 2010
    Posts
    7
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11

    Thumbs up Re: QT Console app

    Thats it!! Thanks Fezvez... Thats worked a charm!!!!!
    Regards

    John
    Last edited by yorkshireflatcap; 22nd June 2010 at 21:01.

Similar Threads

  1. about console application
    By jirach_gag in forum Qt Programming
    Replies: 2
    Last Post: 5th January 2012, 11:39
  2. Console window?
    By waynew in forum Qt Programming
    Replies: 6
    Last Post: 22nd April 2010, 22:27
  3. No information on the console
    By starcontrol in forum Qt Programming
    Replies: 1
    Last Post: 8th April 2008, 13:39
  4. No console output in Mac OSX using Qt4
    By popoholic in forum Qt Programming
    Replies: 2
    Last Post: 26th September 2006, 01:36
  5. QT4 console apps
    By ucntcme in forum Newbie
    Replies: 5
    Last Post: 24th January 2006, 10:18

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.