PDA

View Full Version : QMYSQL Problem?



baray98
6th October 2009, 01:13
I have a QTableView and I connected an QSqlQueryModel on it in my app. My database connection is MySQL. The whole idea is to replace view's model everytime the user made a valid SQL statement so,,,

User Input SQL statement ->create QSqlQuery and if valid ->create QSqlQueryModel-> set it to view

if in the creating of sqlQuery is not Valid then show an error and return.

now the strange things happen is that when my view is connected to QSqlQueryModel (previuosly produced from a valid SQL statement) then another SQL statement coming but its invalid (with error) the whole application CRUSH.

I traced down and i think it crushed when my model is trying to paint himself, since i am showing a message on top, and try to access some data from its model. I am sure that model is till alive and kicking. I am not changing model in my view until I have a good sql statement.

here a snippet of my code


void MyClass::anotherStatement( QString statement)
{
QSqlQuery query (statement,*p_data->m_db);
if (query.lastError().isValid())
{
qApp->restoreOverrideCursor();
QMessageBox::critical(this,tr("SQL Error"),tr("%1").arg(query.lastError().text()));
return; // it should not replace my existing model
}

QSqlQueryModel* model = new QSqlQueryModel(this);
model->setQuery(query);

// get the old model if there any..
.....
p_data->view->setModel(model);
model->setParent(p_data->view); //reparent - just to make sure its gone when the view is deleted

//delete old model if exist
}


This is ONLY happening in QMYSQL and have used it on QODBC database and it work fine...

please help,

baray98