How to use C code in qt?



  • see i have one c code , printf("hello world"); normally this c program will run by gcc in console. i lyk to develop gui for this code. that is hello world should be display in dialog box of qt.. for that what i want to do?? how to add C code(linux c) in qt? what are the libraries should add? how to use the c code in qt like how to create object and how to synchronize? please help me soon

    thanks in advance,
    shakthi


  • Moderators

    Did you install Qt Creator?
    Then you can make a new project in console or GUI with "hello world". The needed libraries are included for you automatically.

    To use C code in C++ you need to use the
    @extern "C"@ directive.

    Here is an example on how you can use the printf.
    @#include<cstdio>
    void foo()
    {
    std::printf("hello world");
    }@



  • Well if you want to show your message in a window printf will not do this for you. You need to change your code to something like this:

    @
    #include <QApplication>
    #include <QLabel>

    int main( int argc, char* argv[] )
    {
    QApplication app( argc, argv );
    QLabel label = new QLabel( "Hello World!", 0 );
    label->show();
    return app.exec();
    }
    @



  • @Eddy and zapB, you people dint understand my thread i think.. my question s, how to use c code in qt with out change of c code. like c code displaying some output , i like to build gui for that code.. for that if i want to create one variable and using that i may access qt code right. how to create that variable? and how to synchronise c and c++?



  • You are not being clear about what you want. In one sentence you say you want to use your C code unchanged, which Eddy has shown you how to do, ie just wrap your C code declarations in extern "C" so that the linker knows those functions have c-linkage (no C++ name mangling).

    In another sentence you say you want a GUI to display the output, which is what I showed you.

    Can you give a small example of a C function that you want to mix with C++/Qt please and also say how you would like to use it in your GUI (ie is the C function used to calculate some numbers that you need to display).



  • //You are not being clear about what you want. In one sentence you say you want to use your C code unchanged, which Eddy has shown you how to do, ie just wrap your C code declarations in extern ā€œCā€ so that the linker knows those functions have c-linkage (no C++ name mangling).//

    see yes i ll use pthread.h , time.h, sys/time.h and all c headers file, see my question is,

    back end - C
    front end - QT

    normally c program output displaying in console rite, now i like to design gui so that my c program will display in widget... so how to use that.. tats what my question..

    am new to qt , so ur answer should be precise so that i can understand.. and extern "C" k i ll try tat too. but is it possible to add my C Code and design gui so that both language need synchronization.. if u give me any doc too, it will be helpful for me... please help me dude..



  • OK I think I understand now but before we get into the details, do you already have the backend written in C or is this something you still need to do?

    I ask because Qt is much more than just a GUI library. It has some very nice classes for working with threads, dates, times, XML, SQL to name but a few topics. So if you have yet to write your backend you may want to consider using C++/Qt for that too.



  • @zapb yes dude.. gui have enough class to implement everything. but am expert in C and its like driver program so best to written in C and my first application is socket in c using tcp eventhough qsocket is there in qt .. i like to implement as front alone in qt and backend is c.. please provide me any suggestions.. atleast give me small example then i can follow using that.. have u tried it??



  • if you want to keep backend and frontend separately you either need to implement some data protocol (dbus maybe or pipes) to transfer data from backend to frontend. If you want simply show all output in single QTextEdit widget you can redirect stdout somewhere and show it in your widget, but first suggestion is more correct way (and much more flexible).


  • Moderators

    If the C-based code is a standalone program, it might be possible to use QProcess to run it, and then capture the output and show it in a widget. Not sure if that's a viable option for you or not, though.



  • shakthi

    With some C you will need to use extern "C" and you might have to
    convert some C types to Qt C++ . Smae goes with C++ std to Qt.

    For a regular C string, just use the main constructor:

    @char name[] = "Stack Overflow";
    qDebug() << QString qname(name);
    @
    or

    @std::string str("character/test/raw");
    qDebug() << QString::fromStdString(str);
    @
    or

    @std::string str("character/test/raw");
    QString qstr(str.c_str());
    qDebug() << qstr;@

    Edit: please use @ tags around code sections; Andre



  • I have used C code withing Qt without any problem, to be exact I have used the languages C (with OpenSSL library), C++, Qt, QML, Javascript, HTML in a single application without too much effort. I don't think that you have to do something to mix C, C++ and Qt.

    OK, now for example: the following code will use C and Qt.

    @
    #include <QApplication>
    #include <QLabel>

    //C Header files
    #include <stdlib.h>

    int main(int argc, char* argv[])
    {
    int 1;
    char a[] = "64";
    i = atoi(a);//C function to convert char to integer
    QApplication app(argc, argv);
    QLabel label(QString("Actual char: " + a + "Integer :" + QString::fromNumber(i) )); //char to QString
    return app.exec();
    }
    @

    All you h


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.