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 ?
Forum Updated to NodeBB v4.3 + New Features

How to call another project's function ?

Scheduled Pinned Locked Moved Unsolved General and Desktop
14 Posts 4 Posters 2.1k 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.
  • M Mucahit

    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.

    jsulmJ Offline
    jsulmJ Offline
    jsulm
    Lifetime Qt Champion
    wrote on 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

      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.

      JonBJ Offline
      JonBJ Offline
      JonB
      wrote on 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 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;
        }
        
        jsulmJ 1 Reply Last reply
        0
        • KH-219DesignK Offline
          KH-219DesignK Offline
          KH-219Design
          wrote on 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

            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;
            }
            
            jsulmJ Offline
            jsulmJ Offline
            jsulm
            Lifetime Qt Champion
            wrote on 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 last edited by Mucahit
              #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();
              }
              
              jsulmJ 1 Reply Last reply
              0
              • M Mucahit

                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();
                }
                
                jsulmJ Offline
                jsulmJ Offline
                jsulm
                Lifetime Qt Champion
                wrote on last edited by jsulm
                #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 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?

                  jsulmJ 1 Reply Last reply
                  0
                  • M Mucahit

                    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?

                    jsulmJ Offline
                    jsulmJ Offline
                    jsulm
                    Lifetime Qt Champion
                    wrote on last edited by jsulm
                    #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 last edited by
                      #13

                      thank you

                      1 Reply Last reply
                      0
                      • KH-219DesignK Offline
                        KH-219DesignK Offline
                        KH-219Design
                        wrote on 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

                        • Login

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