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. [Beginner] QPushButton causing segmentation fault
QtWS25 Last Chance

[Beginner] QPushButton causing segmentation fault

Scheduled Pinned Locked Moved Unsolved General and Desktop
8 Posts 3 Posters 2.4k 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.
  • N Offline
    N Offline
    N1ck
    wrote on last edited by
    #1

    Hello

    I am fairly new to QT. I am trying to develop a simple desktop program to make some things at work more simple. At this point I am trying to get inputs from QPlainTextEdits. The QPlainTextEdits are displaying correctly. However, when the button is pressed the program gets a segmentation fault. My code is attached below. I think it is the way the QPlainTextEdits are constructed (i.e. the button cannot find where inputs[i] points to) but I am not quite sure. I have tried changing the constructor parameters. Also I have tried changing the data structure from a pointer of arrays to a vector. Neither of these fixed the issue. If anyone has any constructive comments, please advise.

    ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    ////////////////////////////////////////////// mainwindow.h /////////////////////////////////////////////////
    ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    #ifndef MAINWINDOW_H
    #define MAINWINDOW_H

    #include <QMainWindow>
    #include <QDebug>
    #include <QTextEdit>
    #include <QPlainTextEdit>
    #include <QVector>
    #include <QLabel>
    #include <QPushButton>
    #include <QDebug>

    class MainWindow : public QMainWindow
    {
    Q_OBJECT

    public:
    explicit MainWindow(QWidget *parent = 0);
    ~MainWindow();

    private slots:
    void setup_text_edit(QPlainTextEdit *a, int x, int y);
    void setup_label(QLabel *a, const QString labeltext, int x, int y);
    void add_ride_clicked();

    private:
    QPlainTextEdit *inputs[5];
    QLabel *input_labels[5];
    QLabel *stats[8];
    QLabel *stat_labels[8];
    QPushButton *add_ride;
    };
    ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    ////////////////////////////////////////////// main.cpp /////////////////////////////////////////////////
    ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    #include "mainwindow.h"
    #include <QApplication>

    int main(int argc, char *argv[])
    {
    QApplication a(argc, argv);
    MainWindow w;
    w.showMaximized();
    return a.exec();
    }

    ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    ////////////////////////////////////////////// mainwindow.cpp///////////////////////////////////////////
    ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    #include "mainwindow.h"
    #include "ui_mainwindow.h"
    MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent){

    //Set up text edits and labels for the ride inputs
    QString temp[5] = {"1","2","3","4","5"};
    QString temp2[8] = {"<Label>", "<Label>","<Label>","<Label>","<Label>","<Label>", "<Label>", "<Label>"};
    QString temp3[8] = {"L1", "L2", "L3","L4","L5","L6", "L7", "L8"};
    
    for(int i = 0; i < 5; i++)   MainWindow::setup_text_edit(inputs[i], 50,50+40*i);
    for(int i = 0; i < 5; i++)   MainWindow::setup_label(input_labels[i], temp[i], 170, 50+40*i);
    for(int i = 0; i < 8; i++)   MainWindow::setup_label(stats[i], temp2[i], 50, 325+40*i);
    for(int i = 0; i < 8; i++)   MainWindow::setup_label(stat_labels[i], temp3[i], 170, 325+40*i);
    
    add_ride = new QPushButton("Add Ride", this);
    add_ride->setGeometry((QRect(QPoint(100,270),QSize(100,25))));
    connect(add_ride, SIGNAL(clicked(bool)), this, SLOT(add_ride_clicked()));
    

    }

    MainWindow::~MainWindow(){
    }

    void MainWindow::add_ride_clicked(){
    //Get data from text edit boxes
    QString input_data[5];
    for(int i = 0; i < 5; i++) input_data[i] = inputs[i]->toPlainText(); //SEG FAULT HERE

    //clear text boxes
    //Convert labels to object/database
    //update stats
    //update stat labels
    

    }

    void MainWindow::setup_text_edit(QPlainTextEdit *a, int x, int y){
    a = new QPlainTextEdit("", this);
    a->setGeometry(QRect(QPoint(x,y),QSize(100,25)));

    }

    K 1 Reply Last reply
    0
    • N N1ck

      Hello

      I am fairly new to QT. I am trying to develop a simple desktop program to make some things at work more simple. At this point I am trying to get inputs from QPlainTextEdits. The QPlainTextEdits are displaying correctly. However, when the button is pressed the program gets a segmentation fault. My code is attached below. I think it is the way the QPlainTextEdits are constructed (i.e. the button cannot find where inputs[i] points to) but I am not quite sure. I have tried changing the constructor parameters. Also I have tried changing the data structure from a pointer of arrays to a vector. Neither of these fixed the issue. If anyone has any constructive comments, please advise.

      ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
      ////////////////////////////////////////////// mainwindow.h /////////////////////////////////////////////////
      ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
      #ifndef MAINWINDOW_H
      #define MAINWINDOW_H

      #include <QMainWindow>
      #include <QDebug>
      #include <QTextEdit>
      #include <QPlainTextEdit>
      #include <QVector>
      #include <QLabel>
      #include <QPushButton>
      #include <QDebug>

      class MainWindow : public QMainWindow
      {
      Q_OBJECT

      public:
      explicit MainWindow(QWidget *parent = 0);
      ~MainWindow();

      private slots:
      void setup_text_edit(QPlainTextEdit *a, int x, int y);
      void setup_label(QLabel *a, const QString labeltext, int x, int y);
      void add_ride_clicked();

      private:
      QPlainTextEdit *inputs[5];
      QLabel *input_labels[5];
      QLabel *stats[8];
      QLabel *stat_labels[8];
      QPushButton *add_ride;
      };
      ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
      ////////////////////////////////////////////// main.cpp /////////////////////////////////////////////////
      ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
      #include "mainwindow.h"
      #include <QApplication>

      int main(int argc, char *argv[])
      {
      QApplication a(argc, argv);
      MainWindow w;
      w.showMaximized();
      return a.exec();
      }

      ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
      ////////////////////////////////////////////// mainwindow.cpp///////////////////////////////////////////
      ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
      #include "mainwindow.h"
      #include "ui_mainwindow.h"
      MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent){

      //Set up text edits and labels for the ride inputs
      QString temp[5] = {"1","2","3","4","5"};
      QString temp2[8] = {"<Label>", "<Label>","<Label>","<Label>","<Label>","<Label>", "<Label>", "<Label>"};
      QString temp3[8] = {"L1", "L2", "L3","L4","L5","L6", "L7", "L8"};
      
      for(int i = 0; i < 5; i++)   MainWindow::setup_text_edit(inputs[i], 50,50+40*i);
      for(int i = 0; i < 5; i++)   MainWindow::setup_label(input_labels[i], temp[i], 170, 50+40*i);
      for(int i = 0; i < 8; i++)   MainWindow::setup_label(stats[i], temp2[i], 50, 325+40*i);
      for(int i = 0; i < 8; i++)   MainWindow::setup_label(stat_labels[i], temp3[i], 170, 325+40*i);
      
      add_ride = new QPushButton("Add Ride", this);
      add_ride->setGeometry((QRect(QPoint(100,270),QSize(100,25))));
      connect(add_ride, SIGNAL(clicked(bool)), this, SLOT(add_ride_clicked()));
      

      }

      MainWindow::~MainWindow(){
      }

      void MainWindow::add_ride_clicked(){
      //Get data from text edit boxes
      QString input_data[5];
      for(int i = 0; i < 5; i++) input_data[i] = inputs[i]->toPlainText(); //SEG FAULT HERE

      //clear text boxes
      //Convert labels to object/database
      //update stats
      //update stat labels
      

      }

      void MainWindow::setup_text_edit(QPlainTextEdit *a, int x, int y){
      a = new QPlainTextEdit("", this);
      a->setGeometry(QRect(QPoint(x,y),QSize(100,25)));

      }

      K Offline
      K Offline
      koahnig
      wrote on last edited by
      #2

      @N1ck

      Hi and welcome to devnet forum

      I think your problem could be tracked right here:

      void MainWindow::setup_text_edit(QPlainTextEdit *a, int x, int y){
      a = new QPlainTextEdit("", this);
      a->setGeometry(QRect(QPoint(x,y),QSize(100,25)));
      QString temp = a->toPlainText();  //  <== 
      }
      

      I could not find any obvious flaws. However, I have not checked the code through trying.

      Vote the answer(s) that helped you to solve your issue(s)

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

        Hi and welcome to devnet,

        From the code you posted, you don't initialise the content of your inputs variable hence you are trying to access non existing objects. Same goes for your other widget arrays.

        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
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #4

          I just realised I haven't been clear: you are initialising the content of a which is a copy of the pointer to your table resource.

          Since you want to initialise the content of the table, you need to pass a reference. So basically change setup_text_editto something like setup_text_edit(QPlainTextEdit *&a, int x, int y) which is IMHO not really nice to read. A cleaner alternative would be to have something like:

          void MainWindow::setup_text_edit(int x, int y){
              QPlainTextEdit *textEdit = new QPlainTextEdit("", this);
              textEdit->setGeometry(QRect(QPoint(x,y),QSize(100,25)));
              return textEdit;
          }
          

          And in your loop:

          for(int i = 0; i < 5; i++) {
              inputs[i] = MainWindow::setup_text_edit(50,50+40*i);
          }
          

          That makes things more readable.

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          K N 2 Replies Last reply
          2
          • SGaistS SGaist

            I just realised I haven't been clear: you are initialising the content of a which is a copy of the pointer to your table resource.

            Since you want to initialise the content of the table, you need to pass a reference. So basically change setup_text_editto something like setup_text_edit(QPlainTextEdit *&a, int x, int y) which is IMHO not really nice to read. A cleaner alternative would be to have something like:

            void MainWindow::setup_text_edit(int x, int y){
                QPlainTextEdit *textEdit = new QPlainTextEdit("", this);
                textEdit->setGeometry(QRect(QPoint(x,y),QSize(100,25)));
                return textEdit;
            }
            

            And in your loop:

            for(int i = 0; i < 5; i++) {
                inputs[i] = MainWindow::setup_text_edit(50,50+40*i);
            }
            

            That makes things more readable.

            K Offline
            K Offline
            koahnig
            wrote on last edited by
            #5

            To add to @SGaist and correct flaw

            QPlainTextEdit * MainWindow::setup_text_edit(int x, int y){
                QPlainTextEdit *textEdit = new QPlainTextEdit("", this);
                textEdit->setGeometry(QRect(QPoint(x,y),QSize(100,25)));
                return textEdit;
            }
            

            Vote the answer(s) that helped you to solve your issue(s)

            N 1 Reply Last reply
            0
            • SGaistS SGaist

              I just realised I haven't been clear: you are initialising the content of a which is a copy of the pointer to your table resource.

              Since you want to initialise the content of the table, you need to pass a reference. So basically change setup_text_editto something like setup_text_edit(QPlainTextEdit *&a, int x, int y) which is IMHO not really nice to read. A cleaner alternative would be to have something like:

              void MainWindow::setup_text_edit(int x, int y){
                  QPlainTextEdit *textEdit = new QPlainTextEdit("", this);
                  textEdit->setGeometry(QRect(QPoint(x,y),QSize(100,25)));
                  return textEdit;
              }
              

              And in your loop:

              for(int i = 0; i < 5; i++) {
                  inputs[i] = MainWindow::setup_text_edit(50,50+40*i);
              }
              

              That makes things more readable.

              N Offline
              N Offline
              N1ck
              wrote on last edited by
              #6

              @SGaist Thank you so much! That was very clear and concise - you rock!

              1 Reply Last reply
              0
              • K koahnig

                To add to @SGaist and correct flaw

                QPlainTextEdit * MainWindow::setup_text_edit(int x, int y){
                    QPlainTextEdit *textEdit = new QPlainTextEdit("", this);
                    textEdit->setGeometry(QRect(QPoint(x,y),QSize(100,25)));
                    return textEdit;
                }
                
                N Offline
                N Offline
                N1ck
                wrote on last edited by
                #7

                @koahnig I kinda figured, but thank you for the extra clarification - you guys are the best

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

                  You're welcome !

                  Since you have it working now, please mark the thread as solved using the "Topic Tools" button so that other forum users may know a solution has been found :)

                  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

                  • Login

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