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 another project's function ?
QtWS25 Last Chance

How to call another project's function ?

Scheduled Pinned Locked Moved Unsolved General and Desktop
14 Posts 4 Posters 2.0k 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.
  • M Offline
    M Offline
    Mucahit
    wrote on 10 Feb 2021, 07:53 last edited by Mucahit 2 Oct 2021, 07:54
    #1

    Hi all,

    I want to call another project's function in my project. I reviewed many sources but I couldn't. Can you tell me with a simple example ? I don't know if this is a problem but my projects are naturally in two different folders. I would be glad if you help.

    1-) First Project : /home/pi/Desktop/kaliteform

    kaliteform.h :

    #ifndef KALITEFORM_H
    #define KALITEFORM_H
    
    #include <QMainWindow>
    
    namespace Ui {
    class kaliteform;
    }
    
    class kaliteform : public QMainWindow
    {
        Q_OBJECT
    
    public:
        explicit kaliteform(QWidget *parent = nullptr);
        ~kaliteform();
    
    private:
        Ui::kaliteform *ui;
        
    };
    
    #endif // KALITEFORM_H
    

    kaliteform.cpp :

    #include "kaliteform.h"
    #include "ui_kaliteform.h"
    
    #include<QDebug>
    
    #include"/home/pi/Desktop/untitled4/mainwindow.h"
    
    kaliteform::kaliteform(QWidget *parent) :
        QMainWindow(parent),
        ui(new Ui::kaliteform)
    {
        ui->setupUi(this);
        
        MainWindow main; // i am getting this error undefined to 'MainWindow::MainWindow(QWidget*)'
        main.testfunc();
    }
    

    2-) Second Project : /home/pi/Desktop/untitled4

    mainwindow.h :

    #ifndef MAINWINDOW_H
    #define MAINWINDOW_H
    
    #include <QMainWindow>
    
    namespace Ui {
    class MainWindow;
    }
    
    class MainWindow : public QMainWindow
    {
        Q_OBJECT
    
    public:
        explicit MainWindow(QWidget *parent = 0);
        ~MainWindow();
        void testfunc();
    
    };
    
    #endif // MAINWINDOW_H
    

    mainwindow.cpp:

    #include "mainwindow.h"
    #include "ui_mainwindow.h"
    
    #include<QDebug>
    
    MainWindow::MainWindow(QWidget *parent) :
        QMainWindow(parent),
        ui(new Ui::MainWindow)
    {
        ui->setupUi(this);
    }
    
    void MainWindow::testfunc()
    {
    	qDebug()<<"I reached the function in the first project";
    }
    
    J 1 Reply Last reply 10 Feb 2021, 07:59
    0
    • M Mucahit
      10 Feb 2021, 07:53

      Hi all,

      I want to call another project's function in my project. I reviewed many sources but I couldn't. Can you tell me with a simple example ? I don't know if this is a problem but my projects are naturally in two different folders. I would be glad if you help.

      1-) First Project : /home/pi/Desktop/kaliteform

      kaliteform.h :

      #ifndef KALITEFORM_H
      #define KALITEFORM_H
      
      #include <QMainWindow>
      
      namespace Ui {
      class kaliteform;
      }
      
      class kaliteform : public QMainWindow
      {
          Q_OBJECT
      
      public:
          explicit kaliteform(QWidget *parent = nullptr);
          ~kaliteform();
      
      private:
          Ui::kaliteform *ui;
          
      };
      
      #endif // KALITEFORM_H
      

      kaliteform.cpp :

      #include "kaliteform.h"
      #include "ui_kaliteform.h"
      
      #include<QDebug>
      
      #include"/home/pi/Desktop/untitled4/mainwindow.h"
      
      kaliteform::kaliteform(QWidget *parent) :
          QMainWindow(parent),
          ui(new Ui::kaliteform)
      {
          ui->setupUi(this);
          
          MainWindow main; // i am getting this error undefined to 'MainWindow::MainWindow(QWidget*)'
          main.testfunc();
      }
      

      2-) Second Project : /home/pi/Desktop/untitled4

      mainwindow.h :

      #ifndef MAINWINDOW_H
      #define MAINWINDOW_H
      
      #include <QMainWindow>
      
      namespace Ui {
      class MainWindow;
      }
      
      class MainWindow : public QMainWindow
      {
          Q_OBJECT
      
      public:
          explicit MainWindow(QWidget *parent = 0);
          ~MainWindow();
          void testfunc();
      
      };
      
      #endif // MAINWINDOW_H
      

      mainwindow.cpp:

      #include "mainwindow.h"
      #include "ui_mainwindow.h"
      
      #include<QDebug>
      
      MainWindow::MainWindow(QWidget *parent) :
          QMainWindow(parent),
          ui(new Ui::MainWindow)
      {
          ui->setupUi(this);
      }
      
      void MainWindow::testfunc()
      {
      	qDebug()<<"I reached the function in the first project";
      }
      
      J Offline
      J Offline
      JonB
      wrote on 10 Feb 2021, 07:59 last edited by JonB 2 Nov 2021, 09:23
      #2

      @Mucahit
      You cannot directly access code in one project's executable from another one's. You either have to merge your projects into one executable, e.g. making one a linked library/DLL, or you have to use QProcess to run the other process and then some kind of command-line arguments or IPC or input/output file to exchange information.

      1 Reply Last reply
      3
      • M Offline
        M Offline
        Mucahit
        wrote on 10 Feb 2021, 11:12 last edited by
        #3

        I know run any application directly with QProcess but I don't know call function. How can I call any function with QProcess ?

        For run application with QProcess:

        QProcess test;
        test.start("/home/pi/Desktop/untitled4");
        test.waitForFinished(-1);
        

        But for call function: I don't know.

        J J 2 Replies Last reply 10 Feb 2021, 12:03
        0
        • M Mucahit
          10 Feb 2021, 11:12

          I know run any application directly with QProcess but I don't know call function. How can I call any function with QProcess ?

          For run application with QProcess:

          QProcess test;
          test.start("/home/pi/Desktop/untitled4");
          test.waitForFinished(-1);
          

          But for call function: I don't know.

          J Offline
          J Offline
          jsulm
          Lifetime Qt Champion
          wrote on 10 Feb 2021, 12:03 last edited by
          #4

          @Mucahit said in How to call another project's function ?:

          How can I call any function with QProcess ?

          You can't. How would that work?
          You will need to specify parameters in the executable you want to start via QProcess to tell it what you want it to do...

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

          1 Reply Last reply
          2
          • M Mucahit
            10 Feb 2021, 11:12

            I know run any application directly with QProcess but I don't know call function. How can I call any function with QProcess ?

            For run application with QProcess:

            QProcess test;
            test.start("/home/pi/Desktop/untitled4");
            test.waitForFinished(-1);
            

            But for call function: I don't know.

            J Offline
            J Offline
            JonB
            wrote on 10 Feb 2021, 12:20 last edited by
            #5

            @Mucahit said in How to call another project's function ?:

            I know run any application directly with QProcess but I don't know call function. How can I call any function with QProcess ?

            That's why I wrote:

            You cannot directly access code in one project's executable from another one's.

            and the rest of my comment above telling you what your alternatives are. If it had been possible to call a function in another process, I would have said so, instead of saying it can't be done....

            1 Reply Last reply
            0
            • M Offline
              M Offline
              Mucahit
              wrote on 10 Feb 2021, 14:08 last edited by
              #6

              I understand, thank you both. I created my project as a subdirectory using this link :

              https://forum.qt.io/topic/58180/solved-create-top-level-qt-project-to-build-multiple-projects
              

              I combined my two project in a single project, but when I try to run the untitled4 project, I get an error like this:

              Cannot initialize object parameter of type 'qwidget' with an expression of type 'mainwindow'
              
              

              My code is below. Can you help me please. I'm sorry for my bad english.

              singleproject.pro:

              #-------------------------------------------------
              #
              # Project created by QtCreator 2021-02-10T16:59:50
              #
              #-------------------------------------------------
              
              QT       += core gui
              
              greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
              
              TARGET = singleproject
              TEMPLATE = app
              SUBDIRS = kaliteform\
                        untitled4\
              
              # The following define makes your compiler emit warnings if you use
              # any feature of Qt which has been marked as deprecated (the exact warnings
              # depend on your compiler). Please consult the documentation of the
              # deprecated API in order to know how to port your code away from it.
              DEFINES += QT_DEPRECATED_WARNINGS
              
              # You can also make your code fail to compile if you use deprecated APIs.
              # In order to do so, uncomment the following line.
              # You can also select to disable deprecated APIs only up to a certain version of Qt.
              #DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000    # disables all the APIs deprecated before Qt 6.0.0
              
              CONFIG += c++11
              
              SOURCES += \
                      main.cpp \
                      singleproject.cpp
              
              HEADERS += \
                      singleproject.h
              
              FORMS += \
                      singleproject.ui
              
              # Default rules for deployment.
              qnx: target.path = /tmp/$${TARGET}/bin
              else: unix:!android: target.path = /opt/$${TARGET}/bin
              !isEmpty(target.path): INSTALLS += target
              
              

              singleproject.h:

              #ifndef SINGLEPROJECT_H
              #define SINGLEPROJECT_H
              
              #include <QMainWindow>
              
              namespace Ui {
              class singleproject;
              }
              
              class singleproject : public QMainWindow
              {
                  Q_OBJECT
              
              public:
                  explicit singleproject(QWidget *parent = nullptr);
                  ~singleproject();
              
              private:
                  Ui::singleproject *ui;
              };
              
              #endif // SINGLEPROJECT_H
              

              singleproject.cpp :

              #include "singleproject.h"
              #include "ui_singleproject.h"
              
              #include"untitled4/mainwindow.h"
              
              singleproject::singleproject(QWidget *parent) :
                  QMainWindow(parent),
                  ui(new Ui::singleproject)
              {
                  ui->setupUi(this);
              
                  MainWindow m;
                  m.show(); // i am getting this error 'Cannot initialize object parameter of type 'qwidget' with an expression of type 'mainwindow''
              }
              
              singleproject::~singleproject()
              {
                  delete ui;
              }
              
              J 1 Reply Last reply 11 Feb 2021, 05:25
              0
              • K Offline
                K Offline
                KH-219Design
                wrote on 10 Feb 2021, 17:45 last edited by
                #7

                This error ("cannot initialize...") is not a Qt-specific error. This is a general error in C++ usage that can happen in any C++ program.

                By the wording of the error, it seems you are using the Clang compiler.

                It would be best to show the full (copy-pasted), verbatim, lines of the Clang output.

                C++ is case-sensitive, so please don't type out the errors in lower case (qwidget instead of QWidget). It also seems like you may have omitted some pointer notation (* asterisks) from the error.

                I think this line:

                m.show();
                

                Is not actually the most pertinent to the "Cannot initialize object" error.

                That error is (more likely) pertaining to this:

                singleproject::singleproject(QWidget *parent)
                

                And it means that in some other part of the code where the constructor is used, such as (hypothetically):

                singleproject proj(x);   // hypothetical
                

                ^^ In some scenario such as that, the x that you pass to the constructor is not of the QWidget type (and/or the compiler hasn't seen enough prior headers and declarations to know that it is).

                www.219design.com
                Software | Electrical | Mechanical | Product Design

                1 Reply Last reply
                0
                • M Mucahit
                  10 Feb 2021, 14:08

                  I understand, thank you both. I created my project as a subdirectory using this link :

                  https://forum.qt.io/topic/58180/solved-create-top-level-qt-project-to-build-multiple-projects
                  

                  I combined my two project in a single project, but when I try to run the untitled4 project, I get an error like this:

                  Cannot initialize object parameter of type 'qwidget' with an expression of type 'mainwindow'
                  
                  

                  My code is below. Can you help me please. I'm sorry for my bad english.

                  singleproject.pro:

                  #-------------------------------------------------
                  #
                  # Project created by QtCreator 2021-02-10T16:59:50
                  #
                  #-------------------------------------------------
                  
                  QT       += core gui
                  
                  greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
                  
                  TARGET = singleproject
                  TEMPLATE = app
                  SUBDIRS = kaliteform\
                            untitled4\
                  
                  # The following define makes your compiler emit warnings if you use
                  # any feature of Qt which has been marked as deprecated (the exact warnings
                  # depend on your compiler). Please consult the documentation of the
                  # deprecated API in order to know how to port your code away from it.
                  DEFINES += QT_DEPRECATED_WARNINGS
                  
                  # You can also make your code fail to compile if you use deprecated APIs.
                  # In order to do so, uncomment the following line.
                  # You can also select to disable deprecated APIs only up to a certain version of Qt.
                  #DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000    # disables all the APIs deprecated before Qt 6.0.0
                  
                  CONFIG += c++11
                  
                  SOURCES += \
                          main.cpp \
                          singleproject.cpp
                  
                  HEADERS += \
                          singleproject.h
                  
                  FORMS += \
                          singleproject.ui
                  
                  # Default rules for deployment.
                  qnx: target.path = /tmp/$${TARGET}/bin
                  else: unix:!android: target.path = /opt/$${TARGET}/bin
                  !isEmpty(target.path): INSTALLS += target
                  
                  

                  singleproject.h:

                  #ifndef SINGLEPROJECT_H
                  #define SINGLEPROJECT_H
                  
                  #include <QMainWindow>
                  
                  namespace Ui {
                  class singleproject;
                  }
                  
                  class singleproject : public QMainWindow
                  {
                      Q_OBJECT
                  
                  public:
                      explicit singleproject(QWidget *parent = nullptr);
                      ~singleproject();
                  
                  private:
                      Ui::singleproject *ui;
                  };
                  
                  #endif // SINGLEPROJECT_H
                  

                  singleproject.cpp :

                  #include "singleproject.h"
                  #include "ui_singleproject.h"
                  
                  #include"untitled4/mainwindow.h"
                  
                  singleproject::singleproject(QWidget *parent) :
                      QMainWindow(parent),
                      ui(new Ui::singleproject)
                  {
                      ui->setupUi(this);
                  
                      MainWindow m;
                      m.show(); // i am getting this error 'Cannot initialize object parameter of type 'qwidget' with an expression of type 'mainwindow''
                  }
                  
                  singleproject::~singleproject()
                  {
                      delete ui;
                  }
                  
                  J Offline
                  J Offline
                  jsulm
                  Lifetime Qt Champion
                  wrote on 11 Feb 2021, 05:25 last edited by
                  #8

                  @Mucahit said in How to call another project's function ?:

                  MainWindow m;
                  m.show();

                  Why do you create a LOCAL MainWindow instance in singleproject constructor? Are you aware that it will be deleted as soon as the constructor finishes? singleproject is already a QMainWindow, so I really don't understand what you are doing. You should only have one QMainWindow.

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

                  1 Reply Last reply
                  2
                  • M Offline
                    M Offline
                    Mucahit
                    wrote on 11 Feb 2021, 06:43 last edited by Mucahit 2 Nov 2021, 06:45
                    #9

                    Because my aim is to gather two different projects under one roof and these projects should be able to use each other's functions, because they have some common functionality to use. What I tried to do above is to be able to choose which program to start according to user preference, and my aim is to start untitled4 or kaliteform projects according to the status of clicking the buttons.I am aware that I have two main windows. So how can I overcome this problem and reach my goal? I changed untitled4's QMainwindow to QDialog but it didn't work

                    For example what I want to do:

                    #include "singleproject.h"
                    #include "ui_singleproject.h"
                    
                    #include"untitled4/mainwindow.h"
                    #include"kaliteform/kaliteform.h"
                    
                    singleproject::singleproject(QWidget *parent) :
                        QMainWindow(parent),
                        ui(new Ui::singleproject)
                    {
                        ui->setupUi(this);
                    }
                    
                    singleproject::~singleproject()
                    {
                        delete ui;
                    }
                    void singleproject::on_pushButton_rununtitledbutton_clicked()
                    {
                     MainWindow m;
                     m.show();
                    }
                    void singleproject::on_pushButton_runkaliteformbutton_clicked()
                    {
                    kaliteform kalite;
                    kalite.show();
                    }
                    
                    J 1 Reply Last reply 11 Feb 2021, 06:50
                    0
                    • M Mucahit
                      11 Feb 2021, 06:43

                      Because my aim is to gather two different projects under one roof and these projects should be able to use each other's functions, because they have some common functionality to use. What I tried to do above is to be able to choose which program to start according to user preference, and my aim is to start untitled4 or kaliteform projects according to the status of clicking the buttons.I am aware that I have two main windows. So how can I overcome this problem and reach my goal? I changed untitled4's QMainwindow to QDialog but it didn't work

                      For example what I want to do:

                      #include "singleproject.h"
                      #include "ui_singleproject.h"
                      
                      #include"untitled4/mainwindow.h"
                      #include"kaliteform/kaliteform.h"
                      
                      singleproject::singleproject(QWidget *parent) :
                          QMainWindow(parent),
                          ui(new Ui::singleproject)
                      {
                          ui->setupUi(this);
                      }
                      
                      singleproject::~singleproject()
                      {
                          delete ui;
                      }
                      void singleproject::on_pushButton_rununtitledbutton_clicked()
                      {
                       MainWindow m;
                       m.show();
                      }
                      void singleproject::on_pushButton_runkaliteformbutton_clicked()
                      {
                      kaliteform kalite;
                      kalite.show();
                      }
                      
                      J Offline
                      J Offline
                      jsulm
                      Lifetime Qt Champion
                      wrote on 11 Feb 2021, 06:50 last edited by jsulm 2 Nov 2021, 06:51
                      #10

                      @Mucahit said in How to call another project's function ?:

                      void singleproject::on_pushButton_rununtitledbutton_clicked()
                      {
                      MainWindow m;
                      m.show();
                      }
                      void singleproject::on_pushButton_runkaliteformbutton_clicked()
                      {
                      kaliteform kalite;
                      kalite.show();
                      }

                      This also will not work as m and kalite are LOCAL variables and show() is a non-blocking method. Please read about scopes in C++! You have to allocate both on the heap, not on the stack.

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

                      1 Reply Last reply
                      0
                      • M Offline
                        M Offline
                        Mucahit
                        wrote on 11 Feb 2021, 07:03 last edited by
                        #11

                        Thank you. You keep telling me it won't work and i already see it not working, but i don't know how to get it to work. Well, can you share an example code with me on how to do it?

                        J 1 Reply Last reply 11 Feb 2021, 07:17
                        0
                        • M Mucahit
                          11 Feb 2021, 07:03

                          Thank you. You keep telling me it won't work and i already see it not working, but i don't know how to get it to work. Well, can you share an example code with me on how to do it?

                          J Offline
                          J Offline
                          jsulm
                          Lifetime Qt Champion
                          wrote on 11 Feb 2021, 07:17 last edited by jsulm 2 Nov 2021, 07:17
                          #12

                          @Mucahit You should really read about scopes in C++!
                          As I wrote already you can allocate on the heap:

                          void singleproject::on_pushButton_rununtitledbutton_clicked()
                          {
                              MainWindow *m = new MainWindow();
                              m->show();
                          }
                          void singleproject::on_pushButton_runkaliteformbutton_clicked()
                          {
                              kaliteform *kalite = new kaliteform;
                              kalite->show();
                          }
                          

                          Please note that the code above does not free the memory (memory leak)!

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

                          1 Reply Last reply
                          1
                          • M Offline
                            M Offline
                            Mucahit
                            wrote on 11 Feb 2021, 08:01 last edited by
                            #13

                            thank you

                            1 Reply Last reply
                            0
                            • K Offline
                              K Offline
                              KH-219Design
                              wrote on 11 Feb 2021, 18:09 last edited by
                              #14

                              @Mucahit , I share with you this little fable I just invented in the spirit of making amends. Some forum threads (here and elsewhere, such as StackOverflow) seem to inevitably take on an "us versus them" ("experts versus n00bs") aura, and I wish I could avoid it more easily. I offer this in the spirit of helping both sides (expert, n00b, and those in-between) to understand each other better.

                              You are trying to play a chess game against a chess opponent named Qt. You pick up a rook and move it to a part of the chessboard, and the chessboard (let's assume it is a "smart chess board" with electronic detection of your move)... the chessboard beeps and balks and the game halts.

                              You show us a picture of your chessboard and we go "well of course it failed, you cannot move the rook like that, because it's against the rules of chess." Next you pick up a pawn and place it down somewhere. The chessboard beeps and fails again. Now we tell you: "well of course! You aren't allowed to move a pawn in that way either."

                              What is the solution in order to end this frustration?

                              The solution is learn the rules of chess (C++) before attempting to apply chess (C++) to this particular adversary (Qt).

                              (Apologies to Qt for casting it in an adversarial role, but all frameworks appear that way on some days...)

                              You could, of course, persist in learning both chess and Qt via this repeated method of trial and error. However, learning in this way is unnecessarily slow, frustrating, fragile, and incomplete. People will keep telling you "stop, don't do that!" and it can be demoralizing for you and for the person warning you. Those of us trying to help are scratching our heads in bafflement thinking "why don't they just read a booklet on the rules of chess (C++)?"

                              On the topic of reading the rules of C++:

                              While Qt changes yearly (or more) and is often underdocumented (or documented in hard-to-find corners of the web), the core of C++ (especially if you set aside concurrency) has been stable for decades and is documented in abundance in all kinds of media and in all the world's major spoken languages.

                              There is a huge return on investment that you will enjoy by stepping back briefly to study C++.

                              www.219design.com
                              Software | Electrical | Mechanical | Product Design

                              1 Reply Last reply
                              2

                              6/14

                              10 Feb 2021, 14:08

                              • Login

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