Problems with Open-Source Downloads read https://www.qt.io/blog/problem-with-open-source-downloads and https://forum.qt.io/post/638946

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



  • 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



  • Hi@DenisKormalev I need to interface C driver code to QT GUI, Can you show me a snippet to achieve the linking process from C source code to QT GUI

    Ex 
    unsigned int a[100]={ 25,  17 , 10,  6,  -5 , -6, -8 , 10, ......... };
    
    

    I am required to send this array data to QT GUI through Slots, from C source file to QT Slots is that possible?

    If possible please someone shows the code snippet in QT that will help me a lot


  • Lifetime Qt Champion

    @vivekyuvan said in How to use C code in qt?:

    I am required to send this array data to QT GUI through Slots, from C source file to QT Slots is that possible?

    No it's not as your C driver code isn't based on Qt, right? You will need to call the interface functions of your C driver in your app.



  • Hi@jsulm
    I like to implement frontend alone in qt and backend is in C. The backend code was written in C its best to keep the code in C because its a driver code. so now my concern is can I communicate my driver code with frontend QT GUI?

    // You will need to call the interface functions of your C driver in your app.//

    some me some snipet to interface C code to qt with QDBUS IPC protocol


  • Lifetime Qt Champion

    @vivekyuvan You can use C code in a C++ project. I don't really understand what the problem is. Either add your C code directly to your project or add it as a shared library.
    I can't give you any snippet of code to use this C code as I have no idea how it looks like. Take a look at its interface/API, read its documentation and find out how to use it. I can't/will not do it for you.



  • Hi@jsulm Ill explain in detail, first of all, I cant add my C driver code as the shared library because I am Implement the lightweight GUI application. hope you can understand lightweight GUI application.

    A lot of driver code participate in my project like UART Driver and I2C Driver and SPI Driver, all driver code was written in C code.

    So I can't wrap my driver code into QT Source code as well as its difficult to add all driver code as the shared lib in QT Source code

    Because I am Implementing lightweight GUI application. My frontend QT code is just a window to monitor all parameters

    So I am forced to create my driver code as like as demon process in the background. Then I catch the required data in QT QProcess class in the frontend code Is that possible?


  • Lifetime Qt Champion

    @vivekyuvan How is your "driver code" communicating with the outside world? If you start it as a deamon you need a way to communicate with it. So, how does your driver communicate? Does it have to run as a daemon? I suggested two solutions: one was shared library (I still don't understand why you think it is not possible), or add the C code to your Qt app. Can't you simply add C code to your Qt app?


  • Lifetime Qt Champion

    @vivekyuvan

    I cant add my C driver code as the shared library because I am Implement the lightweight GUI application

    Both options are not mutally exclusive.

    You really only have three options:

    1. Add your C code directly in the Qt project (i.e. link it statically)
    2. Add your C code as a shared library (i.e. link it dynamically)
    3. Let your C code run as a standalone program (a daemon) and communicate with it by some interprocess communication method.

    Note that for option 3 the daemon must be started by someone, in order to communicate with your GUI program.

    Regards


Log in to reply