如何在 C++ 中构建图形用户界面?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1186017/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I build a graphical user interface in C++?
提问by waiwai933
All of my C++ programs so far have been using the command line interface and the only other language I have experience with is PHP which doesn't support GUIs.
到目前为止,我所有的 C++ 程序都在使用命令行界面,而我使用过的唯一其他语言是不支持 GUI 的 PHP。
Where do I start with graphical user interface programming in C++? How do I create one?
我从哪里开始使用 C++ 进行图形用户界面编程?我如何创建一个?
采纳答案by Breton
Essentially, an operating system's windowing system exposes some API calls that you can perform to do jobs like create a window, or put a button on the window. Basically, you get a suite of header files and you can call functions in those imported libraries, just like you'd do with stdlib and printf
.
本质上,操作系统的窗口系统公开了一些 API 调用,您可以执行这些调用来执行创建窗口或在窗口上放置按钮等工作。基本上,您可以获得一套头文件,并且可以调用这些导入库中的函数,就像使用 stdlib 和printf
.
Each operating system comes with its own GUI toolkit, suite of header files, and API calls, and their own way of doing things. There are also cross platform toolkits like GTK, Qt, and wxWidgetsthat help you build programs that work anywhere. They achieve this by having the same API calls on each platform, but a different implementation for those API functions that call down to the native OS API calls.
每个操作系统都有自己的 GUI 工具包、头文件套件和 API 调用,以及它们自己的工作方式。还有像GTK、Qt和wxWidgets这样的跨平台工具包,可帮助您构建可在任何地方运行的程序。他们通过在每个平台上使用相同的 API 调用来实现这一点,但对调用本机 OS API 调用的那些 API 函数使用不同的实现。
One thing they'll all have in common, which will be different from a CLI program, is something called an event loop. The basic idea there is somewhat complicated, and difficult to compress, but in essence it means that not a hell of a lot is going in in your main class/main function, except:
它们都有一个共同点,这与 CLI 程序不同,就是所谓的事件循环。那里的基本思想有些复杂,并且难以压缩,但本质上这意味着在您的主类/主函数中并没有太多内容,除了:
- check the event queue if there's any new events
- if there is, dispatch those events to appropriate handlers
- when you're done, yield control back to the operating system (usually with some kind of special "sleep" or "select" or "yield" function call)
- then the yield function will return when the operating system is done, and you have another go around the loop.
- 检查事件队列是否有任何新事件
- 如果有,将这些事件分派给适当的处理程序
- 完成后,将控制权交还给操作系统(通常使用某种特殊的“sleep”或“select”或“yield”函数调用)
- 那么yield函数将在操作系统完成时返回,并且您还有另一个循环。
There are plenty of resources about event based programming. If you have any experience with JavaScript, it's the same basic idea, except that you, the scripter have no access or control over the event loop itself, or what events there are, your only job is to write and register handlers.
有很多关于基于事件的编程的资源。如果您对 JavaScript 有任何经验,那么它的基本思想是相同的,除了您,脚本编写者无法访问或控制事件循环本身,或者有哪些事件,您唯一的工作就是编写和注册处理程序。
You should keep in mind that GUI programming is incredibly complicated and difficult, in general. If you have the option, it's actually much easier to just integrate an embedded webserver into your program and have an HTML/web based interface. The one exception that I've encountered is Apple's Cocoa+Xcode+interface builder + tutorials that make it easily the most approachable environment for people new to GUI programming that I've seen.
您应该记住,一般来说,GUI 编程极其复杂和困难。如果您可以选择,将嵌入式网络服务器集成到您的程序中并具有基于 HTML/Web 的界面实际上要容易得多。我遇到的一个例外是 Apple 的Cocoa+ Xcode+interface builder + 教程,这使它成为我见过的 GUI 编程新手最容易上手的环境。
回答by vog
There are plenty of free portable GUI libraries, each with its own strengths and weaknesses:
有很多免费的可移植 GUI 库,每个库都有自己的优点和缺点:
- Qt
- Dear ImGui
- GTKmm(based on GTK+)
- wxWidgets
- FLTK
- Ultimate++
- JUCE
- ...
Especially Qt has nice tutorialsand tools which help you getting started. Enjoy!
特别是Qt 有很好的教程和工具可以帮助您入门。享受!
Note, however, that you should avoid platform specificfunctionality such as the Win32 API or MFC. That ties you unnecessarily on a specific platform with almost no benefits.
但是请注意,您应该避免使用特定于平台的功能,例如 Win32 API 或 MFC。这将您不必要地绑定在特定平台上,几乎没有任何好处。
回答by Kirill V. Lyadvinsky
OS independent algorithm "Creating GUI applications in C++ in three steps":
独立于操作系统的算法“在 C++ 中三步创建 GUI 应用程序”:
Install Qt Creator
Create new project (Qt Widgets Application)
Build it.
创建新项目(Qt Widgets 应用程序)
建造它。
Congratulations, you've got your first GUI in C++.
恭喜,您已经获得了第一个 C++ 图形用户界面。
Now you're ready to read a lot of documentationto create something more complicate than "Hello world" GUI application.
现在您已准备好阅读大量文档来创建比“Hello world”GUI 应用程序更复杂的东西。
回答by KTC
Given the comment of "say Windows XP as an example", then your options are:
鉴于“以 Windows XP 为例”的评论,那么您的选择是:
Interact directly with the operating system via its API, which for Microsoft Windows is surprise surprise call Windows API. The definitive reference for the WinAPI is Microsoft's MSDN website. A popular online beginner tutorial for that is theForger's Win32 API Programming Tutorial. The classic book for that is Charles Petzold's Programming Windows, 5th Edition.
Use a platform (both in terms of OS and compiler) specific library such as MFC, which wraps the WinAPI into C++ class. The reference for that is again MSDN. A classic book for that is Jeff Prosise's Programming Windows with MFC, 2nd Edition. If you are using say CodeGear C++ Builder, then the option here is VCL.
Use a cross platform library such as GTK+(C++ wrapper: gtkmm), Qt, wxWidgets, or FLTKthat wrap the specific OS's API. The advantages with these are that in general, your program could been compiled for different OS without having to change the source codes. As have already been mentioned, they each have its own strengths and weaknesses. One consideration when selecting which one to use is its license. For the examples given, GTK+ & gtkmm is license under LGPL, Qt is under various licenses including proprietary option, wxWidgets is under its own wxWindows Licence (with a rename to wxWidgets Licence), and FLTK is under LGPL with exception. For reference, tutorial, and or books, refer to each one's website for details.
通过其 API 直接与操作系统交互,这对于 Microsoft Windows 来说是令人惊讶的意外调用Windows API。WinAPI 的权威参考是 Microsoft 的MSDN 网站。一个流行的在线初学者教程是theForger 的 Win32 API 编程教程。这方面的经典书籍是 Charles Petzold 的Windows 编程,第 5 版。
使用平台(在操作系统和编译器方面)特定的库,例如MFC,它将 WinAPI 包装到 C++ 类中。对此的参考再次是 MSDN。这方面的经典书籍是 Jeff Prosise 的Programming Windows with MFC, 2nd Edition。如果您使用的是 CodeGear C++ Builder,那么这里的选项是VCL。
使用跨平台库,例如GTK+(C++ 包装器:gtkmm)、Qt、wxWidgets或FLTK包装特定操作系统的 API。这些的优点是,通常,您的程序可以针对不同的操作系统编译,而无需更改源代码。如前所述,它们各有优缺点。选择使用哪个时的一个考虑因素是其许可证。对于给出的示例,GTK+ & gtkmm 是 LGPL 下的许可,Qt 下有各种许可,包括专有选项,wxWidgets 下有自己的 wxWindows 许可(重命名为 wxWidgets 许可),而 FLTK 下是 LGPL 下的例外。有关参考、教程和/或书籍,请参阅每个人的网站了解详细信息。
回答by Spidey
Since I've already been where you are right now, I think I can "answer" you.
既然我已经去过你现在的地方,我想我可以“回答”你。
The fact is there is no easy way to make a GUI. GUI's are highly dependent on platform and OS specific code, that's why you should start reading your target platform/OS documentation on window management APIs. The good thing is: there are plenty of libraries that address these limitations and abstract architecture differences into a single multi-platform API. Those suggested before, GTK and Qt, are some of these libraries.
事实是没有简单的方法来制作 GUI。GUI 高度依赖于平台和操作系统特定的代码,这就是为什么您应该开始阅读有关窗口管理 API 的目标平台/操作系统文档的原因。好消息是:有很多库可以将这些限制和抽象的架构差异解决到一个单一的多平台 API 中。之前建议的 GTK 和 Qt 是其中一些库。
But even these are a little toocomplicated, since lots of new concepts, data types, namespaces and classes are introduced, all at once. For this reason, they use to come bundled with some GUI WYSIWYG editor. They pretty much make programming software with GUIs possible.
但即使是这些也有点太复杂了,因为一次性引入了许多新概念、数据类型、命名空间和类。出于这个原因,它们通常与一些 GUI 所见即所得编辑器捆绑在一起。它们几乎使带有 GUI 的编程软件成为可能。
To sum it up, there are also non free "environments" for GUI development such as Visual Studio from Microsoft. For those with Delphiexperience backgrounds, Visual Studio may be more familiar. There are also free alternatives to the full Visual Studio environment supplied from Microsoft: Visual Studio Express, which is more than enough for starting on GUI development.
总而言之,也有非免费的 GUI 开发“环境”,例如 Microsoft 的 Visual Studio。对于有Delphi经验背景的人来说,Visual Studio 可能更熟悉。Microsoft 提供的完整 Visual Studio 环境还有免费的替代品:Visual Studio Express,对于开始 GUI 开发来说已经绰绰有余了。
回答by ZippyV
I found a website with a "simple" tutorial: http://www.winprog.org/tutorial/start.html
我找到了一个带有“简单”教程的网站:http: //www.winprog.org/tutorial/start.html
回答by Justicle
It's easy to create a .NET Windows GUI in C++.
在 C++ 中创建 .NET Windows GUI 很容易。
See the following tutorial from MSDN. You can download everything you need (Visual C++ Express) for free.
请参阅MSDN 中的以下教程。您可以下载你需要的一切(的Visual C ++ Express公司)免费。
Of course you tie yourself to .NET, but if you're just playing around or only need a Windows application you'll be fine (most people still have Windows...for now).
当然,您将自己与 .NET 联系在一起,但是如果您只是在玩弄或只需要一个 Windows 应用程序,那您就可以了(大多数人仍然拥有 Windows……目前)。