Results 1 to 18 of 18

Thread: Qt with C?

  1. #1
    Join Date
    Jan 2006
    Posts
    2
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Question Qt with C?

    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!

  2. #2
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Qt with C?

    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.

  3. #3
    Join Date
    Jan 2006
    Location
    Ukraine,Lviv
    Posts
    454
    Thanks
    9
    Thanked 27 Times in 27 Posts
    Qt products
    Qt3
    Platforms
    Unix/X11 Windows

    Default Re: Qt with C?

    Qt library based on C++ language,all standart library components writed on it.

  4. #4
    Join Date
    Jan 2006
    Posts
    2
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Qt with C?

    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?

  5. #5
    Join Date
    Jan 2006
    Location
    Ukraine,Lviv
    Posts
    454
    Thanks
    9
    Thanked 27 Times in 27 Posts
    Qt products
    Qt3
    Platforms
    Unix/X11 Windows

    Default Re: Qt with C?

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

  6. #6
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Qt with C?

    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.

  7. #7
    Join Date
    Jan 2006
    Location
    travelling
    Posts
    1,116
    Thanks
    8
    Thanked 127 Times in 121 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Qt with C?

    Quote Originally Posted by DecWiz
    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!
    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

  8. #8
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Qt with C?

    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.

  9. #9
    Join Date
    Jan 2006
    Location
    Bern || Lausanne
    Posts
    1
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Qt with C?

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

  10. #10
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Qt with C?

    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.

  11. #11
    Join Date
    Jan 2006
    Posts
    46
    Qt products
    Qt3
    Platforms
    Unix/X11

    Default Re: Qt with C?

    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

  12. #12
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Qt with C?

    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?

  13. #13
    Join Date
    Jan 2006
    Location
    travelling
    Posts
    1,116
    Thanks
    8
    Thanked 127 Times in 121 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Qt with C?

    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* :
    Qt Code:
    1. QString::operator char* ()
    2. { ... }
    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

  14. #14
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Qt with C?

    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

  15. #15
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Qt with C?

    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

  16. #16
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Qt with C?

    Quote Originally Posted by high_flyer
    Not that I can see....
    How about this one: http://doc.trolltech.com/3.3/qstring...r-const-char-*?

  17. #17
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Qt with C?

    I stand corrected.
    I forgot to look in the "List of all member functions".
    Sorry Mariane.

  18. #18
    Join Date
    Jan 2006
    Posts
    17
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Qt with C?

    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:
    1. #ifdef __cplusplus
    2. extern "C" {
    3. #endif
    To copy to clipboard, switch view to plain text mode 

    and at the end of the header

    Qt Code:
    1. #ifdef __cplusplus
    2. }
    3. #endif
    To copy to clipboard, switch view to plain text mode 

    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

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.