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. Can a Qwindow have plain c++ attribute ?
Forum Updated to NodeBB v4.3 + New Features

Can a Qwindow have plain c++ attribute ?

Scheduled Pinned Locked Moved Solved General and Desktop
8 Posts 5 Posters 473 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.
  • L Offline
    L Offline
    Louce
    wrote on last edited by
    #1

    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 value

    main.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 :

    007be038-9f0b-4be2-a46a-d9e2180096ed-image.png

    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.

    JonBJ 1 Reply Last reply
    0
    • Christian EhrlicherC Offline
      Christian EhrlicherC Offline
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on last edited by
      #2

      You don't show what getI() does.
      Also please format your code so it's readable - please use the code tags.

      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
      1
      • L Louce

        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 value

        main.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 :

        007be038-9f0b-4be2-a46a-d9e2180096ed-image.png

        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.

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

        @Louce
        Also where do the two QT App start messages come from?

        1 Reply Last reply
        0
        • L Offline
          L Offline
          Louce
          wrote on last edited by
          #4

          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 ->

          607aefda-a0c1-408c-8290-666b2902c299-image.png

          I have the feeling that it's more an error from my side due to my lack of Qt knowledge.

          Thanks again.

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

            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.

            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
            1
            • L Offline
              L Offline
              Louce
              wrote on last edited by
              #6

              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 :) .

              Pl45m4P 1 Reply Last reply
              0
              • L Louce

                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 :) .

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

                @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 like MainMenuWindow *.

                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
                

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

                ~E. W. Dijkstra

                L 1 Reply Last reply
                1
                • Pl45m4P Pl45m4

                  @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 like MainMenuWindow *.

                  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
                  
                  L Offline
                  L Offline
                  Louce
                  wrote on last edited by
                  #8

                  @Pl45m4

                  Thanks for the clarification.

                  Yes I understood that's wasn't related to Qt.

                  Thank a lot to all of you.

                  1 Reply Last reply
                  0

                  • Login

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