PDA

View Full Version : Automatically refresh QSqlQueryModel from external from dialog



windsword
25th August 2009, 12:57
Hello,

I want to automatically refresh my table, after I add a new record to the database.

I use a button to bring up a dialog form, in the "accept" button slot I'd like to refresh it before
executing the "accept()" function. But I get an error message that the
model is not defined in this class.


void AddClientForm::addclient2db_accept()
{ // this is the slot called to accept the entry and INSERT it into the desired table
.... code adding data to db record ...
model->clear();
model->setQuery("SELECT FirstName,LastName,ClientID FROM Clients");
model->setHeaderData(0,Qt::Horizontal, tr("First"));
model->setHeaderData(1,Qt::Horizontal, tr("Last Name"));
model->setHeaderData(2,Qt::Horizontal, tr("Client ID"));

accept();
}



model is defined in another class, but the above slot is define as shown in this header file:



/* AddClientForm - Add a new client to the database */
class AddClientForm : public QDialog
{
Q_OBJECT

public:
AddClientForm(QWidget *parent=0);

private slots:
void addclientform_reset();
void addclient2db_accept();


Do I need to inherit the parent class (where the button to add a new record is defined)?


Reference:
http://www.qtcentre.org/forum/f-qt-programming-2/t-how-can-i-refresh-qsqlquerymodel-21742.html

victor.fernandez
25th August 2009, 13:26
Just pass the form a pointer to the model. You may do it in the constructor:


class AddClientForm : public QDialog
{
Q_OBJECT

public:
AddClientForm(QSqlQueryModel *model, QWidget *parent=0);

private slots:
void addclientform_reset();
void addclient2db_accept();

private:
QSqlQueryModel *m_model;



AddClientForm::AddClientForm(model, parent)
: QDialog(parent) {
m_model = model;
...
}

void AddClientForm::addclient2db_accept()
{ // this is the slot called to accept the entry and INSERT it into the desired table
.... code adding data to db record ...
m_model->clear();
m_model->setQuery("SELECT FirstName,LastName,ClientID FROM Clients");
m_model->setHeaderData(0,Qt::Horizontal, tr("First"));
m_model->setHeaderData(1,Qt::Horizontal, tr("Last Name"));
m_model->setHeaderData(2,Qt::Horizontal, tr("Client ID"));

accept();
}



AddClientForm *form = new AddClientForm(model, this);
form->show();