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 in main.cpp from mainwindow.cpp Qt c++?
Forum Updated to NodeBB v4.3 + New Features

How to call a function in main.cpp from mainwindow.cpp Qt c++?

Scheduled Pinned Locked Moved Solved General and Desktop
13 Posts 5 Posters 787 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.
  • R Offline
    R Offline
    Rozerin YILDIZ
    wrote on last edited by Rozerin YILDIZ
    #1

    I have a function (called deltaOmega(&w)) in main.cpp. If one of the items qlistWidget is clicked, then this should be called this function.

    in mainwindow.cpp:

    MainWindow::MainWindow(QWidget *parent)
        : QMainWindow(parent)
        , ui(new Ui::MainWindow)
    {
        ui->setupUi(this);
    ui->listWidget->addItem("item1");
    ui->listWidget->addItem("item2");
    ui->listWidget->addItem("item3");
    
    connect(ui->listWidget, &QListWidget::itemClicked, this, &MainWindow::onListItemClicked);
    
    }
    
    void MainWindow::onListItemClicked(QListWidgetItem *item)
    {
        QString clickedItemText = item->text();
    
        if(clickedItemText == "item1")
        {
            std::cout << "item1 is clicked!" << std::endl;
        }
    }
    

    I want to call deltaOmega(&w) function in main.cpp instead of std::cout << "item1 is clicked!" << std::endl;. How can i call this function from mainwindow.cpp? And how to modify mainwindow.h?

    Thank you.

    JonBJ 1 Reply Last reply
    0
    • R Rozerin YILDIZ

      I have a function (called deltaOmega(&w)) in main.cpp. If one of the items qlistWidget is clicked, then this should be called this function.

      in mainwindow.cpp:

      MainWindow::MainWindow(QWidget *parent)
          : QMainWindow(parent)
          , ui(new Ui::MainWindow)
      {
          ui->setupUi(this);
      ui->listWidget->addItem("item1");
      ui->listWidget->addItem("item2");
      ui->listWidget->addItem("item3");
      
      connect(ui->listWidget, &QListWidget::itemClicked, this, &MainWindow::onListItemClicked);
      
      }
      
      void MainWindow::onListItemClicked(QListWidgetItem *item)
      {
          QString clickedItemText = item->text();
      
          if(clickedItemText == "item1")
          {
              std::cout << "item1 is clicked!" << std::endl;
          }
      }
      

      I want to call deltaOmega(&w) function in main.cpp instead of std::cout << "item1 is clicked!" << std::endl;. How can i call this function from mainwindow.cpp? And how to modify mainwindow.h?

      Thank you.

      JonBJ Offline
      JonBJ Offline
      JonB
      wrote on last edited by JonB
      #2

      @Rozerin-YILDIZ
      Don't put some function you need to call from mainwindow.cpp in main.cpp. Why should it be there? Nothing really ought know about that, and nothing else ought include main.h. Put it in its own file or some class and include the header.

      R 1 Reply Last reply
      0
      • JonBJ JonB

        @Rozerin-YILDIZ
        Don't put some function you need to call from mainwindow.cpp in main.cpp. Why should it be there? Nothing really ought know about that, and nothing else ought include main.h. Put it in its own file or some class and include the header.

        R Offline
        R Offline
        Rozerin YILDIZ
        wrote on last edited by
        #3

        @JonB I have created deltaOmega.h and deltaOmega.cpp for this function, i deleted this function in main.cpp. So how do I call this function in mainwindow.cpp?

        Christian EhrlicherC Pl45m4P 2 Replies Last reply
        0
        • R Rozerin YILDIZ

          @JonB I have created deltaOmega.h and deltaOmega.cpp for this function, i deleted this function in main.cpp. So how do I call this function in mainwindow.cpp?

          Christian EhrlicherC Offline
          Christian EhrlicherC Offline
          Christian Ehrlicher
          Lifetime Qt Champion
          wrote on last edited by
          #4

          @Rozerin-YILDIZ said in How to call a function in main.cpp from mainwindow.cpp Qt c++?:

          So how do I call this function in mainwindow.cpp?

          Simply call it?

          Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
          Visit the Qt Academy at https://academy.qt.io/catalog

          1 Reply Last reply
          0
          • R Rozerin YILDIZ

            @JonB I have created deltaOmega.h and deltaOmega.cpp for this function, i deleted this function in main.cpp. So how do I call this function in mainwindow.cpp?

            Pl45m4P Online
            Pl45m4P Online
            Pl45m4
            wrote on last edited by Pl45m4
            #5

            @Rozerin-YILDIZ said in How to call a function in main.cpp from mainwindow.cpp Qt c++?:

            I have created deltaOmega.h and deltaOmega.cpp for this function, i deleted this function in main.cpp. So how do I call this function in mainwindow.cpp?

            Include deltaOmega.h in mainWindow.cpp, create an instance of a deltaOmega class and call the function.

            // somewhere in mainWindow.cpp
            
            DeltaOmega deltaO;
            deltaO.deltaOmega ( ... ); 
            

            The question is, what this function does and why you pass MainWindow w to it in your main.cpp ( deltaOmega(&w)).
            This would require a circular dependency which won't work now.


            If debugging is the process of removing software bugs, then programming must be the process of putting them in.

            ~E. W. Dijkstra

            1 Reply Last reply
            0
            • R Offline
              R Offline
              Rozerin YILDIZ
              wrote on last edited by Rozerin YILDIZ
              #6

              When the deltaOmega() function was in main.cpp, it was like this:

              int main(int argc, char *argv[])
              {
                  QApplication a(argc, argv);
                  MainWindow w;
                  setTitleFramesBackground(&w);
                  setLabelTitles(&w);
                  
                  deltaOmega(&w);
              
                  w.show();
                  return a.exec();
              }
              

              The function for creating a chart.

              Pl45m4P 1 Reply Last reply
              0
              • R Rozerin YILDIZ

                When the deltaOmega() function was in main.cpp, it was like this:

                int main(int argc, char *argv[])
                {
                    QApplication a(argc, argv);
                    MainWindow w;
                    setTitleFramesBackground(&w);
                    setLabelTitles(&w);
                    
                    deltaOmega(&w);
                
                    w.show();
                    return a.exec();
                }
                

                The function for creating a chart.

                Pl45m4P Online
                Pl45m4P Online
                Pl45m4
                wrote on last edited by Pl45m4
                #7

                @Rozerin-YILDIZ said in How to call a function in main.cpp from mainwindow.cpp Qt c++?:

                int main(int argc, char *argv[])
                {
                    QApplication a(argc, argv);
                    MainWindow w;
                    setTitleFramesBackground(&w);
                    setLabelTitles(&w);
                    
                    deltaOmega(&w);
                
                    w.show();
                    return a.exec();
                }
                

                Yes, this can be guessed from the description, but why do you need to pass the MainWindow to it?
                This looks like bad design.


                If debugging is the process of removing software bugs, then programming must be the process of putting them in.

                ~E. W. Dijkstra

                R 1 Reply Last reply
                0
                • Pl45m4P Pl45m4

                  @Rozerin-YILDIZ said in How to call a function in main.cpp from mainwindow.cpp Qt c++?:

                  int main(int argc, char *argv[])
                  {
                      QApplication a(argc, argv);
                      MainWindow w;
                      setTitleFramesBackground(&w);
                      setLabelTitles(&w);
                      
                      deltaOmega(&w);
                  
                      w.show();
                      return a.exec();
                  }
                  

                  Yes, this can be guessed from the description, but why do you need to pass the MainWindow to it?
                  This looks like bad design.

                  R Offline
                  R Offline
                  Rozerin YILDIZ
                  wrote on last edited by Rozerin YILDIZ
                  #8

                  @Pl45m4 I needed to create a chart inside a function. I did this in deltaOmage(). I tried to show it in a widget in the mainwindow. How can I call this function by clicking on the qlistWidget I created? What kind of design do you suggest?

                  Pl45m4P 1 Reply Last reply
                  0
                  • R Rozerin YILDIZ

                    @Pl45m4 I needed to create a chart inside a function. I did this in deltaOmage(). I tried to show it in a widget in the mainwindow. How can I call this function by clicking on the qlistWidget I created? What kind of design do you suggest?

                    Pl45m4P Online
                    Pl45m4P Online
                    Pl45m4
                    wrote on last edited by Pl45m4
                    #9

                    @Rozerin-YILDIZ said in How to call a function in main.cpp from mainwindow.cpp Qt c++?:

                    I tried to show it in a widget in the mainwindow

                    What about

                    QWidget *deltaOmega( /* ARGS */  );
                    

                    ?

                    So the function can construct the widget and return it to MainWindow.

                    // in mainwindow something like
                    DeltaOmega *deltaO = new DeltaOmega(this);
                    QWidget *chart = deltaO->deltaOmega(....);
                    centralWidget()->layout()->addWidget(chart);
                    

                    But I dont see why you want to do this and dont create the chart in mainWindow itself.


                    If debugging is the process of removing software bugs, then programming must be the process of putting them in.

                    ~E. W. Dijkstra

                    R 1 Reply Last reply
                    0
                    • Pl45m4P Pl45m4

                      @Rozerin-YILDIZ said in How to call a function in main.cpp from mainwindow.cpp Qt c++?:

                      I tried to show it in a widget in the mainwindow

                      What about

                      QWidget *deltaOmega( /* ARGS */  );
                      

                      ?

                      So the function can construct the widget and return it to MainWindow.

                      // in mainwindow something like
                      DeltaOmega *deltaO = new DeltaOmega(this);
                      QWidget *chart = deltaO->deltaOmega(....);
                      centralWidget()->layout()->addWidget(chart);
                      

                      But I dont see why you want to do this and dont create the chart in mainWindow itself.

                      R Offline
                      R Offline
                      Rozerin YILDIZ
                      wrote on last edited by
                      #10

                      @Pl45m4 deltaOmega() function like this:

                      void deltaOmega(MainWindow *window)
                      {
                          double a[arraySize];
                          double b[arraySize];
                      
                       loop for a;
                      loop for b;
                      QLineSeries *series = new QLineSeries();
                          for (int i = 0; i < arraySize; i++) {
                              series->append(a[i], b[i]);
                          }
                      
                          QChart *chart = new QChart();
                          chart->addSeries(series);
                          chart->setTitle("Graph");
                          chart->setMargins(QMargins(0, -60, 0, 0));
                      
                          QValueAxis *axisX = new QValueAxis;
                          axisX->setTitleText("X");
                          chart->addAxis(axisX, Qt::AlignBottom);
                          series->attachAxis(axisX);
                      
                          QValueAxis *axisY = new QValueAxis;
                          axisY->setTitleText("Y");
                          chart->addAxis(axisY, Qt::AlignLeft);
                          series->attachAxis(axisY);
                      
                      
                          // Graph
                          QChartView *chartView = new QChartView(chart);
                          chartView->setRenderHint(QPainter::Antialiasing);
                      
                          QWidget *widget = window->findChild<QWidget *>("widget");
                      
                      
                          chartView->setParent(widget);
                      
                          chartView->resize(widget->size());
                         
                          chartView->setChart(chart);
                        
                      }
                      

                      If I don't need to do that, or if there is a better way to do it, I can follow your suggestion.

                      jsulmJ 1 Reply Last reply
                      0
                      • R Rozerin YILDIZ

                        @Pl45m4 deltaOmega() function like this:

                        void deltaOmega(MainWindow *window)
                        {
                            double a[arraySize];
                            double b[arraySize];
                        
                         loop for a;
                        loop for b;
                        QLineSeries *series = new QLineSeries();
                            for (int i = 0; i < arraySize; i++) {
                                series->append(a[i], b[i]);
                            }
                        
                            QChart *chart = new QChart();
                            chart->addSeries(series);
                            chart->setTitle("Graph");
                            chart->setMargins(QMargins(0, -60, 0, 0));
                        
                            QValueAxis *axisX = new QValueAxis;
                            axisX->setTitleText("X");
                            chart->addAxis(axisX, Qt::AlignBottom);
                            series->attachAxis(axisX);
                        
                            QValueAxis *axisY = new QValueAxis;
                            axisY->setTitleText("Y");
                            chart->addAxis(axisY, Qt::AlignLeft);
                            series->attachAxis(axisY);
                        
                        
                            // Graph
                            QChartView *chartView = new QChartView(chart);
                            chartView->setRenderHint(QPainter::Antialiasing);
                        
                            QWidget *widget = window->findChild<QWidget *>("widget");
                        
                        
                            chartView->setParent(widget);
                        
                            chartView->resize(widget->size());
                           
                            chartView->setChart(chart);
                          
                        }
                        

                        If I don't need to do that, or if there is a better way to do it, I can follow your suggestion.

                        jsulmJ Offline
                        jsulmJ Offline
                        jsulm
                        Lifetime Qt Champion
                        wrote on last edited by
                        #11

                        @Rozerin-YILDIZ The suggestion was to do all this inside MainWindow. Then you also don't have to do dirty things like:

                        QWidget *widget = window->findChild<QWidget *>("widget");
                        

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

                        Pl45m4P 1 Reply Last reply
                        3
                        • jsulmJ jsulm

                          @Rozerin-YILDIZ The suggestion was to do all this inside MainWindow. Then you also don't have to do dirty things like:

                          QWidget *widget = window->findChild<QWidget *>("widget");
                          
                          Pl45m4P Online
                          Pl45m4P Online
                          Pl45m4
                          wrote on last edited by Pl45m4
                          #12

                          @jsulm said in How to call a function in main.cpp from mainwindow.cpp Qt c++?:

                          The suggestion was to do all this inside MainWindow. Then you also don't have to do dirty things like:

                          @Rozerin-YILDIZ either this, or like I've written here

                          @Pl45m4 said in How to call a function in main.cpp from mainwindow.cpp Qt c++?:

                          QWidget * deltaOmega( /* ARGS */ );

                          if you really want to "construct" your QChart outside of MainWindow, then make at least some kind of factory to return the created widget. You can set it to your MainWindow within its code and don't need to pass the pointer to your MainWindow around.

                          QWidget *deltaOmega() // a.k.a. "make ChartView Widget"
                          {
                              double a[arraySize];
                              double b[arraySize];
                              loop for a;
                              loop for b;
                              QLineSeries *series = new QLineSeries();
                              for (int i = 0; i < arraySize; i++) {
                                   series->append(a[i], b[i]);
                              }
                          
                               // create new container widget for your Chart
                              QWidget *widget = new QWidget();
                          
                          
                              QChart *chart = new QChart();
                              chart->addSeries(series);
                              chart->setTitle("Graph");
                              chart->setMargins(QMargins(0, -60, 0, 0));
                          
                              QValueAxis *axisX = new QValueAxis;
                              axisX->setTitleText("X");
                              chart->addAxis(axisX, Qt::AlignBottom);
                              series->attachAxis(axisX);
                          
                              QValueAxis *axisY = new QValueAxis;
                              axisY->setTitleText("Y");
                              chart->addAxis(axisY, Qt::AlignLeft);
                              series->attachAxis(axisY);
                          
                          
                              // Graph
                              QChartView *chartView = new QChartView(chart, widget);
                          
                              chartView->setChart(chart);
                              chartView->setRenderHint(QPainter::Antialiasing);
                          
                          
                              QHBoxLayout *layout = new QHBoxLayout();
                              widget->setLayout(layout);
                              layout->addWidget(chartView);
                             
                          
                              return widget;
                          }
                          

                          then in MainWindow something like

                          DeltaOmega *deltaO = new DeltaOmega(this);
                          centralWidget()->layout()->addWidget(deltaO->deltaOmega());
                          

                          I still don't see the reason why you want to do this at all, but feel free....


                          If debugging is the process of removing software bugs, then programming must be the process of putting them in.

                          ~E. W. Dijkstra

                          R 1 Reply Last reply
                          1
                          • Pl45m4P Pl45m4

                            @jsulm said in How to call a function in main.cpp from mainwindow.cpp Qt c++?:

                            The suggestion was to do all this inside MainWindow. Then you also don't have to do dirty things like:

                            @Rozerin-YILDIZ either this, or like I've written here

                            @Pl45m4 said in How to call a function in main.cpp from mainwindow.cpp Qt c++?:

                            QWidget * deltaOmega( /* ARGS */ );

                            if you really want to "construct" your QChart outside of MainWindow, then make at least some kind of factory to return the created widget. You can set it to your MainWindow within its code and don't need to pass the pointer to your MainWindow around.

                            QWidget *deltaOmega() // a.k.a. "make ChartView Widget"
                            {
                                double a[arraySize];
                                double b[arraySize];
                                loop for a;
                                loop for b;
                                QLineSeries *series = new QLineSeries();
                                for (int i = 0; i < arraySize; i++) {
                                     series->append(a[i], b[i]);
                                }
                            
                                 // create new container widget for your Chart
                                QWidget *widget = new QWidget();
                            
                            
                                QChart *chart = new QChart();
                                chart->addSeries(series);
                                chart->setTitle("Graph");
                                chart->setMargins(QMargins(0, -60, 0, 0));
                            
                                QValueAxis *axisX = new QValueAxis;
                                axisX->setTitleText("X");
                                chart->addAxis(axisX, Qt::AlignBottom);
                                series->attachAxis(axisX);
                            
                                QValueAxis *axisY = new QValueAxis;
                                axisY->setTitleText("Y");
                                chart->addAxis(axisY, Qt::AlignLeft);
                                series->attachAxis(axisY);
                            
                            
                                // Graph
                                QChartView *chartView = new QChartView(chart, widget);
                            
                                chartView->setChart(chart);
                                chartView->setRenderHint(QPainter::Antialiasing);
                            
                            
                                QHBoxLayout *layout = new QHBoxLayout();
                                widget->setLayout(layout);
                                layout->addWidget(chartView);
                               
                            
                                return widget;
                            }
                            

                            then in MainWindow something like

                            DeltaOmega *deltaO = new DeltaOmega(this);
                            centralWidget()->layout()->addWidget(deltaO->deltaOmega());
                            

                            I still don't see the reason why you want to do this at all, but feel free....

                            R Offline
                            R Offline
                            Rozerin YILDIZ
                            wrote on last edited by
                            #13

                            @Pl45m4 Thank you so much. I created the chart in mainwindow as suggested. And also, I solved the click issue either.

                            1 Reply Last reply
                            0
                            • R Rozerin YILDIZ has marked this topic as solved on
                            • Pl45m4P Pl45m4 referenced this topic on

                            • Login

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