33

The problem I have, is that most of the C++ books I read spend almost forever on syntax and the basics of the language, e.g. for and loops while, arrays, lists, pointers, etc.

But they never seem to build anything that is simple enough to use for learning, yet practical enough to get you to understand the philosophy and power of the language.

Then I stumbled upon QT which is an amazing library!

But working through the demos they have, it seems like I am now in the reverse dilemma. I feel like the rich man's son driving round in a sports car subsidized by the father. Like I could build fantastic software, but have no clue what's going on under the hood.

As an example of my dilemma take the task of building a simple web browser. In pure C++, I wouldn't even know where to start, yet with the Qt library it can be done within a few lines on code.

I am not complaining about this. I am just wondering how to fill the knowledge void between the basic structure of the language and the high level interface that the Qt framework provides?

7
  • I'm not familiar with programming in Qt. Does it actually introduce any new syntax or change the language, or is it just some very sophisticated libraries? Jan 6, 2012 at 20:47
  • @FrustratedWithFormsDesigner It doesn't QT is the GUI Framework qt.nokia.com if you're interested
    – Karlson
    Jan 6, 2012 at 20:50
  • Qt also offers an alternative to standard classes and containers (e.g. QString, QList, and so on) and a lot of other facilities. Some of them I find even more intuitive than the standard ones. I think it is a nice programming environment, but I would advise learning the core C++ language separately.
    – Giorgio
    Jan 6, 2012 at 20:58
  • 3
    If you want to learn how Qt is implemented, just download and look at its source code.
    – user16764
    Jan 7, 2012 at 0:47
  • 1
    @Karlson Qt does add some things to the language, hence the need for the moc aka metaobject compiler. This adds some reflection and forms the basis of Qt's signals/slots system. Jan 7, 2012 at 14:13

8 Answers 8

11

Do you want to know how stepping on the accelerator makes the car go faster, or do you only care that stepping on the accelerator makes the car go faster?

You are seeing the benefit to black box programming, which is a great way to design a system when all the boxes work. Someone has to make the black boxes though and if you want to be that guy/girl then you need to know more about the language than the guy using the box.

There are jobs that are good jobs in each style, so its up to you what you want to program. IMO you would be doing yourself a disservice though if you didn't put forth the effort to peel back some of the abstraction QT is giving you eventually.

1
  • 2
    "IMO you would be doing yourself a disservice though if you didn't put forth the effort to peel back some of the abstraction QT is giving you eventually." Your right and thats the gut feeling I have
    – user866190
    Jan 6, 2012 at 22:56
19

The most damage you will be doing to yourself, if you want to put it that way, is that you will not be learning to use the standard C++ data structures, iterators, algorithms and strings at all. Qt has libraries of its own for all those purposes, and you are all but forced to use them instead of standard C++ entities because Qt APIs only accept data structures of its own.

One could argue that learning to use one algorithms library after mastering other is a trivial task. Before going to an interview where the interviewers expect you to master C++, make sure you deal with that triviality beforehand.

4
  • 4
    Agreed- Qt uses an entirely different toolset altogether to Standard C++.
    – DeadMG
    Jan 6, 2012 at 22:37
  • In our company there was a long debate whether one should use the standard containers and iterators or the Qt ones and we haven't come up with a solution yet: we ended up using both, even though we do not mix the two styles in a given module. I have the feeling that moving from Qt to standard is conceptually very easy, even though it will take some time.
    – Giorgio
    Jan 7, 2012 at 13:17
  • 9
    I use the standard containers and algorithms inside my engineering models - i.e. my business logic. I use Qt containers inside my GUI code. This way my engineering models are protected against changes in Qt and vice versa. Qt has ways of connecting standard containers and their containers.
    – emsr
    Jan 8, 2012 at 19:16
  • 2
    I don't care that much about containers anyway. They're not the most useful part of the STL; algorithms are far more important. Due the STL's design, its algorithms can easily work with other containers, and Qt containers are designed to work with STL algorithms. Win-win, really.
    – MSalters
    Jan 9, 2012 at 12:49
13

Considering that Qt has its own meta-compiler that you have to process your source files with, it's hard to consider Qt code to be "just C++".

But more importantly, the style of C++ that Qt uses and encourages is something that, to the rest of us, was last seen around 1995.

Really, it's an attempt at making C++ as Java-like as at all possible. It misses out on, or discourages, all the amazing things that actually make C++ worth using today. Instead, you're locked into a subset which most of all feels like an inferior Java.

So if the goal is to learn C++, I'd say no, stay away from Qt. Perhaps take a look at Boost instead, if you want to use an established library as a starting point. Boost embodies the practices that are considered good today.

But honestly, if you want to learn the C++ language, then pick up a good textbook, and focus on the language.

Writing a web browser, no matter how you do it, will at best teach you about web browsers. It won't teach you much about the language you're using.

C++ as a language does not have a WebBrowser class built in. If you read the C++ standard, it says nothing about browsers. It doesn't even mention GUI applications.

That's because those things are built on top, provided by libraries such as Qt. C++, like any programming language, is about logic, about how to express that logic. And yes, that means working with arrays and pointers and loops and all those things.

Would you be able to write a web browser using just those built-in tools, given sufficient time? Would you know how to express the program logic involved? If not, then you need to spend more time on loops and pointers, and less calling new QWebKit() and just piggybacking off ready-made libraries.

The "philosophy and power of the language" is in arrays and lists and loops, not in web browsers.

