KillGabio
15th July 2013, 06:24
Hi everyone. Im having the following problem. I can fetch all the rows of my query and view this model with the QTableView.
But the problem comes when, after updating a cell, the view of the model trims again only showing the first 256 rows. Here is the class:
WidgetModificacion::WidgetModificacion(QWidget *parent, int tipo_tabla) :
QWidget(parent),
ui(new Ui::WidgetModificacion)
{
ui->setupUi(this);
this->tipo_tabla = tipo_tabla;
db = QSqlDatabase::database ("app_db");
if (db.isOpen ()){
model = new QSqlTableModel (this, db);
switch (tipo_tabla){
case 0:
model->setTable ("TABLE_PRODUCTOS");
break;
case 1:
model->setTable ("TABLE_CLIENTES");
break;
}
model->setEditStrategy(QSqlTableModel::OnFieldChange);
model->setSort (1, Qt::AscendingOrder);
model->select ();
while (model->canFetchMore()) model->fetchMore();
this->ui->tableView->setModel (model);
QStyledItemDelegate *display = new NumberRepresentation(this);
ui->tableView->setItemDelegateForColumn (0, display);
ui->tableView->setItemDelegateForColumn (4, display);
ui->tableView->setColumnWidth(1,250);
connect (this->model, SIGNAL(beforeUpdate (int, QSqlRecord &)), this, SLOT (actualizar_fecha (int, QSqlRecord &)));
}
else{
QMessageBox::critical(0, qApp->tr("Cannot open database"),
qApp->tr("Unable to establish a database connection.\n"
"Llamar a Agus.\n\n"
"Click Cancel to exit."), QMessageBox::Cancel);
}
}
WidgetModificacion::~WidgetModificacion()
{
delete ui;
}
void WidgetModificacion::on_cerrar_clicked()
{
emit finaliza_modificacion (QString (":ImagesMilk/Iconos/uvacopa.png"));
this->db.close ();
this->close ();
}
void WidgetModificacion::actualizar_fecha (int row, QSqlRecord & record){
if (tipo_tabla == 0){
QSqlQuery *query = new QSqlQuery (db);
query->prepare ("UPDATE TABLE_PRODUCTOS SET fecha_modificacion = SYSDATE WHERE id_codigo_barra = :value");
query->bindValue (":value", this->ui->tableView->model ()->data (this->ui->tableView->model ()->index (row, 0)).toString ());
query->exec ();
//while (model->canFetchMore()) model->fetchMore();
//this->ui->tableView->setModel (model);
}
}
The code that is commented i've alredy tried it, and doesnt work. What i want is to modify the cell and stay at the same position of the table i was before the update. Thank you all in advance.
But the problem comes when, after updating a cell, the view of the model trims again only showing the first 256 rows. Here is the class:
WidgetModificacion::WidgetModificacion(QWidget *parent, int tipo_tabla) :
QWidget(parent),
ui(new Ui::WidgetModificacion)
{
ui->setupUi(this);
this->tipo_tabla = tipo_tabla;
db = QSqlDatabase::database ("app_db");
if (db.isOpen ()){
model = new QSqlTableModel (this, db);
switch (tipo_tabla){
case 0:
model->setTable ("TABLE_PRODUCTOS");
break;
case 1:
model->setTable ("TABLE_CLIENTES");
break;
}
model->setEditStrategy(QSqlTableModel::OnFieldChange);
model->setSort (1, Qt::AscendingOrder);
model->select ();
while (model->canFetchMore()) model->fetchMore();
this->ui->tableView->setModel (model);
QStyledItemDelegate *display = new NumberRepresentation(this);
ui->tableView->setItemDelegateForColumn (0, display);
ui->tableView->setItemDelegateForColumn (4, display);
ui->tableView->setColumnWidth(1,250);
connect (this->model, SIGNAL(beforeUpdate (int, QSqlRecord &)), this, SLOT (actualizar_fecha (int, QSqlRecord &)));
}
else{
QMessageBox::critical(0, qApp->tr("Cannot open database"),
qApp->tr("Unable to establish a database connection.\n"
"Llamar a Agus.\n\n"
"Click Cancel to exit."), QMessageBox::Cancel);
}
}
WidgetModificacion::~WidgetModificacion()
{
delete ui;
}
void WidgetModificacion::on_cerrar_clicked()
{
emit finaliza_modificacion (QString (":ImagesMilk/Iconos/uvacopa.png"));
this->db.close ();
this->close ();
}
void WidgetModificacion::actualizar_fecha (int row, QSqlRecord & record){
if (tipo_tabla == 0){
QSqlQuery *query = new QSqlQuery (db);
query->prepare ("UPDATE TABLE_PRODUCTOS SET fecha_modificacion = SYSDATE WHERE id_codigo_barra = :value");
query->bindValue (":value", this->ui->tableView->model ()->data (this->ui->tableView->model ()->index (row, 0)).toString ());
query->exec ();
//while (model->canFetchMore()) model->fetchMore();
//this->ui->tableView->setModel (model);
}
}
The code that is commented i've alredy tried it, and doesnt work. What i want is to modify the cell and stay at the same position of the table i was before the update. Thank you all in advance.