Results 1 to 2 of 2

Thread: Automatically refresh QSqlQueryModel from external from dialog

  1. #1
    Join Date
    Nov 2008
    Posts
    22
    Qt products
    Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11

    Default Automatically refresh QSqlQueryModel from external from dialog

    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.

    Qt Code:
    1. void AddClientForm::addclient2db_accept()
    2. { // this is the slot called to accept the entry and INSERT it into the desired table
    3. .... code adding data to db record ...
    4. model->clear();
    5. model->setQuery("SELECT FirstName,LastName,ClientID FROM Clients");
    6. model->setHeaderData(0,Qt::Horizontal, tr("First"));
    7. model->setHeaderData(1,Qt::Horizontal, tr("Last Name"));
    8. model->setHeaderData(2,Qt::Horizontal, tr("Client ID"));
    9.  
    10. accept();
    11. }
    To copy to clipboard, switch view to plain text mode 

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

    Qt Code:
    1. /* AddClientForm - Add a new client to the database */
    2. class AddClientForm : public QDialog
    3. {
    4. Q_OBJECT
    5.  
    6. public:
    7. AddClientForm(QWidget *parent=0);
    8.  
    9. private slots:
    10. void addclientform_reset();
    11. void addclient2db_accept();
    To copy to clipboard, switch view to plain text mode 

    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-p...del-21742.html

  2. #2
    Join Date
    Feb 2008
    Posts
    98
    Thanks
    2
    Thanked 24 Times in 24 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Automatically refresh QSqlQueryModel from external from dialog

    Just pass the form a pointer to the model. You may do it in the constructor:

    Qt Code:
    1. class AddClientForm : public QDialog
    2. {
    3. Q_OBJECT
    4.  
    5. public:
    6. AddClientForm(QSqlQueryModel *model, QWidget *parent=0);
    7.  
    8. private slots:
    9. void addclientform_reset();
    10. void addclient2db_accept();
    11.  
    12. private:
    13. QSqlQueryModel *m_model;
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. AddClientForm::AddClientForm(model, parent)
    2. : QDialog(parent) {
    3. m_model = model;
    4. ...
    5. }
    6.  
    7. void AddClientForm::addclient2db_accept()
    8. { // this is the slot called to accept the entry and INSERT it into the desired table
    9. .... code adding data to db record ...
    10. m_model->clear();
    11. m_model->setQuery("SELECT FirstName,LastName,ClientID FROM Clients");
    12. m_model->setHeaderData(0,Qt::Horizontal, tr("First"));
    13. m_model->setHeaderData(1,Qt::Horizontal, tr("Last Name"));
    14. m_model->setHeaderData(2,Qt::Horizontal, tr("Client ID"));
    15.  
    16. accept();
    17. }
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. AddClientForm *form = new AddClientForm(model, this);
    2. form->show();
    To copy to clipboard, switch view to plain text mode 

Similar Threads

  1. how can I refresh QSqlQueryModel?
    By hashb in forum Qt Programming
    Replies: 3
    Last Post: 20th June 2009, 03:39
  2. Q3ScrollView resists to scroll down to the garbage bin
    By sivrisinek in forum Qt Programming
    Replies: 0
    Last Post: 5th February 2009, 17:50
  3. shared vs static
    By alisami in forum Installation and Deployment
    Replies: 3
    Last Post: 4th October 2008, 13:04
  4. Replies: 16
    Last Post: 23rd May 2008, 10:12
  5. Link Errors
    By magikalpnoi in forum Qt Programming
    Replies: 5
    Last Post: 25th September 2006, 22:04

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.