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 call a function from another cpp file
Qt 6.11 is out! See what's new in the release blog

How to call a function from another cpp file

Scheduled Pinned Locked Moved General and Desktop
21 Posts 3 Posters 19.9k Views 1 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.
  • ? Offline
    ? Offline
    A Former User
    wrote on last edited by
    #3

    i didnt find any post related to this question
    Please help

    1 Reply Last reply
    0
    • I Offline
      I Offline
      Ildar
      wrote on last edited by
      #4

      Please, show some problem code.
      Your function is class member?

      1 Reply Last reply
      0
      • ? Offline
        ? Offline
        A Former User
        wrote on last edited by
        #5

        Header.h

        @
        class ap
        {
        Q_OBJECT
        public slot:
        void open();
        }

        call.cpp
        include header.h
        {
        class ap:: open()
        {
        }
        }
        @

        on.cpp

        @
        include header.h
        {
        connect(openButton,SIGNAL(clicked), this, SLOT(open));
        }
        @

        [edit: added missing coding tags @ SGaist]

        1 Reply Last reply
        0
        • ? Offline
          ? Offline
          A Former User
          wrote on last edited by
          #6

          its like signal(clicked),this,slot(open)

          1 Reply Last reply
          0
          • I Offline
            I Offline
            Ildar
            wrote on last edited by
            #7

            Please, use tags for code.

            Maybe you need something like?

            @
            // call.h
            class call : public QObject
            {
            Q_OBJECT

            explicit call(QObject *parent)

            public slot:
            void open();
            }

            // call.cpp
            #include "call.h"
            #include <qdebug.h>

            call::call(QObject *parent): QObject(parent)
            {}

            call::open()
            {
            qDebug() << "CALL";
            }

            // on.h
            #include "call.h"
            class on : public QObject
            {
            Q_OBJECT
            explicit on(QObject *parent)

            signals:
            void mySignal();

            private:
            call *m_call;
            }

            // on.cpp
            on::on(QObject *parent): QObject(parent)
            {
            m_call = new call(this);

            connect(this, &on::mySignal, m_call, &call::open);

            emit mySignal();
            }
            @

            1 Reply Last reply
            0
            • ? Offline
              ? Offline
              A Former User
              wrote on last edited by
              #8

              I am geeting error at line 41
              @m_call = new call(this);@
              no instance of constructor
              no appropriate default constructor available

              1 Reply Last reply
              0
              • I Offline
                I Offline
                Ildar
                wrote on last edited by
                #9

                Sorry!) I updated my answer

                1 Reply Last reply
                0
                • ? Offline
                  ? Offline
                  A Former User
                  wrote on last edited by
                  #10

                  what u updated ???

                  1 Reply Last reply
                  0
                  • I Offline
                    I Offline
                    Ildar
                    wrote on last edited by
                    #11

                    See my above answer:

                    @
                    // call.h
                    class call : public QObject
                    {
                    Q_OBJECT

                    explicit call(QObject *parent)

                    public slot:
                    void open();
                    }
                    ...@

                    1 Reply Last reply
                    0
                    • ? Offline
                      ? Offline
                      A Former User
                      wrote on last edited by
                      #12

                      i did what u suggested but its not working.Not getting any error but not working also.
                      plz help

                      1 Reply Last reply
                      0
                      • ? Offline
                        ? Offline
                        A Former User
                        wrote on last edited by
                        #13

                        @
                        Header.h
                        class ap
                        {
                        Q_OBJECT
                        public slot:
                        void open();
                        }

                        call.cpp
                        include header.h
                        {
                        class ap:: open()
                        {
                        }
                        }
                        

                        on.h
                        include header.h
                        {
                        class on
                        private:
                        ap*m_call

                        }

                        on.cpp

                        include header.h
                        {
                        

                        m_call = new ap(this);
                        connect(openButton,SIGNAL(clicked), m_call, SLOT(open));@

                        1 Reply Last reply
                        0
                        • I Offline
                          I Offline
                          Ildar
                          wrote on last edited by
                          #14

                          Try rename file names: you must have 4 files:
                          call.h
                          call.cpp
                          on.h
                          on.cpp

                          And, please show to us all files acrtual contents, not pseudocode.

                          1 Reply Last reply
                          0
                          • ? Offline
                            ? Offline
                            A Former User
                            wrote on last edited by
                            #15

                            i added header file in the program but then also its not working i have to create instance, with this it will work i guess i did that also means i created one object of the class where the function is and put that object in connect command but its not working please help
                            can a one give me an exmaple or a link for "calling a function from other cpp file" so that i will get an idea how to do that.

                            1 Reply Last reply
                            0
                            • I Offline
                              I Offline
                              Ildar
                              wrote on last edited by
                              #16

                              Just create new project (Test) and add there files:

                              @
                              // call.h
                              #ifndef CALL_H
                              #define CALL_H

                              #include <QObject>

                              class call : public QObject
                              {
                              Q_OBJECT

                              public:
                              explicit call(QObject *parent = 0);

                              public slots:
                              void open();
                              };

                              #endif // CALL_H@

                              @
                              // call.cpp
                              #include "call.h"
                              #include <qdebug.h>

                              call::call(QObject *parent): QObject(parent)
                              {}

                              void call::open()
                              {
                              qDebug() << "CALL";
                              }
                              @

                              @
                              // on.h
                              #ifndef ON_H
                              #define ON_H

                              #include <QObject>
                              #include "call.h"

                              class on : public QObject
                              {
                              Q_OBJECT

                              public:
                              explicit on(QObject *parent = 0);

                              signals:
                              void mySignal();

                              private:
                              call *m_call;
                              };

                              #endif // ON_H
                              @

                              @
                              // on.cpp
                              #include "on.h"

                              on::on(QObject *parent): QObject(parent)
                              {
                              m_call = new call(this);

                              connect(this, &on::mySignal, m_call, &call::open);

                              emit mySignal();
                              }
                              @

                              @
                              // main.cpp
                              #include "on.h"

                              int main(int argc, char *argv[])
                              {
                              on *myOn = new on();
                              }
                              @

                              @
                              // Test.pro
                              HEADERS +=
                              call.h
                              on.h

                              SOURCES +=
                              call.cpp
                              on.cpp
                              main.cpp
                              @

                              1 Reply Last reply
                              0
                              • ? Offline
                                ? Offline
                                A Former User
                                wrote on last edited by
                                #17

                                Thanks this works but in my code its not working dont know why
                                Thanks though.

                                1 Reply Last reply
                                0
                                • ? Offline
                                  ? Offline
                                  A Former User
                                  wrote on last edited by
                                  #18

                                  i am getting error in my prog when i write
                                  @m_call=new call (this)@
                                  ''that no instance of constructor''

                                  1 Reply Last reply
                                  0
                                  • I Offline
                                    I Offline
                                    Ildar
                                    wrote on last edited by
                                    #19

                                    Simpify the code and show it.

                                    1 Reply Last reply
                                    0
                                    • ? Offline
                                      ? Offline
                                      A Former User
                                      wrote on last edited by
                                      #20

                                      ok now i know the problem when i define the class contructore it has arguments in it thats why i am not able to create object.Do you know any solution regrading this how can i create object of a class whose constructor has arguments like bool*a,QWidget parent = 0
                                      means
                                      @call(bool
                                      a ,QWidget *parent = 0,QObject *parent)@

                                      1 Reply Last reply
                                      0
                                      • I Offline
                                        I Offline
                                        Ildar
                                        wrote on last edited by
                                        #21

                                        Please, read books about C++ and Qt.

                                        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