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. getting runtime error (SIGSEGV) in private slot
Forum Updated to NodeBB v4.3 + New Features

getting runtime error (SIGSEGV) in private slot

Scheduled Pinned Locked Moved Unsolved General and Desktop
2 Posts 2 Posters 118 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.
  • I Offline
    I Offline
    Isidore
    wrote on last edited by
    #1

    I want this slot to add student into database, when user pushes button inner_add_button. But accidently i get runtime error (SIGSEGV) when tableWidget->item(0,1)->text() is performed.
    I tried to perform tableWdiget->item(0,1); in save_chng_button_pressed(); slot and it works fine, but when i am trying to parse this into text, runtime error apears. Needed slot implementation is on the bottom of edit_window.cpp class

    Here's the code

    edit_window.h

    #ifndef EDIT_WINDOW_H
    #define EDIT_WINDOW_H
    
    #include <QWidget>
    #include <mylineedit.h>
    #include <lineedit_delegate.h>
    #include <spinbox_delegate.h>
    #include <enums.h>
    #include "mytablewidget.h"
    #include <QTableWidget>
    #include <QPushButton>
    
    #include<QSqlQuery>
    #include<QtDebug>
    #include<QSqlError>
    #include<QSqlRecord>
    
    namespace Ui {
    class Edit_window;
    }
    
    class Edit_window : public QWidget
    {
        Q_OBJECT
    
    public:
        explicit Edit_window(QWidget *parent = nullptr);
     void chng_widget(Button_pressed j);
        ~Edit_window();
    
    
    
    private:
        Ui::Edit_window *add;
        void closeEvent(QCloseEvent *event);
        MyLineEdit *Name_MLine_Edit = new MyLineEdit;
        QCompleter *completer = nullptr;
        QPushButton *inner_add_button = new QPushButton();
        QStringList list;
    
        QSqlQuery qry;
        QString query;
    
        lineedit_delegate line_edit_delegate;
        spinbox_delegate spin_box_delegate;
    
        qint8 whButton;
        Button_pressed j;
        mytablewidget *tableWidget = new mytablewidget();
    
    private slots:
        void add_stud_ok_window();
        void add_stud_canc_window();
        void table_cell_changed();
        void save_chng_button_pressed();
    
    };
    
    extern bool datachanged;
    
    #endif // EDIT_WINDOW_H
    

    edit_window.cpp

    #include "edit_window.h"
    #include "ui_edit_window.h"
    #include <QCloseEvent>
    #include <QMessageBox>
    #include <QValidator>
    #include <QCompleter>
    #include <QTableView>
    
    Edit_window::Edit_window(QWidget *parent) :
        QWidget(parent),
        add(new Ui::Edit_window)
    {
        add->setupUi(this);
        this->tableWidget = new mytablewidget();
    
        QPixmap bkgnd("background.jpg");
            bkgnd = bkgnd.scaled(this->size(), Qt::IgnoreAspectRatio);
            QPalette palette;
            palette.setBrush(QPalette::Background, bkgnd);
            this->setPalette(palette);
    
        ///////////////////// MyLine properties and MySpinbox properties
    
    
        this->Name_MLine_Edit->setValidator( new QRegExpValidator( QRegExp( "[A-Z|a-z]{1,19}" ) ) );
    
    
        this->Name_MLine_Edit->setFixedSize(200, 20);
        this->Name_MLine_Edit->setObjectName("Name_MLine_Edit");
    
    ///////////////////////////////////////////////////////////
    
        //connections
    
        connect(add->buttonBox, SIGNAL(accepted()), this, SLOT(add_stud_ok_window()));
        connect(add->buttonBox, SIGNAL(rejected()), this, SLOT(add_stud_canc_window()));
    
    
    }
    
    
    
    void Edit_window::chng_widget(Button_pressed j)
    {
        //MYSQL QUERY
            if(qry.exec("SELECT * FROM students.students_report_card;"))
            {
                 while(qry.next())
                     list.append(qry.value(1).toString()+" "+qry.value(2).toString()+" "+qry.value(0).toString() );
            }
            else
            {
                qDebug() <<"Error of query- " <<qry.lastError().text();
                qDebug() <<"Query is "<< query;
            }
    
        add->wrng_msg_label->clear();
        completer = new QCompleter(list,this);
            completer->setCaseSensitivity(Qt::CaseInsensitive);
            this->Name_MLine_Edit->setCompleter(completer);
    
        switch (j) {
        case Add_button:
    {
            this->whButton = Add_button;
    
            this->tableWidget->clear();
    
            this->tableWidget->setColumnCount(7);
            this->tableWidget->setRowCount(1);
    
    
            this->tableWidget->setFixedSize(tableWidget->columnWidth(1) * 7 + 16, (tableWidget->rowHeight(0) * 2) -5 );
            tableWidget->setHorizontalHeaderLabels(QStringList() << "Name" << "Surname" << "Physics" << "Maths" << "Chemistry" << "CS" << "");
            tableWidget->setVerticalHeaderLabels(QStringList() << " " << " ");
    
            tableWidget->setItemDelegateForColumn(0, &line_edit_delegate);
            tableWidget->setItemDelegateForColumn(1, &line_edit_delegate);
            tableWidget->setItemDelegateForColumn(2, &spin_box_delegate);
            tableWidget->setItemDelegateForColumn(3, &spin_box_delegate);
            tableWidget->setItemDelegateForColumn(4, &spin_box_delegate);
            tableWidget->setItemDelegateForColumn(5, &spin_box_delegate);
    
            inner_add_button->setFixedSize(tableWidget->columnWidth(0),tableWidget->rowHeight(0));
            inner_add_button->setText("Add");
            tableWidget->setCellWidget(0,6, inner_add_button);
    
            connect(inner_add_button, SIGNAL(clicked()), this, SLOT(saven_chng_button_pressed()));
    
            tableWidget->show();
            break;
    }
        case Change_button:
    
            this->whButton = Change_button;
            add->add_student_label->clear();
            add->add_student_label->setText("Enter name of the student\n to change its data");
            add->gridLayout->addWidget(this->Name_MLine_Edit,1,0,2,1,Qt::AlignHCenter);
            break;
    
        case Delete_button:
    
            this->whButton = Delete_button;
            add->add_student_label->clear();
            add->add_student_label->setText("Enter name of the student\n to delete its data");
            add->gridLayout->addWidget(this->Name_MLine_Edit,1,0,2,1,Qt::AlignHCenter);
            break;
    
        default:
            break;
        }  
    }
    
    void Edit_window::add_stud_ok_window()
    {
        QString str = this->Name_MLine_Edit->text();
        auto parts = str.split(QLatin1Char(' '));
        if(qry.exec("insert into students.students_report_card(firstname,lastname,physics,maths,chemystry,computer_science) values(\"" +
                     tableWidget->item(0,1)->text() + "\", \"" + tableWidget->item(0,2)->text() +
                     "\"," + tableWidget->item(0,3)->text() +
                     "," +tableWidget->item(0,4)->text()+"," + tableWidget->item(0,5)->text() +
                     ","+tableWidget->item(0,6)->text() + ");" ))
        {
            qDebug() <<"Succesfully added a student";
        }
        else
        {
            QMessageBox msgWarning;
                msgWarning.setText("Cannot add the student. "+ qry.lastError().text());
                msgWarning.setIcon(QMessageBox::Warning);
                msgWarning.setWindowTitle("Caution");
                msgWarning.exec();
        }
        switch (this->whButton) {
        case Change_button:
        {
    
                if(qry.exec("SELECT * FROM students.students_report_card where id = " + parts[2] + ";"))
                {
    
                    tableWidget->clear();
    
                    //Set the amount of rows and columns in the table
                     tableWidget->setColumnCount(qry.record().count());
                     tableWidget->setRowCount(qry.size());
    
                    //Set the size of widgets which we are going to insert into our table
    
                      for (int i = 0; qry.next(); i++)
                      {
                         for (int j = 0; j <qry.record().count(); j++)
                         {
                              QTableWidgetItem *itm = new QTableWidgetItem(qry.value(j).toString());
    
                              if (j == 0 || j == 7)
                              {
                                  itm->setFlags(itm->flags() ^ Qt::ItemIsEditable);
                              }
    
    
                             itm->setTextAlignment(Qt::AlignHCenter);
                             tableWidget->setItem(i,j,itm);
                         }
                      }
    
                     //Set width of the reg.date column, size and header labels of the table
                     tableWidget->setColumnWidth(7,150);
                     tableWidget->setHorizontalHeaderLabels(QStringList() << "ID" << "Name" << "Surname" << "Physics" << "Maths" << "Chemistry" << "CS" << "Reg_date" );
                     tableWidget->setFixedSize(tableWidget->columnWidth(1) * qry.record().count() + 46, (tableWidget->rowHeight(0) * 2) -9 );
    
                     tableWidget->setItemDelegateForColumn(1, &line_edit_delegate);
                     tableWidget->setItemDelegateForColumn(2, &line_edit_delegate);
                     tableWidget->setItemDelegateForColumn(3, &spin_box_delegate);
                     tableWidget->setItemDelegateForColumn(4, &spin_box_delegate);
                     tableWidget->setItemDelegateForColumn(5, &spin_box_delegate);
                     tableWidget->setItemDelegateForColumn(6, &spin_box_delegate);
       
                     //connections
                     connect(tableWidget, SIGNAL(cellChanged(int, int)),this, SLOT(table_cell_changed()));
    
    
    
                     //Show the table
                     tableWidget->show();
    
                }
                else
                {
                    qDebug() <<"Error of query- " <<qry.lastError().text();
                    qDebug() <<"Query is "<< query;
                }
            break;
        }
    
        case Delete_button:
    
            if(qry.exec("delete from students.students_report_card where id = " + parts[2] + ";"))
            {
                if (!qry.size())
                    add->wrng_msg_label->setText("0 rows were affected");
                else
                    add->wrng_msg_label->setText("Succes!");
            }
            else
            {
                QMessageBox msgWarning;
                    msgWarning.setText("WARNING!\nCannot delete the student. "+ qry.lastError().text());
                    msgWarning.setIcon(QMessageBox::Warning);
                    msgWarning.setWindowTitle("Caution");
                    msgWarning.exec();
            }
    
            break;
        default:
            break;
        }
    }
    
    
    void Edit_window::closeEvent(QCloseEvent *bar)
    {
    
        if(this->whButton == 1  &&  !this->Name_MLine_Edit->text().isEmpty())
        {
        QMessageBox::StandardButton resBtn = QMessageBox::question( this," ",
                                                                        tr("Clear entered data?\n"),
                                                                        QMessageBox::Cancel | QMessageBox::No | QMessageBox::Yes,
                                                                        QMessageBox::Yes);
    
        switch (resBtn) {
        case QMessageBox::Yes:
            this->Name_MLine_Edit->clear();
    
            bar->accept();
            break;
    
    
        case QMessageBox::No:
    
             bar->accept();
             break;
        case QMessageBox::Cancel:
    
            bar->ignore();
            break;
        default:
            break;
        }
    
        }
            this->Name_MLine_Edit->setStyleSheet("#Name_MLine_Edit { border: none; }");
    
        bar->accept();
    }   
    
    void Edit_window::add_stud_canc_window()
    {
        Edit_window::close();
    }
    
    void Edit_window::table_cell_changed()
    {
        datachanged = true;
    }
    
    void Edit_window::save_chng_button_pressed()
    {
    //Here appears runtime error, when tableWidget->item(0,1)->text() is performed 
            if(qry.exec("insert into students.students_report_card(firstname,lastname,physics,maths,chemystry,computer_science) values(\"" +
                         tableWidget->item(0,1)->text() + "\", \"" + tableWidget->item(0,2)->text() +
                         "\"," + tableWidget->item(0,3)->text() +
                         "," +tableWidget->item(0,4)->text()+"," + tableWidget->item(0,5)->text() +
                         ","+tableWidget->item(0,6)->text() + ");" ))
            {
                qDebug() <<"Succesfully added a student";
            }
            else
            {
                QMessageBox msgWarning;
                    msgWarning.setText("Cannot add the student. "+ qry.lastError().text());
                    msgWarning.setIcon(QMessageBox::Warning);
                    msgWarning.setWindowTitle("Caution");
                    msgWarning.exec();
            }
        }
        
        Edit_window::~Edit_window()
        {
            delete add;
        }
    
    1 Reply Last reply
    0
    • C Offline
      C Offline
      ChrisW67
      wrote on last edited by
      #2

      @Isidore said in getting runtime error (SIGSEGV) in private slot:

      I tried to perform tableWdiget->item(0,1); in save_chng_button_pressed(); slot and it works fine

      By "works fine" you mean it does not crash the program. The call to item() plainly returns either a nullptr or an invalid pointer that you try to use to access the text() method precipitating the SIGSEGV. Maybe there's no first row or no second column, or maybe it is the seventh column that is missing (it's all one statement). You need to work out how that can happen using your actual code. The code here is full of odd things like:

      connect(inner_add_button, SIGNAL(clicked()), this, SLOT(saven_chng_button_pressed()));
      

      Where is that slot? How many times does this connection get made during the life of your program?

      Not immediately related to the crash: building strings to execute as SQL is the road to ruin. Take a look at QSqlQuery::prepare() and QSqlQuery::bindValue() (https://doc.qt.io/qt-5/qsqlquery.html#approaches-to-binding-values)

      1 Reply Last reply
      3

      • Login

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