Solved Could you tell me how to use value in main.cpp?
-
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
-
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 hereSo 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; -
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(); }
-
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.
-
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);
-
@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
-
@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.
-
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).