[Beginner] QPushButton causing segmentation fault
-
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_OBJECTpublic:
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)));}
-
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_OBJECTpublic:
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)));}
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.
-
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. -
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_edit
to something likesetup_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.
-
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_edit
to something likesetup_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.
-
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_edit
to something likesetup_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.
-
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; }
-
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 :)