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
Q_OBJECT
public:
void setQuery(const QString& query);
public slots:
void startQuery(const QString& text);
private:
};
class MyLineEdit : public QLineEdit {
Q_OBJECT
public:
MyLineEdit(QWidget *parent = 0);
void setQuery(const QString& query);
public slots:
void startQuery(const QString& text);
private:
QString m_query;
};
To copy to clipboard, switch view to plain text mode
mylineedit.cpp
MyLineEdit
::MyLineEdit(QWidget *parent
) connect(this, SIGNAL(textChanged(const QString&)), this, SLOT(startQuery(const QString&));
}
void MyLineEdit::setQuery(const QString& query)
{
m_query = query;
}
void MyLineEdit::startQuery(const QString& text)
{
// execute the query stored in "q"
}
MyLineEdit::MyLineEdit(QWidget *parent)
: QLineEdit(parent) {
connect(this, SIGNAL(textChanged(const QString&)), this, SLOT(startQuery(const QString&));
}
void MyLineEdit::setQuery(const QString& query)
{
m_query = query;
}
void MyLineEdit::startQuery(const QString& text)
{
QString q = m_query.arg(text);
// execute the query stored in "q"
}
To copy to clipboard, switch view to plain text mode
to use:
MyLineEdit *lineEdit = new MyLineEdit(this);
lineEdit->setQuery("INSERT INTO table VALUES('%1');");
MyLineEdit *lineEdit = new MyLineEdit(this);
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.
Bookmarks