Can a Qwindow have plain c++ attribute ?
-
Hi there.
As far as i search it seems that I don't have a regular behavior on Qt.
I'm trying to play with attribute in a window. At the moment a simple int.
I have a int i in my window attribute and I set it inside the constructor.
but I soon as i leave the constructor my int seems to have a random valuemain.cpp int i = 1; QApplication a(argc, argv); MainMenuWindow w = new MainMenuWindow(i); w.show();
Window.h MainMenuWindow(int, QWidget *parent = nullptr); void setI(int i); int getI(); private slots: void on_pushButton_3_clicked(); private: Ui::MainMenuWindow *ui; int i;
Window.cpp MainMenuWindow::MainMenuWindow(int i,QWidget *parent) : QMainWindow(parent) , ui(new Ui::MainMenuWindow) { ui->setupUi(this); this->i = i; std::cout << "Test in constructor" << std::endl; std::cout << this->i << std::endl; } void MainMenuWindow::on_pushButton_3_clicked() { std::cout << "test with getI" << std::endl; std::cout << this->getI() << std::endl; std::cout << "test with attribute" << std::endl; std::cout << this->i << std::endl; }
And when I start the app and click on the push buton 3 :
I'm a bit confused here as it seems to behave like this with other attributes types.
Hope you can light me on this.
Good day to you all.
-
You don't show what getI() does.
Also please format your code so it's readable - please use the code tags. -
Sorry to both of you I did use to code tag but i removed a lot of irrelevant code from my file to be cleaner for you.
I'll try again
Here's the main.cpp
#include "mainmenuwindow.h" #include "iostream" #include <QApplication> int main(int argc, char *argv[]) { int i = 1; QApplication a(argc, argv); MainMenuWindow w = new MainMenuWindow(i); w.show(); return a.exec(); }
Here's the MainMenuWindow.h
#ifndef MAINMENUWINDOW_H #define MAINMENUWINDOW_H #include <QMainWindow> #include <QLabel> QT_BEGIN_NAMESPACE namespace Ui { class MainMenuWindow; } QT_END_NAMESPACE class MainMenuWindow : public QMainWindow { Q_OBJECT public: MainMenuWindow(QWidget *parent = nullptr); MainMenuWindow(int, QWidget *parent = nullptr); ~MainMenuWindow(); void setI(int i); int getI(); private slots: void on_pushButton_3_clicked(); private: Ui::MainMenuWindow *ui; int i; }; #endif // MAINMENUWINDOW_H
Here's the MainMenuWindow.cpp
#include "mainmenuwindow.h" #include "ui_mainmenuwindow.h" #include <string> #include <iostream> MainMenuWindow::MainMenuWindow(QWidget *parent) : QMainWindow(parent) , ui(new Ui::MainMenuWindow) { ui->setupUi(this); } MainMenuWindow::MainMenuWindow(int i,QWidget *parent) : QMainWindow(parent) , ui(new Ui::MainMenuWindow) { ui->setupUi(this); this->i = i; std::cout << "Test in constructor" << std::endl; std::cout << this->i << std::endl; } MainMenuWindow::~MainMenuWindow() { delete ui; } void MainMenuWindow::setI(int i) { this->i = i; } int MainMenuWindow::getI() { return this->i; } void MainMenuWindow::on_pushButton_3_clicked() { std::cout << "test with getI" << std::endl; std::cout << this->getI() << std::endl; std::cout << "test with attribute" << std::endl; std::cout << this->i << std::endl; }
Result after starting the app and cliking on the button 3 ->
I have the feeling that it's more an error from my side due to my lack of Qt knowledge.
Thanks again.
-
Hi,
Nothing stands out a fundamentally wrong (except the .show call as it should be a -> since it's a pointer).
One thing you should add is the const qualifier for your getter.
-
well .... it was just that.
with this change it's working. ;
int main(int argc, char *argv[]) { int i = 1; QApplication a(argc, argv); MainMenuWindow* w = new MainMenuWindow(i); w->show(); return a.exec(); }
apparantly Qt compute a non pointer with " = new MainMenuWindow() and then accept the w.show but I guess he went crazy after.
Quick question, Do I have to declare it MainMenuWindow* w like above, ore the fact dans I write = new MainMenuWindow makes it automaticaly a pointer ?
Thanks anyway your help :) .
-
@Louce said in Can a Qwindow have plain c++ attribute ?:
Quick question, Do I have to declare it MainMenuWindow* w like above, ore the fact dans I write = new MainMenuWindow makes it automaticaly a pointer ?
Using
new...
-"something" allocates memory on heap and returns/assigns the pointer to that memory. So you need a variable, which is a pointer likeMainMenuWindow *
.MainMenuWindow w = new MainMenuWindow;
wont work.BTW: This has nothing to do with Qt. This is how C++ is.
int i = new int(2); // WRONG // If you dont need a ptr, use stack allocation int i = 2; int * j = new int(2); // This works