PDA

View Full Version : Filling form after shwoing it



SIFE
17th March 2012, 21:46
I have this class:

#include <QSqlError>
#include <QFile>
#include <QTextStream>
#include <QMessageBox>

#include "itemsell.h"
#include "ui_ItemSell.h"
#include "sales.h"

ItemSell::ItemSell(QWidget *parent) :
QDialog(parent),
ui(new Ui::ItemSell)
{
ui->setupUi(this);
}

void ItemSell::errorLog(QString error)
{
QFile log("errors.log");
log.open(QFile::Append | QFile::Text);
QTextStream out(&log);
out << error;
log.close();
}

void ItemSell::getSale(const int& id)
{
sqlQuery = "SELECT Sales.Countity as Scoun, Sales.Totale as Stotale FROM Sales "
"JOIN Items ON Sales.ID_I = Items.ID_I "
"WHERE Sales.ID_S=" + id;
if(!query.exec(sqlQuery))
{
error = "[" + query.lastError().text() + "]\t[" + query.lastQuery() + "]\t[" + QDate::currentDate().toString("yyyy-MM-dd") + "\t" +QTime::currentTime().toString("hh-mm") + "]\n";
this->errorLog(error);
}

query.next();
rec = query.record();
ui->itemMat->setText(query.value(rec.indexOf("ID_I")).toString());
ui->nameSale->setText(query.value(rec.indexOf("Name")).toString());
ui->dateSale->setDate(query.value(rec.indexOf("Day")).toDate());
ui->priceSale->setText(query.value(rec.indexOf("Price")).toString());
ui->countitySale->setText(query.value(rec.indexOf("Scoun")).toString());
ui->totaleSale->setText(query.value(rec.indexOf("Stotale")).toString());
QMessageBox sd;
sd.setText(query.lastQuery() + "*** " + query.lastError().text());
sd.exec();
}

ItemSell::~ItemSell()
{
delete ui;
}
And I have this slot executed when the user click on action menu:

void Store::showItem()
{
int id = ui->lstSells->model()->index(m_ind.row(), 0).data().toInt();
ItemSell *item = new ItemSell(this);
item->getSale(id);
item->exec();
}
My problem is the query is never executed because an SQL error statement, it is always executed like this:

CT Sales.Countity as Scoun, Sales.Totale as Stotale FROM Sales JOIN Items ON Sales.ID_I = Items.ID_I WHERE Sales.ID_S=1
Sales.Countity as Scoun, Sales.Totale as Stotale FROM Sales JOIN Items ON Sales.ID_I = Items.ID_I WHERE Sales.ID_S=1
While it should be like this:

SELECT Sales.Countity as Scoun, Sales.Totale as Stotale FROM Sales JOIN Items ON Sales.ID_I = Items.ID_I WHERE Sales.ID_S=1

d_stranz
17th March 2012, 23:35
ItemSell::~ItemSell()
{
delete ui;
}


This is probably corrupting memory and resulting in your sqlQuery constant string getting messed up. The code in your constructor "ui->setup( this )" passes ownership of the ui pointer to the widget, so therefore the ui pointer is getting deleted twice, once by you and once by the widget. Get rid of the delete in your destructor.



void Store::showItem()
{
int id = ui->lstSells->model()->index(m_ind.row(), 0).data().toInt();
ItemSell *item = new ItemSell(this);
item->getSale(id);
item->exec();
}


In this code, if ItemSell is a modal dialog (which it seems to be, since you are calling exec()), then you do not need to create a new one on the heap (using new), you should simply do this:



void Store::showItem()
{
int id = ui->lstSells->model()->index(m_ind.row(), 0).data().toInt();
ItemSell item(this);
item.getSale(id);
item.exec();
}


The exec() statement causes the dialog to block until the user clicks the OK button, then the ItemSell instance will automatically go away as the showItem() method exits scope. The way you have it written now, you have a memory leak - the ItemSell instances you are creating do not get deleted when the showItem method() exits, but since you aren't saving the pointer anywhere, you have no way to get rid of it later. Not until the Store instance is finally destroyed (when your program exits) do the ItemSell instances it owns (because you set the parent to "this") get deleted. So if the user of your Store software runs the program for weeks or months without ever exiting, eventually their computer will use up all the available memory and your program will probably crash.