简体   繁体   English

我应该学习 GTK+ 还是 GTKMM?

[英]Should I learn GTK+ or GTKMM?

I am a C# programmer who started using ubuntu about 2 years ago.我是一名 C# 程序员,大约 2 年前开始使用 ubuntu。 I'm wanting to learn GUI programming in either C or C++. I don't really like mono, it tends to crash on my system.我想在 C 或 C++ 学习 GUI 编程。我不太喜欢 mono,它在我的系统上容易崩溃。 I have a basic understanding of C++. I have never worked in C, but it looks cool.对C++基本了解,没在C工作过,不过看着爽Which toolkit should I learn/use?我应该学习/使用哪个工具包? Give Pro/Cons of each.给出每个的优点/缺点。 Thanks!谢谢!

I could be accused of bias since I do help contribute to gtkmm, but I was a user first, so... In any case, I would highly recommend gtkmm if you're comfortable with C++. Memory management is much easier with gtkmm than with GTK+ because reference-counted objects are managed automatically with smart pointers.我可能会被指责有偏见,因为我确实为 gtkmm 做出了贡献,但我首先是用户,所以......无论如何,如果你对 C++ 感到满意,我会强烈推荐 gtkmm。Memory 管理比 gtkmm 更容易使用 GTK+,因为引用计数对象是使用智能指针自动管理的。 You can also instantiate objects as auto variables (eg on the stack) and have their lifetime determined by their scope. So in practice, it's much easier to avoid memory leaks with gtkmm than with GTK+.您还可以将对象实例化为自动变量(例如在堆栈上),并让它们的生命周期由它们的 scope 决定。因此在实践中,使用 gtkmm 比使用 GTK+ 更容易避免 memory 泄漏。

Another huge advantage of gtkmm over GTK+ (in my opinion) is the use of a type-safe signals framework. gtkmm 相对于 GTK+ 的另一个巨大优势(在我看来)是使用了类型安全的信号框架。 In GTK+, you constantly need to pass things as void pointers and then cast them around to the type you think they should be.在 GTK+ 中,您经常需要将事物作为 void 指针传递,然后将它们强制转换为您认为它们应该是的类型。 In gtkmm, you dont need to do this, and can take advantage of the compiler enforcing type-safety on your signal handlers.在 gtkmm 中,您不需要这样做,并且可以利用编译器在您的信号处理程序上强制执行类型安全。

Another big advantage over C/GTK+ is the ease of deriving new classes.与 C/GTK+ 相比的另一大优势是易于派生新类。 In GTK+, you need to write a lot of boilerplate code and basically re-implement things that you get for free in C++ as part of the language (eg inheritance, constructors, destructors, etc).在 GTK+ 中,您需要编写大量样板代码并基本上重新实现您在 C++ 中免费获得的东西作为语言的一部分(例如 inheritance、构造函数、析构函数等)。 This is more tedious and error-prone.这更加繁琐且容易出错。

greyfade mentioned that gtkmm is incomplete, and he's right to a certain extent -- gtkmm does not cover absolutely everything in the GTK+ API (though it gets awfully close). greyfade 提到 gtkmm 是不完整的,在某种程度上他是对的——gtkmm 并没有完全涵盖 GTK+ API 中的所有内容(尽管它非常接近)。 But in practice this is not a problem because you can always use the C/GTK+ API directly from your gtkmm code.但实际上这不是问题,因为您始终可以直接从 gtkmm 代码中使用 C/GTK+ API。 This C compatibility is a huge advantage of C++ over something like C# or python bindings where you would have no alternatives if the binding didn't cover part of the API.这种 C 兼容性是 C++ 相对于 C# 或 python 绑定的巨大优势,如果绑定不涵盖 API 的一部分,您将别无选择。

The only real reasons to choose GTK+ over gtkmm (IMO) are that gtkmm has a little additional overhead since it is a wrapper on top of the C library (but this is generally just a single function call, which is going to have negligible impact), or if you hate or can't use C++.选择 GTK+ 而不是 gtkmm (IMO) 的唯一真正原因是 gtkmm 有一些额外的开销,因为它是 C 库之上的包装器(但这通常只是一个 function 调用,其影响可以忽略不计) , 或者如果你讨厌或不能使用 C++。