24
  • 4
    I never said they were better because they are new. They are new and they are better. The point is that C++ as a language is just not very well suited for this Java-like approach. In C++, inheritance and runtime polymorphism is a pain, because it practically requires you to heap-allocate objects all over the place, which is what gave the language its reputation for memory leaks in the first place, because it's prone to slicing errors, because it makes copying objects harder. It's also inefficient, less extensible and requires more tedious boilerplate code.
    – jalf
    Jan 7, 2012 at 15:47
  • 2
    Heck, Qt needs its own meta-compiler just to make their style of programming bearable. Doesn't that tell you something?
    – jalf
    Jan 7, 2012 at 15:47
  • 4
    I was not defending Qt in itself (I see a lot of limitations in it myself). Your statement "the style of C++ that Qt uses and encourages is something that, to the rest of us, was last seen around 1995." With this formulation it is not clear what is bad with this style (other that it is 15 years old) and what the current, better alternatives are. Maybe if you are more specific what you mean will become clearer.
    – Giorgio
    Jan 7, 2012 at 16:05
  • 3
    @JimInTexas: it also predates C++. Your point? In the early-mid 90's, C++ was a very different beast. It was much closer to what eventually became Java than it is today.
    – jalf
    Jan 8, 2012 at 2:43
  • 3
    @JimInTexas: Yes, it's wrong, but it should also hopefully be fairly clear what I mean. It is an attempt to achieve the design goals of Java, in C++. It's an attempt at implementing the same flawed OOP model that java implements.
    – jalf
    Jan 9, 2012 at 8:02
9

Is Learning C++ Through The Qt Framework Really Learning C++

Maybe.

I'd have to see the code you're putting in your event handlers.

Really though, don't be obsessed with how much you "know". We all use windowing frameworks and we're all still learning. Just keep coding/reading new things and you'll continue learning C++. Learning a new windowing framework is a great addition to your skills even if it might not mean you can implement a neural network or a quicksort in C++.

5

Don't worry; at first most of your code will just use the framework, but after a while you'll have to extend it, even if just a bit. Then you'll have to use more and more of C++.

Also remember that you have the whole source of Qt available, the IDE will happily take you to the definition of any function/method/class you want. The deeper you go, the more C++ you'll see.

Others have mentioned about the difference between Qt C++ and standard C++. These fall in two camps:

  • different library: Qt includes all the usual containers: arrays, lists, sets, hashmaps, etc. They fit together very well and are good implementations, but they're not the STD variety. Still, in recent versions (4.1 and later, I think) they have 'STD-like' APIs in addition to the old variety (and a Java-like API). In the end, the design choices (when to use an array, when to use a hashmap) are the same, so changing to STD for non-Qt projects is not so hard.

  • moc syntax additions: mostly for signal-handling, but also a couple of nicer loop constructions. Lots of people feel this tool means it's not C++ anymore; but IMHO, they feel just like slightly smarter macros. A good loosely-coupled signal handling is a huge advantage of a good framework, and it's notoriously hard to do on a static typed language. In modern C++, it's doable with a heavy dose of templates; but that was far from standard when Qt first got moc. In the end, if you later want to do non-Qt projects, first check if you'll be using any framework and if it has signals. If yes, then most of what you're used to do with Qt will apply, so no 'harm' in learning Qt first.

6
  • What loop improvements are given by moc? Jan 7, 2012 at 12:42
  • @Basile Starynkevitch: Maybe he refers to the foreach loop (which I use heavily). This is a syntax addition but, ASAIK, this has nothing to do with moc. So you are right that maybe the answer should be formulated in a different way.
    – Giorgio
    Jan 7, 2012 at 12:55
  • foreach is just a macro.... (actually an "alias" to Q_FOREACH defined in <Qt/qglobal.h>). I don't think that moc is processing it. And C++11 has the for (auto it : container) construct! Jan 7, 2012 at 13:06
  • @Basile Starynkevitch: Probably Qt would need a deep re-engineering if it had to move to C++11: Qt was designed for C++ (and also tried to overcome some of its limitations) therefore some of its solutions are not needed with C++11.
    – Giorgio
    Jan 7, 2012 at 13:51
  • 1
    I use both Qt and Boost. There is a lot of overlap between the two libraries, but Qt is much easier to learn. Jan 7, 2012 at 16:02
4

Qt is widely used in the commercial world because it provides a full cross platform toolset and development environment, including a good GUI library.

It also fully supports internationalization, including the excellent 'Linguist' tool.

If you plan an academic career then I wouldn't bother with Qt. If, on the other hand, you like C++ and want to learn a marketable skill then Qt is worth learning.

And yes, Qt is C++, and you can mix in the standard and boost libraries to your hearts content if that makes you feel better.

3

It is a bad idea to learn C++ through Qt. First you have to learn the language concepts independant of any framework and that is what c++ books will teach you and they are right. Basically, 'for and loops while, arrays, lists, pointers' are what programming languages are all about. Additional functionalities are provided by frameworks. Once you learn a programming language, you can learn any framework like Qt or MFC that is built using the language, so that applications can be developed quickly. Regarding Qt, once you learn c++, it is an excellent framework which makes you as productive as any Java or .Net developers. Shortly you will be able develop ios and Android apps using Qt.

2

Well, I think the best way to learn C++ is by using its own syntax ONLY(Standard C++), so you will be able to use the LANGUAGE stuff, and NOT the Qt(or any other frameworks, libraries ...etc).

Why? because as a beginner, when you look at any C++ code mixed with other Non-C++ code(Qt in this case) you will not be able to see what is C++'s things and what is not, rather it will be more complex process.

Not the answer you're looking for? Browse other questions tagged or ask your own question.