Results 1 to 2 of 2

Thread: How to detect data changes in Widgets and transfer them to DB?

  1. #1
    Join Date
    Mar 2007
    Posts
    1
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default How to detect data changes in Widgets and transfer them to DB?

    How to detect data changes in Widgets and to transfer them to DB? QDataWidgetMapper and QSqlTableModel are used.
    Now changes are detect manually through connect textChanged(QString &) - for QLineEdit, editTextChanged(QString &) - for QComboBox, etc. It is possible to automate or make uniform detecting data changes?
    Now changes are transfer through QSqlTableModel::setRecord(int,const QSqlRecord &). It is possible to make it through the delegate?

  2. #2
    Join Date
    Jan 2006
    Location
    Innsbruck, Austria
    Posts
    62
    Thanks
    10
    Thanked 7 Times in 6 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: How to detect data changes in Widgets and transfer them to DB?

    You may subclass QLineEdit, QComboBox, etc and implement a method where you store the data in the database and connect the textChanged(), activated(), etc signals to your method in the subclass. That way you'll only need to implement storing the data only once for each type of widget instead of for each widget instance. For instance:

    mylineedit.h
    Qt Code:
    1. class MyLineEdit : public QLineEdit {
    2.  
    3. Q_OBJECT
    4.  
    5. public:
    6. MyLineEdit(QWidget *parent = 0);
    7.  
    8. void setQuery(const QString& query);
    9.  
    10. public slots:
    11. void startQuery(const QString& text);
    12.  
    13. private:
    14. QString m_query;
    15. };
    To copy to clipboard, switch view to plain text mode 

    mylineedit.cpp
    Qt Code:
    1. MyLineEdit::MyLineEdit(QWidget *parent)
    2. : QLineEdit(parent) {
    3. connect(this, SIGNAL(textChanged(const QString&)), this, SLOT(startQuery(const QString&));
    4. }
    5.  
    6. void MyLineEdit::setQuery(const QString& query)
    7. {
    8. m_query = query;
    9. }
    10.  
    11. void MyLineEdit::startQuery(const QString& text)
    12. {
    13. QString q = m_query.arg(text);
    14. // execute the query stored in "q"
    15. }
    To copy to clipboard, switch view to plain text mode 

    to use:
    Qt Code:
    1. MyLineEdit *lineEdit = new MyLineEdit(this);
    2. lineEdit->setQuery("INSERT INTO table VALUES('%1');");
    To copy to clipboard, switch view to plain text mode 

    Anyway, I wouldn't recommend you executing a SQL query directly from the GUI thread since it will block the GUI unless you're using it in a local network. Instead, better create a thread that handles the queries.
    Last edited by vfernandez; 9th March 2007 at 21:16.

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.