If you're a C# programmer, why don't you take a look at Vala ?如果您是 C# 程序员,为什么不看看Vala呢?

I use pygtk for most of my Linux GUI applications, but Python was simply too slow for the project I'm working on right now, so I was trying to pick one of GTK+ and GTKmm.我的大部分 Linux GUI 应用程序都使用 pygtk,但是 Python 对于我现在正在处理的项目来说太慢了,所以我试图选择 GTK+ 和 GTKmm 之一。 Then I met Vala.然后我遇到了瓦拉。

It's a pretty new language, and therefore documentation is pretty limited at the moment, but I think it has the best of both worlds: C# syntax with C speed.这是一种非常新的语言,因此目前文档非常有限,但我认为它具有两全其美的优点:C# 语法和 C 速度。

Since C++ is more familiar to you, you may find GTKmm to be a better fit, since you can use idioms like RAII.由于您更熟悉 C++,您可能会发现 GTKmm 更适合,因为您可以使用像 RAII 这样的习惯用法。 Unfortunately, GTKmm is a little incomplete and is missing a few of the lesser-used parts of GTK.不幸的是,GTKmm 有点不完整,缺少 GTK 中一些较少使用的部分。

GTK+ on its own, however, essentially exposes an object model similar to what you find in C++, but with only C functions.然而,GTK+ 本身本质上公开了一个 object model,类似于您在 C++ 中找到的,但只有 C 函数。 Things like construction and destruction in C++ are done explicitly in the C API and instances of widgets are handled via pointers exclusively. C++ 中的构建和销毁之类的事情在 C API 中明确完成,并且小部件的实例仅通过指针处理。

Try both and see which fits your project better.尝试两者,看看哪个更适合您的项目。

Like many have said, Gtkmm does provide you with good memory management, reference counted objects, etc. It does fall down in one department, though.就像许多人所说的那样,Gtkmm 确实为您提供了良好的 memory 管理、引用计数对象等。不过,它确实在一个部门中失败了。 Documentation.文档。 The whole of the Gtkmm project suffers from the "undocumentation" phenomena, where the posted (and reposted on 3rd party sites) documentation is simply a javadoc scan of the header files.整个 Gtkmm 项目都存在“未记录”现象,其中发布(并在第 3 方站点上重新发布)的文档只是 header 文件的 javadoc 扫描。

Just wanted you to know what you'd be getting into.只是想让你知道你会进入什么。 For instance, the Scrolled Window is one of the better documented classes in Gtkmm.例如, Scrolled Window是 Gtkmm 中记录较好的类之一。

I think the best way to go would be first learn gtkmm, After you are done with the basics of gtkmm.我认为达到 go 的最佳方法是先学习 gtkmm,在你完成 gtkmm 的基础知识之后。 GTK+ should be fairly straightforward to learn(provided you know C and are comfortable with pointers). GTK+ 应该相当容易学习(前提是您知道 C 并且熟悉指针)。

In case you don't know C, you can learn it quickly by reading The C Programming Language by Dennis Ritchie如果你不知道 C,你可以通过阅读 Dennis Ritchie 的 The C Programming Language 快速学习

I recommend you to learn gtkmm first because it is specially designed for C++, which is somewhat similar to C# since both are Object Oriented, so gtkmm will be relatively easy to learn first than GTK+.推荐大家先学gtkmm,因为它是专门为C++设计的,有点类似C#,都是面向Object的,所以gtkmm先学起来会比GTK+容易一些。

After gtkmm, you can move on to GTK+在 gtkmm 之后,您可以继续使用 GTK+

Most of the open source companies use GTK+ rather than gtkmm, so GTK+ is worthwile to learn!大多数开源公司使用GTK+而不是gtkmm,所以GTK+值得学习!

Have you looked at Qt?你看过Qt吗?
It's nice C++ design, cross platform and LGPL这是很好的 C++ 设计,跨平台和 LGPL

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

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