Lincoln Ingaroca
Lincoln Ingaroca

Reputation: 116

Why Application terminates when choosing a row from the QTableView

My application closes when I select a row from the QTableView, which I load, from a PostgreSQL DB.

I have tried making the connection this way but the result is the same.

QObject::connect(ui->tableView->selectionModel(),&QItemSelectionModel::currentChanged,this,
                       [&](){
                         int id=model->index(ui->tableView->currentIndex().row(),0).data().toInt();
                         dataMonitoreo(id);
                       });

When selecting a row, other data should be shown in the controls below, from another table, I leave the code:

.h

#ifndef WIDGET_H
#define WIDGET_H

#include <QWidget>
#include <QSqlDatabase>
#include <QSqlError>
#include <QSqlQueryModel>
#include <QSqlQuery>

QT_BEGIN_NAMESPACE
namespace Ui { class Widget; }
QT_END_NAMESPACE

class Widget : public QWidget
{
  Q_OBJECT

public:
  Widget(QWidget *parent = nullptr);
  ~Widget();

  void data(int id);
  bool getConection();
  void dataMonitoreo(int id);

private slots:
  void on_tableView_clicked(const QModelIndex &index);

private:
  Ui::Widget *ui;
  QSqlDatabase db;
  QSqlQueryModel *model;
};
#endif // WIDGET_H

.cpp:

#include "widget.h"
#include "ui_widget.h"
#include <QDebug>

Widget::Widget(QWidget *parent)
  : QWidget(parent), ui(new Ui::Widget)
{
  ui->setupUi(this);
  data(25);
}

Widget::~Widget()
{
  delete ui;
}
bool Widget::getConection()
{
  db=QSqlDatabase::addDatabase("QPSQL");
  if(!db.isDriverAvailable("QPSQL")){
    qDebug()<<db.lastError().text();
    return false;
  }
  db.setDatabaseName("monitoreo_db");
  db.setHostName("localhost");
  db.setPassword("123456");
  db.setPort(5432);
  db.setUserName("postgres");
  if(!db.open()){
    qDebug()<<"Failed to open database.\n"<<db.lastError().databaseText();
    return false;
  }
  return true;

}

void Widget::dataMonitoreo(int id)
{
  getConection();
  QSqlQuery qry;
  qry.prepare("SELECT codigo_estacion,fecha_muestra,hora_muestra FROM datos_monitoreo"
              " WHERE id_estacion=?");
  qry.addBindValue(id);
  if(!qry.exec()){
    qDebug()<<qry.lastError().text();
    qDebug()<<qry.lastError().nativeErrorCode();
    return;
  }
  qry.next();
  ui->lineEdit->setText(qry.value(0).toString());
  ui->dateEdit->setDate(qry.value(1).toDate());
  ui->timeEdit->setTime(qry.value(2).toTime());


}

void Widget::data(int id)
{
  getConection();
  QSqlQuery qry;
  qry.prepare("SELECT id_estacion, codigo_estacion, fecha_muestra, id_cliente "
              "FROM datos_monitoreo WHERE id_cliente=?;");
  qry.addBindValue(id);
  if(!qry.exec()){
    qDebug()<<qry.lastError().text();
    qDebug()<<qry.lastError().nativeErrorCode();
    return;
  }
  model=new QSqlQueryModel;
  model->setQuery(qry);
  ui->tableView->setModel(model);
}

void Widget::on_tableView_clicked(const QModelIndex &index)
{
  dataMonitoreo(model->index(index.row(),0).data(Qt::DisplayRole).toInt());
}

.ui

enter image description here

enter image description here

Upvotes: 0

Views: 62

Answers (1)

SageCat
SageCat

Reputation: 345

Every time you use getConection() you duplicate connection to the database. I don’t know if this is causing the program to fall, but it is at least something bad. Try to fix it.

Also, this is the possible thing to fail:

void Widget::dataMonitoreo(int id)
{
...
qry.next();
ui->lineEdit->setText(qry.value(0).toString());
ui->dateEdit->setDate(qry.value(1).toDate());
ui->timeEdit->setTime(qry.value(2).toTime());
}

If query reads empty table, then it will get an invalid data. As a result you try to read the non-existing values. It's a possible way to get segmentation fault. From the docs:

An invalid QVariant is returned if field index does not exist, if the query is inactive, or if the query is positioned on an invalid record.

Upvotes: 1

Related Questions