How usable is Qt without its preprocessing step?

后端 未结 12 787
自闭症患者
自闭症患者 2021-02-02 05:21

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

相关标签:
12条回答
  • 2021-02-02 05:39

    (sorry for reviving such an old post)

    I was required to do a C/W assignment for my MSc unit in 3D Software Development.

    Unfortunately a requirement was to use Qt to provide an OpenGL context rather than use Glut, native X11, Gtk etc...

    I did not want to use MOC and with a lot of fiddling I was able to get just enough callbacks (such as keyboard, mouse, paint, timer etc...) to make a usable submission. So for using Qt with a primary reason for OpenGL software, it actually works fine without MOC.

    However, I can't see how a full application could be developed without using MOC. Which is a shame.

    This Qt MOC thing is a pain. I don't understand why so many C++ developers seem to find it acceptable frankly. It is extremely unportable and will get crusty fast! (Just try to get some Borland Kylix C++ code to compile. You will soon realize what a bad idea this is).

    If I wanted to use non-standard C++, I would just use Microsoft C++/CLI.

    0 讨论(0)
  • 2021-02-02 05:42

    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".

    0 讨论(0)
  • 2021-02-02 05:42

    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.io/qt-4.8/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.

    0 讨论(0)
  • 2021-02-02 05:45

    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 https://doc.qt.io/archives/qt-4.7/metaobjects.html and https://doc.qt.io/archives/qt-4.7/moc.html#moc

    0 讨论(0)
  • 2021-02-02 05:48

    I'm currently in the position of needing to find alternatives to MOC. So far I use GLib for text translation and I'm in the middle of reengineering a signals/slots library I found on the web (sigslot) using C++ templates and C macros combined. The boring and exhausting part is redoing the GUI for my app and replacing normal Qt widgets with my own widgets (i.e. QPushButton -> MyPushButton) through widget promotion (a feature of Qt Designer). This way I can have my widgets emit templated signals instead of Qt's. There's a catch, tho. My "modified" widget classes MUST be run through the preprocessor, but that's a once-in-a-lifetime step. After that, I can move on.

    I can afford to do all of this only because I have already written an Application Framework library with its own event loop (well, it's a wrapper that piggybacks on Qt's event loop code), multithreading and string classes, etc. My project will only need Qt to display the nice and nifty widgets.

    But if you don't have these tools at your disposal - which I'm sure you don't - trust me, trying to get rid of Qt's preprocessor is going to be a royal pain in the arse.

    Here's my message to you: If you can use QMake or moc, just use them.

    0 讨论(0)
  • 2021-02-02 05:52

    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.

    0 讨论(0)
提交回复
热议问题