PDA

View Full Version : Qt with C?



DecWiz
30th January 2006, 14:51
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!

high_flyer
30th January 2006, 15:28
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.

zlatko
30th January 2006, 17:53
Qt library based on C++ language,all standart library components writed on it.

DecWiz
30th January 2006, 18:37
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?

zlatko
31st January 2006, 09:58
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...

high_flyer
31st January 2006, 10:23
Is this scenario realistically possible with Qt?
Yes it is, all thoug I didn't do it my self.
As I said before, you have C bindings to Qt, check it out.

fullmetalcoder
31st January 2006, 15:42
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?

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).
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!:rolleyes:
You'll just need to had GUI code and change the parameters of your compiler/IDE to make it compile your project in C++.

high_flyer
31st January 2006, 15:54
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!
I am not sure what do you mean by that, but this is not quite true.
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.

peschmae
31st January 2006, 17:08
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ä

high_flyer
31st January 2006, 18:24
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.
This is not true.
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.

Mariane
31st January 2006, 20:47
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

high_flyer
1st February 2006, 16:13
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.
When you compile C code as C++, then you have a C++ project.
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.


FILE * logFile;
logFile = fopen(filename, "w");
etc,
where filename is of type QString and it worked fine.
Are you sure about this?
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?

fullmetalcoder
1st February 2006, 21:52
Are you sure about this?
fopen() doesnt know QString, so this is impossible, unless there is more to the code then you showed here.


I've never tried Qt3 but there may be an implicit conversion to char* :


QString::operator char* ()
{ ... }


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!!! :p )
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!)

high_flyer
1st February 2006, 22:15
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/cc/articles/mixing.html
And relevant to this thread, is the section: Accessing C++ Code From Within C Source

high_flyer
1st February 2006, 22:18
I've never tried Qt3 but there may be an implicit conversion to char* :
Not that I can see....
http://doc.trolltech.com/3.3/qstring.html
You have these methods for C like strings:
# const char * ascii () const
# const char * latin1 () const

jacek
1st February 2006, 22:27
Not that I can see....
How about this one: http://doc.trolltech.com/3.3/qstring.html#operator-const-char-*?

high_flyer
1st February 2006, 23:21
I stand corrected.
I forgot to look in the "List of all member functions".:rolleyes:
Sorry Mariane.:o

jakamph
2nd February 2006, 13:30
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:



#ifdef __cplusplus
extern "C" {
#endif


and at the end of the header



#ifdef __cplusplus
}
#endif


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.