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. Program is crashing
Forum Updated to NodeBB v4.3 + New Features

Program is crashing

Scheduled Pinned Locked Moved Unsolved General and Desktop
15 Posts 5 Posters 665 Views 3 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.
  • T Offline
    T Offline
    thomasGl
    wrote on last edited by
    #1

    hey guys. What am i doing wrong. the Program starts only when i delete "beendeButton -> setGeometry(30,30,30,30);".

    #ifndef MAINWINDOW_H
    #define MAINWINDOW_H
    
    #include <QMainWindow>
    #include <qlabel.h>
    #include <QPushButton>
    
    class MainWindow : public QMainWindow
    {
        Q_OBJECT
    
    public:
        MainWindow(QWidget *parent = 0, QLabel *const helloWorldLabel = nullptr, QPushButton *const beendeButton = nullptr);
        ~MainWindow();
    
    private:
        QLabel *const helloWorldLabel;
        QPushButton *const beendeButton;
    
    
    };
    
    
    
    
    #endif // MAINWINDOW_H
    
    #include "mainwindow.h"
    
    
    MainWindow::MainWindow(QWidget *parent, QLabel *const helloWorldLabel, QPushButton *const beendeButton):QMainWindow(parent),helloWorldLabel(new QLabel("Hello World", this)), beendeButton(new QPushButton("Beenden", this))
    
    {
    
      beendeButton -> setGeometry(30,30,30,30);
    
       //Verbindung des Beenden-Buttons mit der close()-Methode des MainWindow
       connect(beendeButton, &QPushButton::clicked, this, &MainWindow::close);
    
    }
    
    MainWindow::~MainWindow()
    {
        delete helloWorldLabel;
        delete beendeButton;
    }
    
    
    
    #include "mainwindow.h"
    #include <QApplication>
    
    int main(int argc, char *argv[])
    {
        QApplication a(argc, argv);
    
    
    
      MainWindow mainWindow;
      mainWindow.show();
    
      return a.exec();
    }
    
    JonBJ Pl45m4P 2 Replies Last reply
    0
    • T thomasGl

      hey guys. What am i doing wrong. the Program starts only when i delete "beendeButton -> setGeometry(30,30,30,30);".

      #ifndef MAINWINDOW_H
      #define MAINWINDOW_H
      
      #include <QMainWindow>
      #include <qlabel.h>
      #include <QPushButton>
      
      class MainWindow : public QMainWindow
      {
          Q_OBJECT
      
      public:
          MainWindow(QWidget *parent = 0, QLabel *const helloWorldLabel = nullptr, QPushButton *const beendeButton = nullptr);
          ~MainWindow();
      
      private:
          QLabel *const helloWorldLabel;
          QPushButton *const beendeButton;
      
      
      };
      
      
      
      
      #endif // MAINWINDOW_H
      
      #include "mainwindow.h"
      
      
      MainWindow::MainWindow(QWidget *parent, QLabel *const helloWorldLabel, QPushButton *const beendeButton):QMainWindow(parent),helloWorldLabel(new QLabel("Hello World", this)), beendeButton(new QPushButton("Beenden", this))
      
      {
      
        beendeButton -> setGeometry(30,30,30,30);
      
         //Verbindung des Beenden-Buttons mit der close()-Methode des MainWindow
         connect(beendeButton, &QPushButton::clicked, this, &MainWindow::close);
      
      }
      
      MainWindow::~MainWindow()
      {
          delete helloWorldLabel;
          delete beendeButton;
      }
      
      
      
      #include "mainwindow.h"
      #include <QApplication>
      
      int main(int argc, char *argv[])
      {
          QApplication a(argc, argv);
      
      
      
        MainWindow mainWindow;
        mainWindow.show();
      
        return a.exec();
      }
      
      JonBJ Offline
      JonBJ Offline
      JonB
      wrote on last edited by JonB
      #2

      @thomasGl ...because you never actually set beendeButton to anything (other than its default value nullptr)...?

      1 Reply Last reply
      0
      • T thomasGl

        hey guys. What am i doing wrong. the Program starts only when i delete "beendeButton -> setGeometry(30,30,30,30);".

        #ifndef MAINWINDOW_H
        #define MAINWINDOW_H
        
        #include <QMainWindow>
        #include <qlabel.h>
        #include <QPushButton>
        
        class MainWindow : public QMainWindow
        {
            Q_OBJECT
        
        public:
            MainWindow(QWidget *parent = 0, QLabel *const helloWorldLabel = nullptr, QPushButton *const beendeButton = nullptr);
            ~MainWindow();
        
        private:
            QLabel *const helloWorldLabel;
            QPushButton *const beendeButton;
        
        
        };
        
        
        
        
        #endif // MAINWINDOW_H
        
        #include "mainwindow.h"
        
        
        MainWindow::MainWindow(QWidget *parent, QLabel *const helloWorldLabel, QPushButton *const beendeButton):QMainWindow(parent),helloWorldLabel(new QLabel("Hello World", this)), beendeButton(new QPushButton("Beenden", this))
        
        {
        
          beendeButton -> setGeometry(30,30,30,30);
        
           //Verbindung des Beenden-Buttons mit der close()-Methode des MainWindow
           connect(beendeButton, &QPushButton::clicked, this, &MainWindow::close);
        
        }
        
        MainWindow::~MainWindow()
        {
            delete helloWorldLabel;
            delete beendeButton;
        }
        
        
        
        #include "mainwindow.h"
        #include <QApplication>
        
        int main(int argc, char *argv[])
        {
            QApplication a(argc, argv);
        
        
        
          MainWindow mainWindow;
          mainWindow.show();
        
          return a.exec();
        }
        
        Pl45m4P Offline
        Pl45m4P Offline
        Pl45m4
        wrote on last edited by Pl45m4
        #3

        @thomasGl said in Program is crashing:

        QLabel *const helloWorldLabel;
        QPushButton *const beendeButton;
        

        What's the reason for const? Remove the const

        @thomasGl said in Program is crashing:

        delete helloWorldLabel;
        delete beendeButton;
        

        also this can be removed... (child QObjects are deleted with their parent)

        and it should work.

        Edit:

        @JonB

        Initialized here:

        MainWindow::MainWindow(QWidget *parent, QLabel *const helloWorldLabel, QPushButton *const beendeButton)
        : QMainWindow(parent)
        , helloWorldLabel(new QLabel("Hello World", this))
          beendeButton(new QPushButton("Beenden", this))
        

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

        ~E. W. Dijkstra

        JonBJ 1 Reply Last reply
        0
        • Pl45m4P Pl45m4

          @thomasGl said in Program is crashing:

          QLabel *const helloWorldLabel;
          QPushButton *const beendeButton;
          

          What's the reason for const? Remove the const

          @thomasGl said in Program is crashing:

          delete helloWorldLabel;
          delete beendeButton;
          

          also this can be removed... (child QObjects are deleted with their parent)

          and it should work.

          Edit:

          @JonB

          Initialized here:

          MainWindow::MainWindow(QWidget *parent, QLabel *const helloWorldLabel, QPushButton *const beendeButton)
          : QMainWindow(parent)
          , helloWorldLabel(new QLabel("Hello World", this))
            beendeButton(new QPushButton("Beenden", this))
          
          JonBJ Offline
          JonBJ Offline
          JonB
          wrote on last edited by JonB
          #4

          @Pl45m4 said in Program is crashing:

          Initialized here:

          OMG, I hate that syntax and it's scrolled off the screen anyway... :( :) At minimum I wish people put newlines in like you show.

          Pl45m4P 1 Reply Last reply
          1
          • JonBJ JonB

            @Pl45m4 said in Program is crashing:

            Initialized here:

            OMG, I hate that syntax and it's scrolled off the screen anyway... :( :) At minimum I wish people put newlines in like you show.

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

            @JonB said in Program is crashing:

            OMG, I hate that syntax and it's scrolled off the screen anyway... :( :)

            Yup, reformatted it to make it somewhat readable.

            What was the rule of thumb for maximum line length again? Until you get beaten as a programmer? :DD
            Like 60-70 characters or so? ;-)


            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
            • T Offline
              T Offline
              thomasGl
              wrote on last edited by
              #6

              Starting P:\Qt training\build-AktuellAktiv-Desktop_Qt_5_9_0_MinGW_32bit-Debug\debug\AktuellAktiv.exe...
              Warning: QT_DEVICE_PIXEL_RATIO is deprecated. Instead use:
              QT_AUTO_SCREEN_SCALE_FACTOR to enable platform plugin controlled per-screen factors.
              QT_SCREEN_SCALE_FACTORS to set per-screen factors.
              QT_SCALE_FACTOR to set the application global scale factor.
              The program has unexpectedly finished.
              P:\Qt training\build-AktuellAktiv-Desktop_Qt_5_9_0_MinGW_32bit-Debug\debug\AktuellAktiv.exe crashed. I am still getting that error

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

                Hi,

                Please post the stack trace of the crash.

                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
                0
                • T Offline
                  T Offline
                  thomasGl
                  wrote on last edited by
                  #8

                  2.png

                  1 Reply Last reply
                  0
                  • Axel SpoerlA Offline
                    Axel SpoerlA Offline
                    Axel Spoerl
                    Moderators
                    wrote on last edited by
                    #9
                     beendeButton -> setGeometry(30,30,30,30);
                    

                    will call setGeometry() on the constructor argument beendeButton, which defaults to nullptr, because no argument is passed to the c'tor in main.cpp.

                    I don't see, why this argument is necessary at all. Probably you get a compiler warning about an unused argument. Probably you expect setGeometryto be called on the member which you correctly initialize.

                    => remove the unneeded arguments from the c'tor. They are the reason for the crash.

                    Software Engineer
                    The Qt Company, Oslo

                    Pl45m4P 1 Reply Last reply
                    3
                    • T Offline
                      T Offline
                      thomasGl
                      wrote on last edited by
                      #10

                      @Axel-Spoerl thx it works now.

                      1 Reply Last reply
                      0
                      • Axel SpoerlA Offline
                        Axel SpoerlA Offline
                        Axel Spoerl
                        Moderators
                        wrote on last edited by
                        #11

                        Remains the (unrelated) question, why you use Qt 5.9.0.
                        That ship has sailed long ago. Better use 6.5 or even 6.7.

                        Software Engineer
                        The Qt Company, Oslo

                        1 Reply Last reply
                        1
                        • Axel SpoerlA Axel Spoerl
                           beendeButton -> setGeometry(30,30,30,30);
                          

                          will call setGeometry() on the constructor argument beendeButton, which defaults to nullptr, because no argument is passed to the c'tor in main.cpp.

                          I don't see, why this argument is necessary at all. Probably you get a compiler warning about an unused argument. Probably you expect setGeometryto be called on the member which you correctly initialize.

                          => remove the unneeded arguments from the c'tor. They are the reason for the crash.

                          Pl45m4P Offline
                          Pl45m4P Offline
                          Pl45m4
                          wrote on last edited by Pl45m4
                          #12

                          @Axel-Spoerl said in Program is crashing:

                          will call setGeometry() on the constructor argument beendeButton, which defaults to nullptr, because no argument is passed to the c'tor in main.cpp.

                          Oh gosh.... haven't even seen that the argument is named exactly the same as the private member.
                          Thought there is something else going on, beside the const which is weird in this case, because the code didn't looked that wrong.

                          @JonB Two blind people trying to help :D
                          Next time I scroll horizontally for you and you check the spelling for me :P


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

                          ~E. W. Dijkstra

                          JonBJ 1 Reply Last reply
                          2
                          • Pl45m4P Pl45m4

                            @Axel-Spoerl said in Program is crashing:

                            will call setGeometry() on the constructor argument beendeButton, which defaults to nullptr, because no argument is passed to the c'tor in main.cpp.

                            Oh gosh.... haven't even seen that the argument is named exactly the same as the private member.
                            Thought there is something else going on, beside the const which is weird in this case, because the code didn't looked that wrong.

                            @JonB Two blind people trying to help :D
                            Next time I scroll horizontally for you and you check the spelling for me :P

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

                            @Pl45m4
                            I saw the formal parameters were the same names as the member variables, which made me wonder how that works. But I said I never use that syntax and thought I could trust you on this one ;-)

                            Pl45m4P 1 Reply Last reply
                            0
                            • JonBJ JonB

                              @Pl45m4
                              I saw the formal parameters were the same names as the member variables, which made me wonder how that works. But I said I never use that syntax and thought I could trust you on this one ;-)

                              Pl45m4P Offline
                              Pl45m4P Offline
                              Pl45m4
                              wrote on last edited by
                              #14

                              @JonB said in Program is crashing:

                              I saw the formal parameters were the same names as the member variables, which made me wonder how that works. But I said I never use that syntax and thought I could trust you on this one ;-)

                              It would have, if only the names were different :'-)
                              In a regular function you would use this->beendeButton(beendeButton), then the same names are not a problem anymore, but it does not work in initializer lists.


                              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
                              2
                              • Axel SpoerlA Offline
                                Axel SpoerlA Offline
                                Axel Spoerl
                                Moderators
                                wrote on last edited by
                                #15
                                m_beendeButton
                                

                                …. and you’re set!

                                Software Engineer
                                The Qt Company, Oslo

                                1 Reply Last reply
                                2

                                • Login

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