Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Mobile and Embedded
  4. How to use plain C in Qt to create a GUI?
Forum Updated to NodeBB v4.3 + New Features

How to use plain C in Qt to create a GUI?

Scheduled Pinned Locked Moved Solved Mobile and Embedded
7 Posts 5 Posters 680 Views 2 Watching
  • 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.
  • D Offline
    D Offline
    dennissoem
    wrote on last edited by dennissoem
    #1

    I want to create a GUI using Qt which should use plain C code.

    There was a similar question asked 11 years ago, which wasn't answered clearly in my opinion. As things have changed over the years, can someone provide a clear and detailed explanation regarding this topic?

    J.HilkJ JonBJ 2 Replies Last reply
    0
    • D dennissoem

      I want to create a GUI using Qt which should use plain C code.

      There was a similar question asked 11 years ago, which wasn't answered clearly in my opinion. As things have changed over the years, can someone provide a clear and detailed explanation regarding this topic?

      J.HilkJ Offline
      J.HilkJ Offline
      J.Hilk
      Moderators
      wrote on last edited by
      #2

      @dennissoem

      https://isocpp.org/wiki/faq/mixing-c-and-cpp


      Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


      Q: What's that?
      A: It's blue light.
      Q: What does it do?
      A: It turns blue.

      1 Reply Last reply
      4
      • D dennissoem

        I want to create a GUI using Qt which should use plain C code.

        There was a similar question asked 11 years ago, which wasn't answered clearly in my opinion. As things have changed over the years, can someone provide a clear and detailed explanation regarding this topic?

        JonBJ Offline
        JonBJ Offline
        JonB
        wrote on last edited by JonB
        #3

        @dennissoem
        As @J-Hilk says. Write the GUI in C++ with Qt. Either add your C source files directly into the project or compile them into a library and link against it.

        1 Reply Last reply
        2
        • D Offline
          D Offline
          dennissoem
          wrote on last edited by dennissoem
          #4

          I am aware that one could easily use C in C++ code. I want to know if there are some special requirements that one should keep in mind? I see that JonB has given an answer. If someone could give a more detailed answer that would be much appreciated.

          JonBJ 1 Reply Last reply
          1
          • D dennissoem

            I am aware that one could easily use C in C++ code. I want to know if there are some special requirements that one should keep in mind? I see that JonB has given an answer. If someone could give a more detailed answer that would be much appreciated.

            JonBJ Offline
            JonBJ Offline
            JonB
            wrote on last edited by JonB
            #5

            @dennissoem
            I really don't think there is anything else to say. You cannot emit signals from C as that requires a QObject. You could probably implement a slot if you wish, since that is only a plain function, but you could equally declare the slot in C++ and just have it call the C function, so I don't see any relevance.

            There are tons of "misbehaviours" you could do in C code called from C++, whether Qt or not, but then there are also tons even if calling C++, so not much to say, nobody is going to enumerate every possible "gotcha".

            SGaistS 1 Reply Last reply
            0
            • D dennissoem has marked this topic as solved on
            • JonBJ JonB

              @dennissoem
              I really don't think there is anything else to say. You cannot emit signals from C as that requires a QObject. You could probably implement a slot if you wish, since that is only a plain function, but you could equally declare the slot in C++ and just have it call the C function, so I don't see any relevance.

              There are tons of "misbehaviours" you could do in C code called from C++, whether Qt or not, but then there are also tons even if calling C++, so not much to say, nobody is going to enumerate every possible "gotcha".

              SGaistS Offline
              SGaistS Offline
              SGaist
              Lifetime Qt Champion
              wrote on last edited by
              #6

              Hi,

              There are no particular requirements. Qt is C++ after all.

              That said, a pretty common pattern used is to create a wrapper object that will handle all the C interaction to provide a simple and clean interface.

              As an example, I wrote a long time agor a QIODevice subclass to take advantage of the D2XX driver for the ftdi chipset.

              Interested in AI ? www.idiap.ch
              Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

              1 Reply Last reply
              1
              • S Offline
                S Offline
                SimonSchroeder
                wrote on last edited by
                #7

                I don't think there is a C wrapper for Qt. And it would be quite cumbersome to write one for all of Qt that you need. It is easy to use C in C++, but it is hard the other way around. Unless you are a C++ expert don't try to write a C wrapper for Qt. You need to understand a good portion of the language standard concerning OO (the part targeted at compiler implementers) to get it right. It is much easier to learn some C++ to be able to write the GUI in C++ using Qt.

                I am not sure what reason you have to ask this question. I guess some of it might be related to embedded systems where there is not STL available. However, Qt does not use the STL internally (though there are some conversion functions). I don't think you'd need RTTI or exceptions for Qt to work properly. This would eliminate most of the problems of using C++ with Qt on embedded systems.

                1 Reply Last reply
                0

                • Login

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