Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

How to call another project's function ?



  • 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";
    }
    


  • @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.



  • 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.


  • Lifetime Qt Champion

    @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...



  • @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....



  • 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;
    }
    


  • 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).


  • Lifetime Qt Champion

    @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.



  • 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();
    }
    

  • Lifetime Qt Champion

    @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.



  • 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?


  • Lifetime Qt Champion

    @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)!



  • thank you



  • @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++.


Log in to reply