Tuesday, August 21, 2012

Using QT with Eclipse - Hello world

I had found couple of articles on this, but they were not a fit and much more complicated than it really is (so, don't follow them if you are in my situation - Windows/Eclipse). The first looks like it was for Linux:

The second was for the Qt Creator environment (full Qt SDK installation, not like me, with Eclipse):

Truth is that things are actually very straightforward. If you create a project following the Wizard, you'll get a Qt hello_world window without typing a line of code. The 2nd link is useful to understand how things are actually working (open the different pieces of code and compare them to see what the wizard did for you automatically). Also, trying to use a C++ project straight from Qt and add the libraries manually is probably a big pain, based on this post:

Anyhow, so, how to do it? Basically, once Qt/Eclipse is installed (see my previous post on that) you should see on the top right of Eclipse a perspective for Qt:

Selecting the Qt C++ perspective and then File --> New we see:

If you don't press/select Qt C++, and you click in File --> New, you should at least see "Project..." option:

 And clicking it, you should get the option to create a Qt project.

 Either way, you can then just pick Qt Gui Project and follow the steps:
  1. The project name can have spaces in there.
  2. On the next dialog, try either one (QWidget, QMainWindow, ...). 
  3. No need to select any additional Qt modules (Core and Gui are enough)
  4. Click finish
Then go to project --> build project

After that, simply click the run (green "play" arrow) and the following window will show up (see note 1):

I selected MinGW gdb, click ok and wait for the application window to show up. Should be immediate. In this case:

That should be it! If you want to quickly play around, check the Project Explorer View, on the left of Eclipse.

If you double click in qt_another_test.ui you can see the shape of the window of your application. Clicking in the Qt C++ Widget Box tap, besides the Project Explorer view, you can add buttons, slide bars, etc... Click on the .cpp, .pro or .h to see the code. Now you can begin to make sense of the explanations on the tutorial listed above...

Good luck!

(1) If a message box shows up saying: "The selection can not be launched or there are not recent launches" right click on the project and select "Clean Project"

No comments:

Post a Comment