PDA

View Full Version : QSqlTableModel/QSqlQueryModel



dragon
10th December 2006, 20:51
Hello everyone,

I,am newbie,

I have a MainWindow with several lineEdits calling: leMateriaal, leDin_norm etc..
I have a searchDialog with comboboxen calling: materiaalComboBox, dinCombobox etc..
I have a connection with a extern database with several rows of input.
If i pop-up my searchDialog in the MainWindow en choose in the combox of the searchDialog the currentText() en push the button ok the seachDialog must be searching in the database for the chosen items en show them in the MainWindow lineEdits.
The problem is i try several options but nothing works.

The first option was QSqlTableModel:


void MainWindow::search()
{
searchDialog dlg(this)
QString mat = dlg.leMateriaalComboBox->currentText()
QString din = dlg.leDinComboBox->currentText()
if(dlg.exec() == QDialog::Accepted) {

QSqltableModel model;
model.setTable("frezen");
model.select();
QString mat = model.record(1).value("Materiaal").toString();
leMateriaal->setText(mat);
QString din = model.record(1).value("Din_norm").toString();
leDin->setText(din);
}
}

In the MainWindow i only see the first record not the the item i choose in my searchDialog combobox.
My second option was the same with QSqlQueryModel.

Is it possible when i choose items in the comboboxen in the searchDialog then searchDialog searching in the database en put the items in my MainWindow lineEdits.
Is something missing in my code?

jacek
10th December 2006, 20:57
Use QSqlTableModel::setFilter() to fetch only the row you want.

dragon
11th December 2006, 18:06
I have try setFilter in my code like this:



QSqltableModel model;
model.setTable("frezen");
model.setFilter("Materiaal = 'Koolstofstaal'");
model.select();

for (int i = 0; i < model.rowCount(); ++i) {
QString mat = model.record(i).value("Materiaal").toString();
leMateriaal->setText(mat);
}


If i read the documentation about setFilter in QSqlTableModel he only accepts one String &filter ?
In my materiaalCombobox i have 5 different items.
Example:
Koolstofstaal, Gelegeerdstaal, Gereedschapstaal etc..
Is possible for setFilter to take more dan one String &filter.

jacek
11th December 2006, 20:59
Is possible for setFilter to take more dan one String &filter.
Yes, the filter is exactly the same string which you would place after WHERE in SQL query.

dragon
12th December 2006, 18:44
Thanks jacek for you answer.

dragon
12th December 2006, 19:49
I have one thing about setFilter.
Normally the use of setFilter is this: model.setFilter("Materiaal = 'Koolstofstaal'"); and this works fine.
Have anybody try this with a bigger string:
model.setFilter("Materiaal = 'Koolstofstaal' and Materiaal = 'Gelegeerdstaal' and Materiaal = 'Gereeedshapstaal'");
If i try this there nothing showing up in my lineEdits.
I want put this strings together to make a choice of a item in my combobox.
thanks in advance.

jpn
12th December 2006, 19:52
Try "or" instead of "and"... ;)

dragon
12th December 2006, 21:40
I have tried model.setFilter("Materiaal = 'Koolstofstaal' or Materiaal = 'Gelegeerdstaal'or Materiaal = 'Gereedschapstaal'");
With no succes it shows only the first string Materiaal = 'Koolstofstaal'

jacek
12th December 2006, 22:17
What value does model.rowCount() return after setFilter() and select()?

dragon
13th December 2006, 18:09
This a part of the code.
void MainWindow::search()
{
searchDialog dlg(this)
QString mat = dlg.leMateriaalComboBox->currentText()
if(dlg.exec() == QDialog::Accepted) {

QSqltableModel model;
model.setTable("frezen");
model.setFilter("Materiaal ='Koolstofstaal' or Materiaal = 'Gelegeerdstaal' or Materiaal = 'Gereedschapstaal'");
model.select();

for (int i = 0; i < model.rowCount(); ++i) {
mat = model.record(i).value("Materiaal").toString();
leMateriaal->setText(mat);
}
}
The value from model.rowCount() is only the first part of the setFilter string ( Koolstofstaal ) the orther value's doesn't show up in the lineEdit in Mainwindow.

jacek
14th December 2006, 00:32
model.setFilter("Materiaal ='Koolstofstaal' or Materiaal = 'Gelegeerdstaal' or Materiaal = 'Gereedschapstaal'");
...
mat = model.record(i).value("Materiaal").toString();
Do you really need a model for this? It seems you know a priori what values you want to display (i.e. "Koolstofstaal", "Gelegeerdstaal" and "Gereedschapstaal").


the orther value's doesn't show up in the lineEdit in Mainwindow.
QLineEdit::setText() sets the text --- it doesn't append it.

lineEdit->setText( "aaa" ); // after this line lineEdit will contain "aaa"
lineEdit->setText( "bbb" ); // now "aaa" is replaced by "bbb"

If you want to display several values, concatenate them.

But I think that you would rather want to use a QComboBox instead of QLineEdit.

dragon
14th December 2006, 16:37
Yes i want to use in searchDialog the combobox.
Maby i chance the code for this program.
Thanks jacek voor your time and answers.