How to use plain C in Qt to create a GUI?
-
I want to create a GUI using Qt which should use plain C code.
There was a similar question asked 11 years ago, which wasn't answered clearly in my opinion. As things have changed over the years, can someone provide a clear and detailed explanation regarding this topic?
-
I want to create a GUI using Qt which should use plain C code.
There was a similar question asked 11 years ago, which wasn't answered clearly in my opinion. As things have changed over the years, can someone provide a clear and detailed explanation regarding this topic?
-
I want to create a GUI using Qt which should use plain C code.
There was a similar question asked 11 years ago, which wasn't answered clearly in my opinion. As things have changed over the years, can someone provide a clear and detailed explanation regarding this topic?
@dennissoem
As @J-Hilk says. Write the GUI in C++ with Qt. Either add your C source files directly into the project or compile them into a library and link against it. -
I am aware that one could easily use C in C++ code. I want to know if there are some special requirements that one should keep in mind? I see that JonB has given an answer. If someone could give a more detailed answer that would be much appreciated.
-
I am aware that one could easily use C in C++ code. I want to know if there are some special requirements that one should keep in mind? I see that JonB has given an answer. If someone could give a more detailed answer that would be much appreciated.
@dennissoem
I really don't think there is anything else to say. You cannot emit signals from C as that requires aQObject
. You could probably implement a slot if you wish, since that is only a plain function, but you could equally declare the slot in C++ and just have it call the C function, so I don't see any relevance.There are tons of "misbehaviours" you could do in C code called from C++, whether Qt or not, but then there are also tons even if calling C++, so not much to say, nobody is going to enumerate every possible "gotcha".
-
-
@dennissoem
I really don't think there is anything else to say. You cannot emit signals from C as that requires aQObject
. You could probably implement a slot if you wish, since that is only a plain function, but you could equally declare the slot in C++ and just have it call the C function, so I don't see any relevance.There are tons of "misbehaviours" you could do in C code called from C++, whether Qt or not, but then there are also tons even if calling C++, so not much to say, nobody is going to enumerate every possible "gotcha".
Hi,
There are no particular requirements. Qt is C++ after all.
That said, a pretty common pattern used is to create a wrapper object that will handle all the C interaction to provide a simple and clean interface.
As an example, I wrote a long time agor a QIODevice subclass to take advantage of the D2XX driver for the ftdi chipset.
-
I don't think there is a C wrapper for Qt. And it would be quite cumbersome to write one for all of Qt that you need. It is easy to use C in C++, but it is hard the other way around. Unless you are a C++ expert don't try to write a C wrapper for Qt. You need to understand a good portion of the language standard concerning OO (the part targeted at compiler implementers) to get it right. It is much easier to learn some C++ to be able to write the GUI in C++ using Qt.
I am not sure what reason you have to ask this question. I guess some of it might be related to embedded systems where there is not STL available. However, Qt does not use the STL internally (though there are some conversion functions). I don't think you'd need RTTI or exceptions for Qt to work properly. This would eliminate most of the problems of using C++ with Qt on embedded systems.