2

I wrote this program in Qt Creator but I'm not sure how to run it. Here is my code:

#include <QtCore/QCoreApplication>
using namespace std;
#include <iostream>

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

   string str;
   cin >> str;
   cout << " str is : " << str;
   return a.exec();
}

When I run it my console shows this:

Starting /home/hamed/qt programs/test3-build-desktop/test3...

...and nothing happens. What should I do?

Samuel Harmer
  • 4,264
  • 5
  • 33
  • 67
Hamed
  • 2,084
  • 6
  • 22
  • 42

3 Answers3

3

When copy pasting your code, it runs for me as expected (well, it doesn't terminate, but runs). Here is what I did in command line:

cd testproject
qmake -project
qmake
make
./testproject

As mentioned in the comment above, Qt itself is a library, so you are probably referring to some IDE when saying running it from "within Qt" - behaviour there is solely dependent on what IDE you are using.

Update:

From your message

Starting /home/hamed/qt programs/test3-build-desktop/test3...

I assume that you are using the QtCreator IDE, which does not allow you to enter things to console when running. I don't know whether you can get it to do so, but it works if you enter your project directory in console and use ./projectname. The building part mentioned above will be handled by QtCreator.

Another update:

Check out this thread for information on how to get it to work directly from QtCreator.

Community
  • 1
  • 1
nikolas
  • 8,707
  • 9
  • 50
  • 70
2

nothing happens!!

Your program is expecting an input, as you written here:

cin >> str;

what should i do?

Just type in something and press enter.

Michal Kottman
  • 16,375
  • 3
  • 47
  • 62
  • i know , but where shoud i type ! i use Qt creator and application output just writes Starting /home/hamed/qt programs/test3-build-desktop/test3... I'm new in Qt creator :( – Hamed Jan 19 '12 at 17:05
2

Add QTimer::singleShot(0, &a, SLOT(quit())); before the line return a.exec(); and don't forget to #include <QtCore/QTimer> (or you can make life easier and import the everything #include <QtCore>).

The a.exec() enters an event loop which waits for an event; normally in the form of user input with a graphical user interface. This however is a command line program and there isn't really a way for a user to send an event so it sits and waits forever. This is useful for server type applications but not what you're doing here. :)

(Note, this is one of many reasons your application might appear to be doing nothing. You may need to follow several of these answers before your program does what you expect)

Samuel Harmer
  • 4,264
  • 5
  • 33
  • 67
  • i recive this error : " main.cpp:12:5: error: ‘QTimer’ has not been declared " what shoud i import ? – Hamed Jan 19 '12 at 18:19
  • @hamed I've updated my answer in response to your comment, but you **really** should read [the Qt Creator manual](http://doc.qt.nokia.com/qtcreator-2.4/). The SO community will soon get tired of answering questions if you don't at least try to find the answer for yourself. The Qt documentation is astounding considering it's completely free! – Samuel Harmer Jan 19 '12 at 18:51
  • but it dosn't work again !!!, the first answer was correct, excuse for my poor questions :( – Hamed Jan 19 '12 at 20:44
  • @hamed You might want to spend 40 minutes on http://developer.qt.nokia.com/videos/watch/meet-qt-creator-ide – nikolas Jan 21 '12 at 10:51