适用于现有 C++ 的 Qt UI项目
我已经编写了一个 C++ 程序,我想为其编写一个 GUI。我意识到 Qt 是一个很棒的工具,但是,Qt 有它自己的类,这让我很困惑。例如:Qt 有一个名为 QString 的类,而不是 String。
我想知道是否可以在 C++ 中混合 C++ 代码和 Qt 代码?
I have already written a C++ program and I would like to write a GUI for it. I realize Qt is a wonderful tool, however, Qt has it's own classes, which make me quite confused. eg: instead of String, Qt has a class named QString..
I am wondering if I can mix C++ code and Qt code in C++?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
是的,您可以非常轻松地混合使用 Qt 和 STL。
GUI 接受
QString
,但会默默地创建这些形式std::string
或char*
,从返回的QStrings
Qt 可以使用toStdString()
或toAscii()
进行转换。Qt 包含一组集合类,但您不必使用它们。
Qt 做得很好,看起来像现代 C++,有一些“额外的”来处理信号和槽事件机制,但这些对构建环境来说比对代码来说更复杂。
编辑:谢谢法案
Yes you can intermix Qt and STL very easily.
The GUI takes
QString
s but will silently create these formstd::string
orchar*
,QStrings
returned from Qt can be converted withtoStdString()
ortoAscii()
.Qt includes a set of collection classes but you don't have to use them.
Qt does a good job of looking like modern C++, there are a few 'extras' to handle the signal and slot event mechanism but these are more a complication to the build environment than to the code.
edit:thanks bill
我不建议将 std c++ 代码和 Qt 代码混合在一起。
比方说,你的 C++ 代码是 Qt 代码的逻辑,应该只用于 GUI。
使用 MVC/MVP 模式或类似方式,将逻辑和 UI 分开。
因此,将来使用/不使用 UI 来运行代码会更容易。
I wont recommend mixing std c++ code and Qt code together.
Let's say, your c++ code is the logic then the Qt code , should be just for GUI.
Using a MVC/MVP patterns or likewise, separate your logic and UI.
Thus,it would be easier in the future to be able to run your code with/without using a UI.
简而言之,是的,你可以!例如,QString 本质上是一个字符串,并且有一个 toAscii() 方法,因此您可以轻松地将 Qt 用于您的项目。尽管您可能会在将 Qt 集成到 Visual Studio 中时遇到一些问题(假设您正在使用它),但您将能够混合使用 C++ 代码和 Qt 代码(C++ 中的代码)
in short, yes you can! for example, a QString is essentially a string and has a toAscii() method, so you can easily use Qt for your projects. Although you MIGHT run into some problems integrating Qt into visual studio (assuming you are using it), you will be able to mix c++ code and Qt code(which is in c++)
是的!您可以在 C++ 代码中使用 Qt。 Qt 主要用于 GUI,但它对于 Socket、容器、网络等来说都是很好的类。
您可以自由使用 STL 和其他普通类或 Qt 为您提供的类。
最重要的一点是,如果您在主编译之前在代码中使用 Qt,则应该进行元编译以使其为编译器做好准备。
您应该注意的一件事是,Qt 不是标准的,这意味着虽然它支持 Windows、Linux 和 Mac OS,但它无法在其他平台上使用。
yeah! Qt is something you can use in your C++ code. Qt is mainly for GUI but it is good classes for Socket, Containers, Network, ...
You are free to use STL and other normal classes or the ones Qt offers you.
The biggest point is that if you use Qt in your code before the main Compile you should do a meta compile to make it ready for you compiler.
One thing you should notice is that Qt is not standard it means although it supports Windows, Linux and Mac OS, it won't be usable on other platforms.
它们可以一起使用。
C++ 有着悠久的历史,STL 的实现和支持也随着该语言的发展而发展。我认为前 Trolltech 团队认为他们对 STL 实现的质量和可用性没有足够的控制,以致 Qt 库依赖于它。结果是Qt有自己的QTL、QtAlgorithms头和QString。
他们注意使它们与 STL 兼容:
就个人而言,我与 QString 相比,它比 std::string 更易于使用且功能更强大。
They can be used together.
C++ has a long history and the STL implementation and support have evolved with the language. I think that the ex-Trolltech team decided that they didn't have enough control over the quality and availability of STL implementations to make the Qt library depend on it. The result is that Qt has its own QTL, QtAlgorithms header and QString.
They have taken care to make them compatible with the STL:
Personally, I like QString more than std::string, it is easier to use and more powerful.
是的,你可以。
我实际上为一组基于 C 的函数制作了一个 Qt-GUI 包装器。 Qt 集成没有问题,只有低级 C (会泄漏内存......)。
请参阅此处:http://code.google.com/p/qhocr/
Yes you can.
I actually made a Qt-GUI-wrapper for a set of C based functions. No problems with the Qt integration, only the low level C (which leaks memory...).
See here: http://code.google.com/p/qhocr/