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. Use a self-made C function in QT
Forum Updated to NodeBB v4.3 + New Features

Use a self-made C function in QT

Scheduled Pinned Locked Moved Solved General and Desktop
12 Posts 6 Posters 2.7k Views 4 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.
  • K koahnig

    @EL_Mambo

    You cannot use simple C functions as slots for signals. You need to have a class derived from QObject which holds also the Q_OBJECT macro. Please have a look to signals and slots.

    Also it is always a good choice to familiarize with some of the examples. Sometimes you find one which resembles closely what you like to achieve. So you might have a good jumpstart and you can divert from there.

    If you like to use Qt C is not sufficient enough and you have to understand some of the basics of C++.

    kshegunovK Offline
    kshegunovK Offline
    kshegunov
    Moderators
    wrote on last edited by kshegunov
    #3

    @koahnig said in Use a self-made C function in QT:

    You cannot use simple C functions as slots for signals.

    Sure you can, example:

    void aboutToQuitHandler()
    {
    }
    
    int main(int argc, char *argv[])
    {
        QCoreApplication app(argc, argv);
      
        QObject::connect(&app, &QCoreApplication::aboutToQuit, aboutToQuitHandler);
        QTimer::singleShot(0, &app, &QCoreApplication::quit);
    
        return app.exec();
    }
    

    Lambdas are (almost) quite the same as a plain C functions.

    @EL_Mambo said in Use a self-made C function in QT:

    Of course, as i expected, Qt doesn't know what "e" is... So here i am, totally stuck, not managing to call my function when needed :/

    Use the Qt 5 connection syntax and std::bind to bind the local variable as an argument to the slot. Also is there a specific reason to use extern "C"? If not then you should just remove it.

    Read and abide by the Qt Code of Conduct

    aha_1980A K 2 Replies Last reply
    4
    • kshegunovK kshegunov

      @koahnig said in Use a self-made C function in QT:

      You cannot use simple C functions as slots for signals.

      Sure you can, example:

      void aboutToQuitHandler()
      {
      }
      
      int main(int argc, char *argv[])
      {
          QCoreApplication app(argc, argv);
        
          QObject::connect(&app, &QCoreApplication::aboutToQuit, aboutToQuitHandler);
          QTimer::singleShot(0, &app, &QCoreApplication::quit);
      
          return app.exec();
      }
      

      Lambdas are (almost) quite the same as a plain C functions.

      @EL_Mambo said in Use a self-made C function in QT:

      Of course, as i expected, Qt doesn't know what "e" is... So here i am, totally stuck, not managing to call my function when needed :/

      Use the Qt 5 connection syntax and std::bind to bind the local variable as an argument to the slot. Also is there a specific reason to use extern "C"? If not then you should just remove it.

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

      @kshegunov

      Also is there a specific reason to use extern "C"? If not then you should just remove it.

      If the function is implemented in a C file, then yes. Reason is the C++ compiler name mangling.

      so extern C should stay.

      Qt has to stay free or it will die.

      kshegunovK 1 Reply Last reply
      0
      • aha_1980A aha_1980

        @kshegunov

        Also is there a specific reason to use extern "C"? If not then you should just remove it.

        If the function is implemented in a C file, then yes. Reason is the C++ compiler name mangling.

        so extern C should stay.

        kshegunovK Offline
        kshegunovK Offline
        kshegunov
        Moderators
        wrote on last edited by
        #5

        @aha_1980 said in Use a self-made C function in QT:

        Reason is the C++ compiler name mangling.

        Which would be irrelevant if you don't intend to export the symbols with undecorated ABI, which is the "specific reason" I was asking about. (note that the linkage specifier is around the actual definition)

        Read and abide by the Qt Code of Conduct

        1 Reply Last reply
        0
        • Pablo J. RoginaP Offline
          Pablo J. RoginaP Offline
          Pablo J. Rogina
          wrote on last edited by
          #6

          @EL_Mambo I was able to call a function contained in a .c file directly from the signal of a Qt widget using the new syntax of connecting signal to simple functions

          function.h

          #ifndef FUNCTION_H
          #define FUNCTION_H
          
          void b_and_w();
          
          #endif // FUNCTION_H
          

          function.c

          #include "function.h"
          #include <stdio.h>
          
          void b_and_w()
          {
              fprintf(stderr, "C function called\n");
          }
          

          In mainwindow.cpp (MainWindow just contains a QPushButton called btnCallC)

          ...
          #ifdef __cplusplus
          extern "C" {
          #endif
          
          #include "function.h"
          
          #ifdef __cplusplus
          }
          #endif
          ...
          MainWindow::MainWindow(QWidget *parent) :
              QMainWindow(parent),
              ui(new Ui::MainWindow)
          {
              ui->setupUi(this);
              connect(ui->btnCallC, &QPushButton::clicked, b_and_w);
          }
          

          you may need to play a little bit to pass the struct you mentioned before. See @kshegunov suggestion about using std:bind

          Upvote the answer(s) that helped you solve the issue
          Use "Topic Tools" button to mark your post as Solved
          Add screenshots via postimage.org
          Don't ask support requests via chat/PM. Please use the forum so others can benefit from the solution in the future

          1 Reply Last reply
          1
          • kshegunovK kshegunov

            @koahnig said in Use a self-made C function in QT:

            You cannot use simple C functions as slots for signals.

            Sure you can, example:

            void aboutToQuitHandler()
            {
            }
            
            int main(int argc, char *argv[])
            {
                QCoreApplication app(argc, argv);
              
                QObject::connect(&app, &QCoreApplication::aboutToQuit, aboutToQuitHandler);
                QTimer::singleShot(0, &app, &QCoreApplication::quit);
            
                return app.exec();
            }
            

            Lambdas are (almost) quite the same as a plain C functions.

            @EL_Mambo said in Use a self-made C function in QT:

            Of course, as i expected, Qt doesn't know what "e" is... So here i am, totally stuck, not managing to call my function when needed :/

            Use the Qt 5 connection syntax and std::bind to bind the local variable as an argument to the slot. Also is there a specific reason to use extern "C"? If not then you should just remove it.

            K Offline
            K Offline
            koahnig
            wrote on last edited by
            #7

            @kshegunov said in Use a self-made C function in QT:

            @koahnig said in Use a self-made C function in QT:

            You cannot use simple C functions as slots for signals.

            Sure you can, example:

            void aboutToQuitHandler()
            {
            }
            
            int main(int argc, char *argv[])
            {
                QCoreApplication app(argc, argv);
              
                QObject::connect(&app, &QCoreApplication::aboutToQuit, aboutToQuitHandler);
                QTimer::singleShot(0, &app, &QCoreApplication::quit);
            
                return app.exec();
            }
            

            Lambdas are (almost) quite the same as a plain C functions.

            Oops, I admit that I was wrong here.

            Anyway you are using a functor here.

            Am I correct that you require the functor-type and cannot use the macro style as at top of post?

            Vote the answer(s) that helped you to solve your issue(s)

            kshegunovK 1 Reply Last reply
            0
            • Pablo J. RoginaP Offline
              Pablo J. RoginaP Offline
              Pablo J. Rogina
              wrote on last edited by
              #8

              @koahnig said in Use a self-made C function in QT:

              Am I correct that you require the functor-type and cannot use the macro style as at top of post?

              Yes, you're right in that case and that's one of the benefits of the new syntax for signals & slots.

              Upvote the answer(s) that helped you solve the issue
              Use "Topic Tools" button to mark your post as Solved
              Add screenshots via postimage.org
              Don't ask support requests via chat/PM. Please use the forum so others can benefit from the solution in the future

              K 1 Reply Last reply
              0
              • K koahnig

                @kshegunov said in Use a self-made C function in QT:

                @koahnig said in Use a self-made C function in QT:

                You cannot use simple C functions as slots for signals.

                Sure you can, example:

                void aboutToQuitHandler()
                {
                }
                
                int main(int argc, char *argv[])
                {
                    QCoreApplication app(argc, argv);
                  
                    QObject::connect(&app, &QCoreApplication::aboutToQuit, aboutToQuitHandler);
                    QTimer::singleShot(0, &app, &QCoreApplication::quit);
                
                    return app.exec();
                }
                

                Lambdas are (almost) quite the same as a plain C functions.

                Oops, I admit that I was wrong here.

                Anyway you are using a functor here.

                Am I correct that you require the functor-type and cannot use the macro style as at top of post?

                kshegunovK Offline
                kshegunovK Offline
                kshegunov
                Moderators
                wrote on last edited by
                #9

                @koahnig said in Use a self-made C function in QT:

                Anyway you are using a functor here.

                A functor is a function with an object attached, so if you don't have data it is simply a function. :)

                Am I correct that you require the functor-type and cannot use the macro style as at top of post?

                Yes, Qt wraps the pointer you pass it in a special object (using some template black magic to get the arguments right) that it uses to actually execute it when needed. This will not work with the Qt4 syntax, as there the resolution is done in runtime through the meta-object system.

                Read and abide by the Qt Code of Conduct

                1 Reply Last reply
                0
                • SGaistS Offline
                  SGaistS Offline
                  SGaist
                  Lifetime Qt Champion
                  wrote on last edited by
                  #10

                  Hi,

                  In addition to what my fellow wrote: you can't connect a signal with fewer parameters than the slot you want to call unless it has default arguments. It's also not possible with the new syntax but you can use a lambda as an alternative.

                  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
                  • Pablo J. RoginaP Pablo J. Rogina

                    @koahnig said in Use a self-made C function in QT:

                    Am I correct that you require the functor-type and cannot use the macro style as at top of post?

                    Yes, you're right in that case and that's one of the benefits of the new syntax for signals & slots.

                    K Offline
                    K Offline
                    koahnig
                    wrote on last edited by
                    #11

                    @Pablo-J.-Rogina @kshegunov @SGaist

                    Well, at least I can use as excuse that I learnd signal-slot functionality way back with Qt 4.3 (probably?) and there was only the macro version possible. And apparently, I shall give up my stubbornness and finally stash or trash my Qt 5.4 version, which is still a bit restricted with functor-based syntax on connect. ;)

                    Vote the answer(s) that helped you to solve your issue(s)

                    1 Reply Last reply
                    1
                    • E Offline
                      E Offline
                      EL_Mambo
                      wrote on last edited by
                      #12

                      Thanks everyone for your answers ! I should be able to do what i want with all the ressources you gave me :)

                      1 Reply Last reply
                      2

                      • Login

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