Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. How to use C code in qt?

How to use C code in qt?

Scheduled Pinned Locked Moved General and Desktop
19 Posts 10 Posters 70.4k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    shakthi
    wrote on last edited by
    #4

    @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++?

    1 Reply Last reply
    0
    • Z Offline
      Z Offline
      ZapB
      wrote on last edited by
      #5

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

      Nokia Certified Qt Specialist
      Interested in hearing about Qt related work

      1 Reply Last reply
      0
      • S Offline
        S Offline
        shakthi
        wrote on last edited by
        #6

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

        1 Reply Last reply
        0
        • Z Offline
          Z Offline
          ZapB
          wrote on last edited by
          #7

          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.

          Nokia Certified Qt Specialist
          Interested in hearing about Qt related work

          1 Reply Last reply
          0
          • S Offline
            S Offline
            shakthi
            wrote on last edited by
            #8

            @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??

            1 Reply Last reply
            0
            • D Offline
              D Offline
              DenisKormalev
              wrote on last edited by
              #9

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

              V 1 Reply Last reply
              0
              • M Offline
                M Offline
                mlong
                wrote on last edited by
                #10

                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.

                Software Engineer
                My views and opinions do not necessarily reflect those of anyone -- living or dead, real or fictional -- in this universe or any other similar multiverse node. Void where prohibited. Your mileage may vary. Caveat emptor.

                1 Reply Last reply
                0
                • Z Offline
                  Z Offline
                  zester
                  wrote on last edited by
                  #11

                  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

                  1 Reply Last reply
                  0
                  • R Offline
                    R Offline
                    raja26
                    wrote on last edited by
                    #12

                    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

                    1 Reply Last reply
                    1
                    • D DenisKormalev

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

                      V Offline
                      V Offline
                      vivekyuvan
                      wrote on last edited by
                      #13

                      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

                      jsulmJ 1 Reply Last reply
                      0
                      • V vivekyuvan

                        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

                        jsulmJ Offline
                        jsulmJ Offline
                        jsulm
                        Lifetime Qt Champion
                        wrote on last edited by
                        #14

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

                        https://forum.qt.io/topic/113070/qt-code-of-conduct

                        1 Reply Last reply
                        1
                        • V Offline
                          V Offline
                          vivekyuvan
                          wrote on last edited by
                          #15

                          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

                          jsulmJ 1 Reply Last reply
                          0
                          • V vivekyuvan

                            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

                            jsulmJ Offline
                            jsulmJ Offline
                            jsulm
                            Lifetime Qt Champion
                            wrote on last edited by
                            #16

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

                            https://forum.qt.io/topic/113070/qt-code-of-conduct

                            1 Reply Last reply
                            3
                            • V Offline
                              V Offline
                              vivekyuvan
                              wrote on last edited by
                              #17

                              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?

                              jsulmJ aha_1980A 2 Replies Last reply
                              0
                              • V vivekyuvan

                                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?

                                jsulmJ Offline
                                jsulmJ Offline
                                jsulm
                                Lifetime Qt Champion
                                wrote on last edited by
                                #18

                                @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?

                                https://forum.qt.io/topic/113070/qt-code-of-conduct

                                1 Reply Last reply
                                2
                                • V vivekyuvan

                                  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?

                                  aha_1980A Offline
                                  aha_1980A Offline
                                  aha_1980
                                  Lifetime Qt Champion
                                  wrote on last edited by
                                  #19

                                  @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

                                  Qt has to stay free or it will die.

                                  1 Reply Last reply
                                  6

                                  • Login

                                  • Login or register to search.
                                  • First post
                                    Last post
                                  0
                                  • Categories
                                  • Recent
                                  • Tags
                                  • Popular
                                  • Users
                                  • Groups
                                  • Search
                                  • Get Qt Extensions
                                  • Unsolved