Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt
  • Unsolved
Collapse
qtws26_logo

The Qt Event of the Year: Qt World Summit 2026

Save Your Spot >
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Program with a GUI crashes when add .h and .cpp files
Qt 6.12 LTS is out! See what's new in the release blog

Program with a GUI crashes when add .h and .cpp files

Scheduled Pinned Locked Moved Unsolved General and Desktop
6 Posts 4 Posters 1.3k Views 1 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.
  • P Offline
    P Offline
    Pordo
    wrote on last edited by
    #1

    Hello! I'm working on a program that uses multiple classes and a GUI. The thing is that I want to make an extra file where all the global variables, whose values are constant and are used in all the program. This file I did it by creating a header where I define the type of each variable and in a .cpp, I give it the value to each variable. The problem is that before creating the .h and .cpp files the program ran without any problems, but when I add these files the program crashes.

    #ifndef CONSTANTESRADARGUI_H
    #define CONSTANTESRADARGUI_H
    
    //VARIABLES GLOBALES CON UN VALOR CONSTANTE
    //se ponen todas las variables globales que se utilizan en todo el programa y su valor es constante
    //las variables se inicializan en constantesradargui.cpp
    
    //----------------------------
    //parametros del radar
    extern float freqM;         //frecuencia de muestreo, en m/s
    extern int nBI;             //frecuencia de bearing increment BI, en Hz   no se usa
    extern int nTRG;            //frecuencia de trigger TRG, en Hz    2037
    extern int rpmHM;           //frecuencia del hearing mark HM, en rpm
    
    //----------------------------
    //parametros de la matriz M
    extern int  nfilas;         //cantidad de filas de la matriz M
    extern int numeroColumna;   //para modificar tamaño de la matriz (Lucas Pordomingo)
                                //numeroColumna=1 para cantidad de columnas calculadas con parametros del radar
                                //numeroColumna=2 para cantidad de columnas de 4096
                                //numeroColumna=3 para cantidad de columnas de 2096
    
    
    //----------------------------
    //parametros calculados a partir de los parametros del radar
    extern int VelGiro;             //velocidad con la que gira el radar
    extern int ncolumnas;           //numero de columnas de la matriz M, con los datos del radar
    extern int tamano_columna;      //sirve para modificar distancia de graficado desde el centro del radar
    //tamano_columna = 1/(ncolumnas/8183) y ncolumnas= int(round((1/nTRG)*(freqM*1000000))), para el caso de usar una matriz M cantidad de  columnas de acuerdo a los parametros del radar
    //tamano_columna = 2 y ncolumnas = 4096, para el caso de usar una matriz M con 4096 columnas
    //tamano_columna = 4 y ncolumnas = 2096, para el caso de usar una matriz M con 2096 columnas
    
    //----------------------------
    //constantes que se utilizan en varios lugares del programa
    extern int niveles;        //colores
    extern float area;         //area del pixel que se dibuja
    extern float kmmilla;
    extern float rmillamuestra;
    extern float rfilaangulo;
    
    #endif // CONSTANTESRADARGUI_H
    
    #include<constantesradargui.h>
    
    #include<math.h>
    
    //----------------------------
    //parametros del radar
    float freqM= 16.666;         //frecuencia de muestreo, en m/s
    int nBI= 2049;               //frecuencia de bearing increment BI, en Hz   no se usa
    int nTRG= 2037;              //frecuencia de trigger TRG, en Hz    2037
    int rpmHM= 25;               //frecuencia del hearing mark HM, en rpm
    
    //----------------------------
    //parametros de la matriz M
    int nfilas= 4096;         //cantidad de filas de la matriz M
    int numeroColumna= 1;    //para modificar tamaño de la matriz (Lucas Pordomingo)
                             //numeroColumna=1 para cantidad de columnas calculadas con parametros del radar
                             //numeroColumna=2 para cantidad de columnas de 4096
                             //numeroColumna=3 para cantidad de columnas de 2096
    
    //----------------------------
    //parametros calculados a partir de los parametros del radar
    int VelGiro= (1/(rpmHM*0.02));;                        //velocidad con la que gira el radar
    int ncolumnas= int(round((1/nTRG)*(freqM*1000000)));      //numero de columnas de la matriz M, con los datos del radar
    int tamano_columna =1/(ncolumnas/8183);                   //sirve para modificar distancia de graficado desde el centro del radar
    //tamano_columna = 1/(ncolumnas/8183) y ncolumnas= int(round((1/nTRG)*(freqM*1000000))), para el caso de usar una matriz M cantidad de  columnas de acuerdo a los parametros del radar
    //tamano_columna = 2 y ncolumnas = 4096, para el caso de usar una matriz M con 4096 columnas
    //tamano_columna = 4 y ncolumnas = 2096, para el caso de usar una matriz M con 2096 columnas
    
    //----------------------------
    //constantes que se utilizan en varios lugares del programa
    int niveles = 5;        //colores
    float area = 0.1;       //area del pixel que se dibuja
    float kmmilla=1.852;
    float rmillamuestra = (1 / (freqM * 1000000)) * (300000 / kmmilla);
    float rfilaangulo= nfilas/360;
    

    The errror is:
    09:55:09: Starting /home/pc2/Descargas/build-Radar-Desktop-Debug/Radar...
    09:55:10: /home/pc2/Descargas/build-Radar-Desktop-Debug/Radar crashed.

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

      Use a debugger and see where exactly it crashes.

      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
      2
      • Paul ColbyP Offline
        Paul ColbyP Offline
        Paul Colby
        wrote on last edited by
        #3

        Hi @Pordo,

        There's probably other things, but this line crashes with a floating-point exception on my machine:

        int tamano_columna =1/(ncolumnas/8183);
        

        The reason is that ncolumnas (from the preceding line) evaluates to 8182. And since all the values (both variables and literals) in that statement are integers, it evaluates like this:

        int tamano_columna =1/(ncolumnas/8183);
                           =1/(8182/8183);
                           =1/trunc(0.99987779543);
                           =1/0;
                           =exception
        

        You can fix this particular exception by making either ncolumnas or the 8183 literal a float or double; eg this does not crash for me:

        int tamano_columna =1/(ncolumnas/8183.0);
        

        But as @Christian-Ehrlicher said, the debugger is your friend :)

        Cheers.

        JonBJ 1 Reply Last reply
        4
        • Paul ColbyP Paul Colby

          Hi @Pordo,

          There's probably other things, but this line crashes with a floating-point exception on my machine:

          int tamano_columna =1/(ncolumnas/8183);
          

          The reason is that ncolumnas (from the preceding line) evaluates to 8182. And since all the values (both variables and literals) in that statement are integers, it evaluates like this:

          int tamano_columna =1/(ncolumnas/8183);
                             =1/(8182/8183);
                             =1/trunc(0.99987779543);
                             =1/0;
                             =exception
          

          You can fix this particular exception by making either ncolumnas or the 8183 literal a float or double; eg this does not crash for me:

          int tamano_columna =1/(ncolumnas/8183.0);
          

          But as @Christian-Ehrlicher said, the debugger is your friend :)

          Cheers.

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

          @Paul-Colby said in Program with a GUI crashes when add .h and .cpp files:

          int tamano_columna =1/(ncolumnas/8183.0);

          Good job for working through @Pordo's code to come to this! Indeed this will prevent the crash --- provided ncolumnas != 8183.0! But @Pordo may have a deeper logic problem in this statement.

          Since the result is assigned to an int, there is a "limited" set of possibilities for what the 1 / something on the right-hand side will evaluate to anyway:

          • if something == 0, it will crash/be undefined.
          • if something >= 1, for any value of something then 1 / something will evaluate to 0 as a (truncated) integer.
          • if and only if 0 < something < 1 will it produce any int great than 0. Which means that for something, in the (ncolumnas/8183.0) expression ncolumnas needs to be a lot less than 8183.0 if this is the intention. Is it? Else I wonder what OP is expecting the value of tamano_columna to be?
          1 Reply Last reply
          3
          • P Offline
            P Offline
            Pordo
            wrote on last edited by
            #5

            Hello, I was reading their answers and tried to comment a section of the code that uses the variable tamano_columa. The code compiles, so I confirm that the problem was there.
            The variable is used to maintain a distance ratio on a graphic. I resolved it changing the variable type to float.
            Thanks to everyone for their answers, they were helpful.

            JonBJ 1 Reply Last reply
            0
            • P Pordo

              Hello, I was reading their answers and tried to comment a section of the code that uses the variable tamano_columa. The code compiles, so I confirm that the problem was there.
              The variable is used to maintain a distance ratio on a graphic. I resolved it changing the variable type to float.
              Thanks to everyone for their answers, they were helpful.

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

              @Pordo said in Program with a GUI crashes when add .h and .cpp files:

              I resolved it changing the variable type to float.

              That sounds a lot more useful :)
              Just a comment: consider using double instead of float for this, other constants and arithmetic. In C all floating point calculations are done in double-precision anyway (at least I think/thought this was the case). Using float instead of double throws away precision for no good reason. The only time I would use floats is to save space if I had millions of them....

              1 Reply Last reply
              1

              • Login

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