简体   繁体   English

如何将Qt集成到现有的C ++项目中

[英]How to integrate Qt into an existing C++ project

I am in the process of teaching myself C++. 我正在自学C ++。 I have completed many tutorials, which have all been console projects and I've been using Visual Studio Community 2019 for those. 我已经完成了许多教程,这些教程都是控制台项目,并且我一直在使用Visual Studio Community 2019。 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. 解析的基础代码已经在工作,现在我想创建一个简单的UI来显示数据,大多数人似乎都建议Qt来完成这项工作。 So I also did some tutorials about Qt, which all feature the Qt Creator. 因此,我也做了一些有关Qt的教程,这些教程都具有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. 现在我不在乎Qt的创建者,我只是想在Visual Studio上继续我的项目,因为我已经很熟悉它了。 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. 我已经将Qt VS工具添加到我的Visual Studio中,并且已经完成了该线程中描述的步骤: 将Qt添加到现有的Visual Studio c ++项目中,但是现在我完全迷失了继续操作的方式,我可以似乎找不到任何进行方法的信息。

Is it possible to convert my project to a Qt application at all or do I have to start from scratch? 是否可以将我的项目完全转换为Qt应用程序,还是必须从头开始? How do I show Qt-generated windows without using the Qt creator? 如何在不使用Qt创建器的情况下显示Qt生成的窗口? 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... 我想我需要使用QMake工具来执行其中的一些操作,但是我什至无法弄清楚该怎么做...

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 . 下载并安装Qt Sounds like you have this wrapped up already. 听起来您已经把它包装好了。
  2. Include the parts of Qt you will use. 包括将要使用的Qt部分。 For example for a simple window, you would include #include <QWidget> and for a button, you would do #include <QPushButton> . 例如,对于一个简单的窗口,您将包含#include <QWidget> ,对于按钮,您将包含#include <QPushButton> You will always need to have do #include <QApplication> Here is a full list of possible things you can use. 您将始终需要执行#include <QApplication>这是您可以使用的所有可能事物的完整列表 Notice that Qt is much more than just a gui library. 请注意,Qt不仅仅是一个gui库。
  3. Create a main function like this: void main (int argc, char **argv ){} 创建一个像这样的main函数:void main(int argc,char ** argv){}
  4. Inside your main you must create the application instance like so: QApplication app(argc, argv); 在主体内部,您必须像这样创建应用程序实例: QApplication app(argc, argv);
  5. Instanciate widgets. 实例化小部件。 For eaxample: auto myWindow=new QWidget() ; auto myButton = new QPushButton(myWindow) ; 对于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(); 显示窗口: myWindow->show();
  7. Start eventloop: return app.exec(); 启动eventloop: return app.exec();
  8. Now your code is ready, try to build and link to Qt libs. 现在您的代码已经准备好,尝试构建并链接到Qt库。 Exactly how to do this in VS I am not sure (I am familiar with Linux mostly). 我不确定如何在VS中执行此操作(我最熟悉Linux)。

Like comments say, there are a gazillion ways to get up and running with Qt. 就像评论说的那样,有大量的方法来启动和运行Qt。 Any editor/IDE and any build system will probably get you there, however, I recommend you use QtCreator. 任何编辑器/ IDE和任何构建系统都可能会带您到那里,但是,我建议您使用QtCreator。 Why? 为什么? Because importing existing C++ code into QtCreator project will be simpler than setting up Qt inside existing VS project. 因为将现有C ++代码导入QtCreator项目将比在现有VS项目中设置Qt更为简单。 QtCreator is dead simple plug and play when it comes to Qt stuff, much more so than VS is. 当涉及到Qt时,QtCreator简直就是简单的即插即用,远比VS更是如此。

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. 对于一个QtCreator,它提供了许多示例项目,您可以单击并按“播放”,然后无需任何设置即可构建和运行它们。 Adapting from this is way easier than trying to manually set up a bunch of stuff in VS. 与尝试在VS中手动设置一堆东西相比,适应这一点要容易得多。

Examples in QtCreator: https://youtu.be/R6zWLfHIYJw?t=40 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();
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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