简体   繁体   中英

How to integrate Qt into an existing C++ project

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?

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) ; 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();
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM