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.3k 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.
  • Z Offline
    Z Offline
    ZapB
    wrote on 24 Jul 2011, 13:47 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 24 Jul 2011, 14:23 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 24 Jul 2011, 17:07 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 29 Oct 2018, 10:58
        0
        • M Offline
          M Offline
          mlong
          wrote on 25 Jul 2011, 18:02 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 15 Aug 2011, 23:03 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 18 Nov 2011, 18:39 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
                24 Jul 2011, 17:07

                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 29 Oct 2018, 10:58 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

                J 1 Reply Last reply 29 Oct 2018, 12:39
                0
                • V vivekyuvan
                  29 Oct 2018, 10:58

                  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

                  J Offline
                  J Offline
                  jsulm
                  Lifetime Qt Champion
                  wrote on 29 Oct 2018, 12:39 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 30 Oct 2018, 05:23 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

                    J 1 Reply Last reply 30 Oct 2018, 05:34
                    0
                    • V vivekyuvan
                      30 Oct 2018, 05:23

                      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

                      J Offline
                      J Offline
                      jsulm
                      Lifetime Qt Champion
                      wrote on 30 Oct 2018, 05:34 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 30 Oct 2018, 12:24 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?

                        J aha_1980A 2 Replies Last reply 30 Oct 2018, 13:12
                        0
                        • V vivekyuvan
                          30 Oct 2018, 12:24

                          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?

                          J Offline
                          J Offline
                          jsulm
                          Lifetime Qt Champion
                          wrote on 30 Oct 2018, 13:12 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
                            30 Oct 2018, 12:24

                            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 30 Oct 2018, 13:43 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