Is it possible to use Qt with a C application and C?
Can someone point me to some examples of using C with Qt?
Thanks!
Is it possible to use Qt with a C application and C?
Can someone point me to some examples of using C with Qt?
Thanks!
Yes it is.
Then again, the question is what do exactly mean.
Do you mean to have an entire Qt project in C, or do you have a C++ project that some parts are in C?
There are C bindings for Qt.
Qt library based on C++ language,all standart library components writed on it.
Apologies, I didn't provide enough context.
The primary application is written in C. I'd like to extend the application by adding a graphical window (with Qt) to simulate a VGA/Keboard/Mouse console, but leaving the core C program intact.
I would need to abe able to create a Qt window from my C code, and display the graphics to it from the C code, and have it deliver events (keyboard, mouse) back to the C code.
Is this scenario realistically possible with Qt?
You can create first Qt project with needed Qt interface and then step by step adding functioonality ftom C code. I'm sure that this way hasn't perfect quality, but after some correctings it must working...
Yes it is, all thoug I didn't do it my self.Is this scenario realistically possible with Qt?
As I said before, you have C bindings to Qt, check it out.
The core will stay intact as long as you wish it and types conversions are clear enough for a C++ compiler (which is more strict than a C one in that domain).Originally Posted by DecWiz
However, your project will not stay a C one, using a binding for C would be silly since any C code can be compiled by a C++ compiler!
You'll just need to had GUI code and change the parameters of your compiler/IDE to make it compile your project in C++.
Current Qt projects : QCodeEdit, RotiDeCode
I am not sure what do you mean by that, but this is not quite true.However, your project will not stay a C one, using a binding for C would be silly since any C code can be compiled by a C++ compiler!
Following your logic, the whole idea of language bindings would not make sense.
This would mean a pyton project would have to be turned to a C++ project to use the PyQt.(python bindings for Qt).
Qt binding are there for projects in the respective languages to interface to Qt, which means, the project stays in its original language.
Correct me if I am wrong.
That's not what he said.
He said a C program also is a C++ program thus you can directly use a C++ Compiler and also directly use Qt without C bindings. That is, without changing the program or "really" converting it to C++.
This is true for some C programs - but not for all of them. You probably will get tons of Warnings and maybe even some errors when compiling a C program with C++ (provided you have never done this before with the code and fixed the bugs)
To come back to your analogy: If a python program could be compiled with any C++ compiler it would be silly to use the pyqt bindings...
Peschmä
No sig yet - just filling the space...
This is not true.He said a C program also is a C++ program thus you can directly use a C++ Compiler and also directly use Qt without C bindings.
C++ has tottaly diferent name mangling then C, because of the features it has such as overloading and classes.
So function signatures are not compatible between C and C++.
This is also why you need to use the extern "C" directive when you want to include C code in C++ project.
Its true, that some times you can get away with it, by just adding the C code and nothing more, but that is very much chance dependant, if it will work or not.
I don't know what the precise rules are, but in my limited experience
you can write some C in a C++ program provided you compile it as
C++ and not as C. Strings are a problem, though, don't mix C char
arrays and C++ strings.
But I've just written a bit of C in Qt :
FILE * logFile;
logFile = fopen(filename, "w");
etc,
where filename is of type QString and it worked fine.
Mariane
When you compile C code as C++, then you have a C++ project.I don't know what the precise rules are, but in my limited experience
you can write some C in a C++ program provided you compile it as
C++ and not as C. Strings are a problem, though, don't mix C char
arrays and C++ strings.
But if you want to include *.c files or lets say import C compiled elements from a C library, then you are in trouble if you don't deal with the different signatures.
Are you sure about this?FILE * logFile;
logFile = fopen(filename, "w");
etc,
where filename is of type QString and it worked fine.
fopen() doesnt know QString, so this is impossible, unless there is more to the code then you showed here.
Could you show the full relevant code?
I've never tried Qt3 but there may be an implicit conversion to char* :Are you sure about this?
fopen() doesnt know QString, so this is impossible, unless there is more to the code then you showed here.
Qt Code:
{ ... }To copy to clipboard, switch view to plain text mode
Anyway it's true that a C++ compiler handles lots of stuff in different way than a C one but code should be still compatible (maybe a few warnings if some type conversions are ambiguous, errors shouldn't come if your code isn't a damn spaghetti plate!!! )
Personnally I tried such a thing (I mean using C inside C++); I wrote a full file processing API using OOP and so on but the core used C (fopen, fgets, strcmp, ...) cuz, in my opinion, STL sucks and moreover, under mingw32, it makes your program bigger!)
Current Qt projects : QCodeEdit, RotiDeCode
Ok, I guess I am articulating my self poorly.
So, instead read the following, to understand what I meant in my previous posts:
http://en.wikipedia.org/wiki/Name_mangling
Pay attantion to the sections:
Name mangling in C++
Handling of C symbols when linking from C++
And this is not bad about mixed C and C++ code:
http://developers.sun.com/prodtech/c...es/mixing.html
And relevant to this thread, is the section: Accessing C++ Code From Within C Source
Not that I can see....I've never tried Qt3 but there may be an implicit conversion to char* :
http://doc.trolltech.com/3.3/qstring.html
You have these methods for C like strings:
# const char * ascii () const
# const char * latin1 () const
How about this one: http://doc.trolltech.com/3.3/qstring...r-const-char-*?Originally Posted by high_flyer
I stand corrected.
I forgot to look in the "List of all member functions".
Sorry Mariane.
Where I work, we have a lot of legacy C code that needs to be used in a QT project. As part of our coding standard, however, we have a rule that states that _any_ C header file must be framed by:
Qt Code:
#ifdef __cplusplus extern "C" { #endifTo copy to clipboard, switch view to plain text mode
and at the end of the header
to ensure that we don't run in to problems with the different name mangling between C and C++. That wouldn't leave your original program untouched, I'm guessing, but it would avoid some of the potential problems of mixing the two.
Bookmarks