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. Could you tell me how to use value in main.cpp?
Forum Updated to NodeBB v4.3 + New Features

Could you tell me how to use value in main.cpp?

Scheduled Pinned Locked Moved Solved General and Desktop
8 Posts 4 Posters 799 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 Offline
    M Offline
    masayoshi
    wrote on last edited by
    #1

    Could you modify my code.?
    I would like to see " MainWindow : I have a : "dog"

    // mainwindow.cpp
    
    #include "mainwindow.h"
    #include "ui_mainwindow.h"
    
    MainWindow::MainWindow(QWidget *parent) :
        QMainWindow(parent),
        ui(new Ui::MainWindow)
    {
        ui->setupUi(this);
        qDebug() << "MainWindow : I have a  : " << pet;
    }
    
    MainWindow::~MainWindow()
    {
        delete ui;
        qDebug() << "~MainWindow : I have a  : " << pet;
    }
    
    
    // main.cpp
    
    #include "mainwindow.h"
    #include <QApplication>
    
    int main(int argc, char *argv[])
    {
        QApplication a(argc, argv);
    
        qDebug() << "Main";
        MainWindow w;   
        w.pet = "dog";
        w.show();
    
        return a.exec();
    }
    
    
    // mainwindow.h
    
    #ifndef MAINWINDOW_H
    #define MAINWINDOW_H
    
    #include <QMainWindow>
    #include <QtDebug>
    
    namespace Ui {
    class MainWindow;
    }
    
    class MainWindow : public QMainWindow
    {
        Q_OBJECT
    
    public:
        explicit MainWindow(QWidget *parent = nullptr);
        ~MainWindow();
        QString pet;
    
    private:
        Ui::MainWindow *ui;
    
    };
    
    #endif // MAINWINDOW_H
    
    
    1 Reply Last reply
    0
    • mrjjM Offline
      mrjjM Offline
      mrjj
      Lifetime Qt Champion
      wrote on last edited by mrjj
      #2

      Hi
      You print the
      qDebug() << "MainWindow : I have a : " << pet;
      before you set pet to dog.

      ..main..
      MainWindow w; <<< you print here
      w.pet = "dog"; << but set it first here

      So you can give it via the constructor ( also add in .h )
      explicit MainWindow(QWidget *parent = nullptr, QString petType);

      MainWindow::MainWindow(QWidget *parent, QString petType) :
          QMainWindow(parent), pet(pettype) <<< set it
          ui(new Ui::MainWindow)
      {
      ....
      
      int main(int argc, char *argv[])
      {
          QApplication a(argc, argv);
      
          qDebug() << "Main";
          MainWindow w(nullptr,"dog");   
          w.show();
          return a.exec();
      }
      
      

      Or
      you can add a function setPet(QString petType) that both sets
      the pet variable and do
      qDebug() << "MainWindow : I have a : " << pet;

      M 1 Reply Last reply
      4
      • mrjjM mrjj

        Hi
        You print the
        qDebug() << "MainWindow : I have a : " << pet;
        before you set pet to dog.

        ..main..
        MainWindow w; <<< you print here
        w.pet = "dog"; << but set it first here

        So you can give it via the constructor ( also add in .h )
        explicit MainWindow(QWidget *parent = nullptr, QString petType);

        MainWindow::MainWindow(QWidget *parent, QString petType) :
            QMainWindow(parent), pet(pettype) <<< set it
            ui(new Ui::MainWindow)
        {
        ....
        
        int main(int argc, char *argv[])
        {
            QApplication a(argc, argv);
        
            qDebug() << "Main";
            MainWindow w(nullptr,"dog");   
            w.show();
            return a.exec();
        }
        
        

        Or
        you can add a function setPet(QString petType) that both sets
        the pet variable and do
        qDebug() << "MainWindow : I have a : " << pet;

        M Offline
        M Offline
        masayoshi
        wrote on last edited by
        #3

        Thank you for reply.
        Sorry , I can not modify code well.
        Please correct my code.

        // mainwindow.cpp
        
        

        #include "mainwindow.h"
        #include "ui_mainwindow.h"

        MainWindow::MainWindow(QWidget *parent, QString petType) :
        QMainWindow(parent), pet (petType),
        ui(new Ui::MainWindow)

        //MainWindow::MainWindow(QWidget *parent) :
        // QMainWindow(parent),
        // ui(new Ui::MainWindow)
        {
        ui->setupUi(this);
        qDebug() << "MainWindow : I have a : " << pet;
        }

        MainWindow::~MainWindow()
        {
        delete ui;
        qDebug() << "~MainWindow : I have a : " << pet;
        }

        // mainwindow.h
        
        #ifndef MAINWINDOW_H
        #define MAINWINDOW_H
        
        #include <QMainWindow>
        #include <QtDebug>
        
        namespace Ui {
        class MainWindow;
        }
        
        class MainWindow : public QMainWindow
        {
            Q_OBJECT
        
        public:
        //    explicit MainWindow(QWidget *parent = nullptr);
        
            explicit MainWindow(QWidget *parent = nullptr, QString petType);
        
            ~MainWindow();
            QString pet;
        
        private:
            Ui::MainWindow *ui;
        
        };
        
        #endif // MAINWINDOW_H
        
        
        // main.cpp
        
        #include "mainwindow.h"
        #include <QApplication>
        
        int main(int argc, char *argv[])
        {
            QApplication a(argc, argv);
        
            qDebug() << "Main";
            MainWindow w(nullptr,"dog");
        
            w.show();
        
            return a.exec();
        }
        
        
        1 Reply Last reply
        0
        • mrjjM Offline
          mrjjM Offline
          mrjj
          Lifetime Qt Champion
          wrote on last edited by mrjj
          #4

          Hi
          you seem to have done it right.
          we add a new parameter to the constructor of main window ( in .h)
          explicit MainWindow(QWidget *parent = nullptr, QString petType);

          and set our internal variable with it ( in .cpp)
          MainWindow::MainWindow(QWidget *parent, QString petType) :
          QMainWindow(parent), pet (petType), <<<< this is actually like pet = petType;
          ui(new Ui::MainWindow)
          ...

          and from main, when we do
          MainWindow w(nullptr,"dog");

          that "dog" is petType and we copy it to our variable in the class called pet.

          M 1 Reply Last reply
          3
          • mrjjM mrjj

            Hi
            you seem to have done it right.
            we add a new parameter to the constructor of main window ( in .h)
            explicit MainWindow(QWidget *parent = nullptr, QString petType);

            and set our internal variable with it ( in .cpp)
            MainWindow::MainWindow(QWidget *parent, QString petType) :
            QMainWindow(parent), pet (petType), <<<< this is actually like pet = petType;
            ui(new Ui::MainWindow)
            ...

            and from main, when we do
            MainWindow w(nullptr,"dog");

            that "dog" is petType and we copy it to our variable in the class called pet.

            M Offline
            M Offline
            masayoshi
            wrote on last edited by masayoshi
            #5

            Thank you.
            I got the following error.
            Sorry , i need your help.

            //your code here
            make: stopped in /usr/home/masayoshi/Qt/build-sample-Desktop-Debug
            --- moc_mainwindow.o ---
            In file included from moc_mainwindow.cpp:9:
            ./../sample/mainwindow.h:18:60: error: missing default argument on parameter 'petType'
                explicit MainWindow(QWidget *parent = nullptr, QString petType);
                                                                       ^
            
            //your code here
            
            --- main.o ---
            In file included from ../sample/main.cpp:1:
            ../sample/mainwindow.h:18:60: error: missing default argument on parameter 'petType'
                explicit MainWindow(QWidget *parent = nullptr, QString petType);
            
            aha_1980A 1 Reply Last reply
            0
            • M masayoshi

              Thank you.
              I got the following error.
              Sorry , i need your help.

              //your code here
              make: stopped in /usr/home/masayoshi/Qt/build-sample-Desktop-Debug
              --- moc_mainwindow.o ---
              In file included from moc_mainwindow.cpp:9:
              ./../sample/mainwindow.h:18:60: error: missing default argument on parameter 'petType'
                  explicit MainWindow(QWidget *parent = nullptr, QString petType);
                                                                         ^
              
              //your code here
              
              --- main.o ---
              In file included from ../sample/main.cpp:1:
              ../sample/mainwindow.h:18:60: error: missing default argument on parameter 'petType'
                  explicit MainWindow(QWidget *parent = nullptr, QString petType);
              
              aha_1980A Offline
              aha_1980A Offline
              aha_1980
              Lifetime Qt Champion
              wrote on last edited by
              #6

              @masayoshi said in Could you tell me how to use value in main.cpp?:

              explicit MainWindow(QWidget *parent = nullptr, QString petType);

              You have to change the declaration in mainwindow.h to this:

              explicit MainWindow(QWidget *parent, QString petType);

              Then you can call it from main.cpp like this: MainWindow w(nullptr,"dog");

              Regards

              Qt has to stay free or it will die.

              M 1 Reply Last reply
              3
              • aha_1980A aha_1980

                @masayoshi said in Could you tell me how to use value in main.cpp?:

                explicit MainWindow(QWidget *parent = nullptr, QString petType);

                You have to change the declaration in mainwindow.h to this:

                explicit MainWindow(QWidget *parent, QString petType);

                Then you can call it from main.cpp like this: MainWindow w(nullptr,"dog");

                Regards

                M Offline
                M Offline
                masayoshi
                wrote on last edited by
                #7

                @aha_1980 said in Could you tell me how to use value in main.cpp?:

                explicit MainWindow(QWidget *parent, QString petType);

                It works. Thank you.

                1 Reply Last reply
                1
                • SGaistS Offline
                  SGaistS Offline
                  SGaist
                  Lifetime Qt Champion
                  wrote on last edited by
                  #8

                  Hi,

                  Side note, it's usual (but not mandatory) to put the parent parameter as last one with a default value of nullptr (unless you really need a parent in which case, don't give it a default value).

                  Interested in AI ? www.idiap.ch
                  Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                  1 Reply Last reply
                  3

                  • Login

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