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. Segfault when initializing pointer array of QPushButton
Forum Updated to NodeBB v4.3 + New Features

Segfault when initializing pointer array of QPushButton

Scheduled Pinned Locked Moved Solved General and Desktop
9 Posts 4 Posters 1.9k Views
  • 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.
  • R Offline
    R Offline
    ryannasaurus
    wrote on last edited by
    #1

    When I try to initialize an array of QPushButton, I get a segfault error.

    Here, I declare the QPushButton variables in a header file.

    QPushButton *num[12];
    

    In a source file, I initialize the array and add it to a layout, which is added to a larger layout:

    for (int i = 0; i < 12; i++)
        {
            //initializes and sets labels to numbers 1-12
            num[i] = new QPushButton(QString::number(i+1));
    
            //*QHBoxLayout
            selectLayout->addWidget(num[i]);
        }
    
        //*QGridLayout
        layout->addLayout(selectLayout, 2, 0, 2, 12, Qt::AlignCenter);
    
        setLayout(layout);
    

    I have also tried initializing and then setting the text:

    for (int i = 0; i < 12; i++)
        {
            //initializes and sets labels to numbers 1-12
            num[i] = new QPushButton();
            num[i]->setText(QString::number(i+1));
    
            //*QHBoxLayout
            selectLayout->addWidget(num[i]);
        }
    

    When executing code, I get Segmentation Fault (Signal SIGSEGV) no matter how I write the code. I've also tried the 2nd variation with this pointers and initializing outside of the loop, but none of these have worked.

    I'm fairly new to Qt and do not how to fix the error. Can anybody help?

    aha_1980A J.HilkJ 2 Replies Last reply
    0
    • R ryannasaurus

      When I try to initialize an array of QPushButton, I get a segfault error.

      Here, I declare the QPushButton variables in a header file.

      QPushButton *num[12];
      

      In a source file, I initialize the array and add it to a layout, which is added to a larger layout:

      for (int i = 0; i < 12; i++)
          {
              //initializes and sets labels to numbers 1-12
              num[i] = new QPushButton(QString::number(i+1));
      
              //*QHBoxLayout
              selectLayout->addWidget(num[i]);
          }
      
          //*QGridLayout
          layout->addLayout(selectLayout, 2, 0, 2, 12, Qt::AlignCenter);
      
          setLayout(layout);
      

      I have also tried initializing and then setting the text:

      for (int i = 0; i < 12; i++)
          {
              //initializes and sets labels to numbers 1-12
              num[i] = new QPushButton();
              num[i]->setText(QString::number(i+1));
      
              //*QHBoxLayout
              selectLayout->addWidget(num[i]);
          }
      

      When executing code, I get Segmentation Fault (Signal SIGSEGV) no matter how I write the code. I've also tried the 2nd variation with this pointers and initializing outside of the loop, but none of these have worked.

      I'm fairly new to Qt and do not how to fix the error. Can anybody help?

      aha_1980A Offline
      aha_1980A Offline
      aha_1980
      Lifetime Qt Champion
      wrote on last edited by
      #2

      @ryannasaurus

      Are you sure the crash is within the loop? Where are selectLayout and num declared?

      Why do you need num at all, i.e. are you accessing your buttons from some other place?

      Qt has to stay free or it will die.

      R 1 Reply Last reply
      2
      • aha_1980A aha_1980

        @ryannasaurus

        Are you sure the crash is within the loop? Where are selectLayout and num declared?

        Why do you need num at all, i.e. are you accessing your buttons from some other place?

        R Offline
        R Offline
        ryannasaurus
        wrote on last edited by
        #3

        @aha_1980
        selectLayout and num are declared in this class:

        #ifndef UI_H
        #define UI_H
        
        #include <QWidget>
        
        class QLabel;
        class QPushButton;
        class QGridLayout;
        class QHBoxLayout;
        class QVBoxLayout;
        
        class ui : public QWidget
        {
            Q_OBJECT
        public:
            explicit ui(QWidget *parent = nullptr);
        
        private:
            //layouts
            QGridLayout *layout;
            QHBoxLayout *selectLayout;
            QGridLayout *titleWidget;
        
            //fonts
            QFont *fTitle;
            QFont *fTitleMini;
            QFont *fInfo;
        
            //labels
            QLabel *title;
            QLabel *titleMini;
            QLabel *info;
        
            //buttons
            QPushButton *num[12];
            QPushButton *rollButton;
        
        signals:
        
        public slots:
        };
        
        #endif // UI_H
        
        

        The buttons are not being accessed anywhere. They are just being initialized and displayed.

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

          @ryannasaurus said in Segfault when initializing pointer array of QPushButton:

          QPushButton *num[12];

          This is a pointer to an array (whyever) and the pointer is not initialized...

          Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
          Visit the Qt Academy at https://academy.qt.io/catalog

          R 1 Reply Last reply
          0
          • Christian EhrlicherC Christian Ehrlicher

            @ryannasaurus said in Segfault when initializing pointer array of QPushButton:

            QPushButton *num[12];

            This is a pointer to an array (whyever) and the pointer is not initialized...

            R Offline
            R Offline
            ryannasaurus
            wrote on last edited by
            #5

            @Christian-Ehrlicher
            I have it set to initialize in this for-loop here:

            for (int i = 0; i < 12; i++)
                {
                    //initializes and sets labels to numbers 1-12
                    num[i] = new QPushButton(QString::number(i+1));
                }
            

            I tried to add a constructor to initialize in the header file itself but it kept returning errors. It is ok to initialize it in the respective source file like I have done here?

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

              Sorry I misread the C declaration. I would use QVector <QPushButton*> num; instead. And there is no need to hold the font as pointer - QFont fTitle; etc is perefectl valid.
              The crash must be somewhere else - I would suggest using a Debugger to take a look where it really crashes.

              Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
              Visit the Qt Academy at https://academy.qt.io/catalog

              R 1 Reply Last reply
              3
              • R ryannasaurus

                When I try to initialize an array of QPushButton, I get a segfault error.

                Here, I declare the QPushButton variables in a header file.

                QPushButton *num[12];
                

                In a source file, I initialize the array and add it to a layout, which is added to a larger layout:

                for (int i = 0; i < 12; i++)
                    {
                        //initializes and sets labels to numbers 1-12
                        num[i] = new QPushButton(QString::number(i+1));
                
                        //*QHBoxLayout
                        selectLayout->addWidget(num[i]);
                    }
                
                    //*QGridLayout
                    layout->addLayout(selectLayout, 2, 0, 2, 12, Qt::AlignCenter);
                
                    setLayout(layout);
                

                I have also tried initializing and then setting the text:

                for (int i = 0; i < 12; i++)
                    {
                        //initializes and sets labels to numbers 1-12
                        num[i] = new QPushButton();
                        num[i]->setText(QString::number(i+1));
                
                        //*QHBoxLayout
                        selectLayout->addWidget(num[i]);
                    }
                

                When executing code, I get Segmentation Fault (Signal SIGSEGV) no matter how I write the code. I've also tried the 2nd variation with this pointers and initializing outside of the loop, but none of these have worked.

                I'm fairly new to Qt and do not how to fix the error. Can anybody help?

                J.HilkJ Offline
                J.HilkJ Offline
                J.Hilk
                Moderators
                wrote on last edited by
                #7

                hi @ryannasaurus said in Segfault when initializing pointer array of QPushButton:

                is selectLayout defined? From what you pasted, I only see the pointer declaration and addWidget calls on it. Did you new
                anywhere ?


                Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


                Q: What's that?
                A: It's blue light.
                Q: What does it do?
                A: It turns blue.

                R 1 Reply Last reply
                4
                • Christian EhrlicherC Christian Ehrlicher

                  Sorry I misread the C declaration. I would use QVector <QPushButton*> num; instead. And there is no need to hold the font as pointer - QFont fTitle; etc is perefectl valid.
                  The crash must be somewhere else - I would suggest using a Debugger to take a look where it really crashes.

                  R Offline
                  R Offline
                  ryannasaurus
                  wrote on last edited by ryannasaurus
                  #8
                  This post is deleted!
                  1 Reply Last reply
                  0
                  • J.HilkJ J.Hilk

                    hi @ryannasaurus said in Segfault when initializing pointer array of QPushButton:

                    is selectLayout defined? From what you pasted, I only see the pointer declaration and addWidget calls on it. Did you new
                    anywhere ?

                    R Offline
                    R Offline
                    ryannasaurus
                    wrote on last edited by
                    #9

                    @J.Hilk
                    Yep, this did it. I forgot to initialize one of the widgets. Thanks.

                    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