4

I am in the process of teaching myself C++. I have completed many tutorials, which have all been console projects and I've been using Visual Studio Community 2019 for those. Now I started to work on a project for a colleague that should parse his inbox for specific mails and summarize their contents.

The underlying code for the parsing is already working, now I want to create a simple UI to show the data and most people seem to suggest Qt for that job. So I also did some tutorials about Qt, which all feature the Qt Creator.

Now I don't mind the Qt creator, I just would like to continue my project on Visual Studio since I'm already quite familiar with it. I have added the Qt VS Tools to my Visual Studio and I have done the steps described in this thread: add Qt to existing Visual Studio c++ project But now I'm at a complete loss as to how I need to continue and I can't seem to find anything on how to proceed.

Is it possible to convert my project to a Qt application at all or do I have to start from scratch? How do I show Qt-generated windows without using the Qt creator? I think I need to use the QMake tool to do some of that, but I can't even figure out how I would do that...

Can anyone give me a detailed guide on how I should proceed or link me to one?

Estelyen
  • 55
  • 1
  • 7
  • Have you installed the QT library itself? – Tom May 21 '19 at 15:06
  • "I think I need to use the QMake tool" - No. You don't need to use qmake or qtcreator or anything like that to build a program that uses Qt. All you need is a text editor and a compiler/linker. Visual Studio can work fine, so can notepad and a cmd shell. I personally use Emacs for editing, g++ as my compiler and CMake + ninja as build system to drive the compilation process of a Qt based project, for example. – Jesper Juhl May 21 '19 at 15:19
  • @Tom Yes I have, that step came with the tutorials I did (Qt for Beginners on the Qt Wiki to be exact). I also included it in my project as per the link in my original post. I just don't know where to go from this point, the next step in my mind would be to #inlcude some of the Qt classes, but VS complains that it cannot open for instance... – Estelyen May 21 '19 at 15:56
  • I highly recommend you use qmake, this will make it easier to port the application / gui to linux also afterwards, and it's a LOT easier than make or normal visual studio build mechanics. You need the latest visual studio update, the VS QT Plugins, then you also need QT itself, which supplies all the base utilities like qmake etc. My advice would be develop the gui separately, with printfs in the button functions, then hook the existing code into those function calls, after the gui is complete. – Owl May 21 '19 at 15:59

1 Answers1

2

You need to do the following:

  1. Download and Install Qt. Sounds like you have this wrapped up already.
  2. Include the parts of Qt you will use. For example for a simple window, you would include #include <QWidget> and for a button, you would do #include <QPushButton>. You will always need to have do #include <QApplication> Here is a full list of possible things you can use. Notice that Qt is much more than just a gui library.
  3. Create a main function like this: void main (int argc, char **argv ){}
  4. Inside your main you must create the application instance like so: QApplication app(argc, argv);
  5. Instanciate widgets. For eaxample: auto myWindow=new QWidget() ; auto myButton = new QPushButton(myWindow) ;
  6. Show your window: myWindow->show();
  7. Start eventloop: return app.exec();
  8. Now your code is ready, try to build and link to Qt libs. Exactly how to do this in VS I am not sure (I am familiar with Linux mostly).

Like comments say, there are a gazillion ways to get up and running with Qt. Any editor/IDE and any build system will probably get you there, however, I recommend you use QtCreator. Why? Because importing existing C++ code into QtCreator project will be simpler than setting up Qt inside existing VS project. QtCreator is dead simple plug and play when it comes to Qt stuff, much more so than VS is.

For one QtCreator comes with a bunch of example project out of the box that you can just click on and press "play" and it will build and run them without any set up. Adapting from this is way easier than trying to manually set up a bunch of stuff in VS.

Examples in QtCreator: https://youtu.be/R6zWLfHIYJw?t=40

Full example just showing a single button:

#include <QApplication>
#include <QPushButton>

int main(int argc, char **argv)
{
 QApplication app (argc, argv);

 QPushButton button ("Hello world !");
 button.show();

 return app.exec();
}
Mr. Developerdude
  • 9,118
  • 10
  • 57
  • 95
  • Thank you Lennart, that answer helped me a lot. I already surmised most of it from the Qt tutorial on the wiki, but it's good to see that I'm on the right path. Sadly, I still can't get it to work: For some arcane reason, my Visual Studio can open neither nor despite the fact that I added the include folder of Qt to VSs additional include directories and its lib folder to the Linkers additional library directories. There has to be some step that I missed but I can't figure out what it might be... – Estelyen May 22 '19 at 17:36