tags:

views:

302

answers:

7

I think it's unreasonable for a library to require preprocessing of my source code with a special tool. That said, several have recommended the Qt library to me for cross platform GUI development.

How usable is Qt without the preprocessing step?

EDIT: Okay people; I'm not meaning this question as a rip on Qt -- too many Qt fanboys are treating it as if it is. I don't want to discuss the merits of the fact that Qt came up with this preprocessing tool. I understand why the tool is there, and I understand why there are large parts of Qt's design that are built upon the idea of preprocessing.

I've never used Qt; ergo I am in no position to rip on Qt. But I would much rather pay in writing a small amount of boilerplate myself and not depend on ripping apart my entire build process. I won't use Flex and Bison in my current project for the same reason; if I won't use those tools, I'm definitely not going to use another kind of preprocessing.

So, please don't take me as ripping on Qt. I cannot comment on how nice or not nice it is; I have not used it. I just want to know if it is possible to use it without moc.

+12  A: 

Qt doesn't require the use of moc just to use it, it requires that usage if you create a subclass of QObject, and to declare signals and slots in your custom classes.

It's not unreasonable, moc provides features that C++ doesn't have, signals/slots, introspection, etc.

So, to do something minimally advanced, you WILL have to use the moc preprocessor. Your either love it, or hate it.

Matias Valdenegro
Is doing something like creating a window with a tab control, a textbox, and a few buttons "minimally advanced"?
Billy ONeal
Boost.Signals2 (http://www.boost.org/doc/libs/1_40_0/doc/html/signals2.html) implements the observer pattern without having to resort to special preprocessing like signals/slots. Also, MFC provides introspection if you derive from `CObject` using preprocessor macros. I'm not suggesting either implementation is superior to Qt's, just that people have managed it without generating additional source files.
Praetorian
@Billy Depends, connecting a button click to a slot in your custom class requires the use of moc.
Matias Valdenegro
@Matias: So, I could have a button, but I'd have no way of telling when someone clicked on it?
Billy ONeal
@Billy Yeah, unless you connect it to a already defined slot (not much useful).
Matias Valdenegro
+6  A: 

I don't consider it unreasonable that Qt requires a special pre-processing tool, considering how large and comprehensive of a library it is.

Other similarly comprehensive libraries such as Boost and GLib don't require special pre-processing tools but do make extensive use of the standard C preprocessor. Qt could have been implemented using only the C preprocessor, but by using its own special preprocessing tool, it can provide a cleaner syntax and avoid many of the pitfalls associated with C preprocessor macros.

As has been answered already, though, you can use Qt without moc, just not anything that requires signals and slots. Yes, this does include all of the GUI stuff, but Qt is not by any means just a GUI library.

Tyler McHenry
-1: That does not answer the question I asked. I'm not asking about the merits/pitfalls of Qt. I'm asking if it's usable without the special preprocessing tool.
Billy ONeal
(sorry for double comment) I know that to do something like reflection they would need to use preprocessing as C++ does not provide that feature out of the box. That said, there was really no reason to have to implement signals and slots that way -- there are plenty of pure C++ solutions to that problem. I understand though, that to fulfill the goals Qt started out with, that preprocessing would be needed. But given that I don't plan on using any kind of reflective features, I want to know if it's possible to use the library reasonably without preprocessing.
Billy ONeal
I'm not sure what else you're looking for other than what's already been posted. The answer is simply that you can "effectively use" the parts of Qt that don't rely on moc to compile or to function properly. Whether that's enough for you depends on what you're doing with Qt. It sounds like you're making a GUI, so you will probably not be able to get away with avoiding moc.
Tyler McHenry
@Tyler: Somehow I missed that last paragraph in your answer... was it there when I commented? Anyway... I just don't want this question to turn into a Qt Fanboys versus Qt Haters flame war. (See edit to my question explaining where I'm coming from). I thought that's where this answer was coming from; I have changed my downvote into an upvote.
Billy ONeal
+2  A: 

I really can't think of anything so unique and useful with Qt without using QObjects. I wish they worked their way around that pre-compilation step.

Sundar
+3  A: 

Is it possible? As long as you're not doing any gui programming, probably. Personally I mostly run with PyQt these days, so it's not a big concern for me.

Why you shouldn't care: Given the nature of the "precompilation" if you're using cmake or qmake, it's not really a big deal in terms of inconvenience. If you're doing anything with a GUI these days, you should be using a graphical designer for most of the work anyway, so you're already adding some "pre-compilation" steps.

Regarding why they did it: You might be interested to read Qt's explanation: http://doc.qt.nokia.com/4.6/templates.html

It boils down to:

  • The template solution lacks properties and overloading
  • The moc solution prevents signals from breaking binary compatability
  • Runtime analysis and modification are possible with the non-templated signals/slots mechanism

On a side note, multithreading signals/slots are also an advantage of their system.

jkerian
It's a big deal in that I have to tear down my existing build system and replace it with Qt's. I don't fancy having to spend hours converting from MSBuild.
Billy ONeal
If it's even a half-decent build system in the first place, it'll accomodate it. http://linuxdummy.blogspot.com/2008/06/automating-qt-compilation-with-msbuild.html
jkerian
Given that objection though, I'm suspicious that Qt may not be appropriate for your problem. Qt is a pretty massive library, and I'm not sure how well it would work as an "addon" to a presumably large project already.
jkerian
@jkerian: It will accommodate it just fine. I'm just saying the response of "Well just throw out your current build system" isn't really realistic.
Billy ONeal
Basically any build system can be taught how to deal with moc and the precompilation step, it's just easier with cmake or qmake. I've done it a few times now with custom makefiles. Not necessarily pleasant, but workable.
jkerian
+2  A: 

Using Qt while avoiding moc will be more difficult than just using them together as intended. You will also sacrifice most of the interesting features that motivated others to recommend Qt.

Without moc you can't

  • Use signals & slots (which are all but required for UI)
  • Use the dynamic property system (needed to write plugins, among other things)
  • Use the internationalization features
  • Expect to get help from anybody when nothing works

If you want to use Qt, use moc. In fact, don't even worry about moc -- just use QMake. You can write a QMake .pro file that looks like this:

TARGET = myApp

FORMS += MainWindow.ui

HEADERS += MainWindow.h

SOURCES += MainWindow.cpp
SOURCES += main.cpp

Everything will be taken care of automatically. Or you can spend all your time trying to figure out how to avoid moc.

See http://doc.qt.nokia.com/4.6/metaobjects.html and http://doc.qt.nokia.com/4.6/moc.html#moc

Steve S
I don't want to have to revamp my entire build system (which is currently MSBuild) in order to use a single library.
Billy ONeal
1) You didn't state that you're converting an existing project. 2) You can probably make MSBuild work with `moc` more easily than you can make Qt work without `moc`.
Steve S
@Steve S: I assumed that if I said "I think using a preprocessing tool is unreasonable" -- which isn't all that invasive -- that it would be sort of implicit that I didn't want to spend a week completely replacing my current build system (which is much more invasive). Given that I was not asking *how* to get `moc` working in my project, I was asking if it was possible to *avoid* it, I did not think that piece of information was relevant to my question. Given that you answered my question in the first part of your answer without that information, I don't think I was wrong.
Billy ONeal
Your question was indeed clear. But I think my answer (all of it) was appropriate given the amount of detail you provided. Even with the added information, my advice remains "Use moc, or don't bother with Qt at all."
Steve S
+2  A: 

I don't have a full answer, but as I understand it, moc mainly (or perhaps only) generates additional C++ code. So potentially there's nothing it does that you couldn't also do yourself manually. However, I have no idea how tedious that might be nor how much study it might take to understand all the necessary concepts and details that go into that code.


Also, as I side note: In my opinion, the reason you're getting as much defense of Qt and moc is because you started your question with the strongly worded "I think it's unreasonable" which is easily interpreted to mean that you don't think moc should ever have existed. This distracts from your actual question. I think it would have been better just to say "moc doesn't fit into my build system" or simply "I have my own reasons for not wanting to use it".

TheUndeadFish
I do think it's unreasonable. That does not mean I dislike Qt as a whole. It does mean that I do strongly dislike that I would have to use a separate preprocessor. I strongly dislike plenty of things about the C++ spec too (i.e. exception specifications) -- that does not mean that C++ is not my favorite language. Any sufficiently complex piece of technology is going to have annoying pieces. That's in no way unique to Qt. I assumed that the people reading my question would understand that. Perhaps I assumed too much.
Billy ONeal
+2  A: 

You can use Qt without the moc, but then you lose certain functions, especially those that make Qt interesting in the first place (such as most of the GUI stuff, signals and slots, and string translation). But it's still a nice general purpose library, even without moc.

Sebastian N.