PDA

View Full Version : QlineEdit + Sql Problem



anouar2002
18th January 2012, 16:40
Hello,
I want create a QlineEdit which shows values from SQL Server Database.
I try to do it in this way but no result (empty QlineEdit area):( :

QString sQuery = QString("SELECT Matricule FROM Projet.dbo.Produit WHERE Nom='%1' ").arg(ui->comboBox->currentText());

QSqlQuery query;
query.exec( sQuery );

QString name = query.value(0).toString();

ui->lineEdit_3->setText(name);

}
Any Suggestions ?? Thanks previsiouly :o

Lykurg
18th January 2012, 17:00
You maybe want to have a look at QSqlQuery::prepare() and call QSqlQuery::next() before trying to get values.

And this is surely not a post for the "Qt Programming" forum. Moved.

anouar2002
18th January 2012, 19:55
Hello
Thank you for answering me. I read the documentation and i try it many times. No result :crying:
This is my first time that programming Qt and also C++.

QSqlQuery query;
query.prepare("SELECT Matricule FROM Projet.dbo.Produit WHERE Nom=? ");

query.exec( sQuery );

QString name = query.value(0).toString();


ui->lineEdit_3->setText(name);
Help ??

Lykurg
18th January 2012, 21:14
Help ??Already given!

ChrisW67
18th January 2012, 21:43
Help ??
Read QSqlQuery::bindValue(), QSqlQuery::next(), look at the return value from QSqlQuery::exec() and QSqlQuery::prepare(), and look at QSqlQuery::lastError() if needed. The documentation is your friend.

anouar2002
19th January 2012, 00:44
Hello,
I found a solution that works perfectly for display but when I change the value of the ComboBox, the value of lineEdit remains the same.
here is the code for this solution:

this->model = new QSqlQueryModel();
model->setQuery("SELECT Nom FROM Projet.dbo.Produit");
ui->comboBox->setModel(model);


QSqlQuery query;
query.prepare("SELECT Matricule FROM Projet.dbo.Produit WHERE Nom=:nom");
query.bindValue(":nom", ui->comboBox->currentText());
if (query.exec() && query.next()) {
ui->lineEdit_3->setText(query.value(0).toString());
}

For information, in my database each 'nom' (displayed in comboBox) corresponds to a single 'id' (displayed in a lineEdit).
Is there a method to the value of lineEdit changes when the ComboBox is changed? Thank you in advance

wysota
19th January 2012, 00:51
Do you rerun this code after changing the combobox value?

anouar2002
19th January 2012, 01:12
Do you rerun this code after changing the combobox value?
Yessssssssssssss

ChrisW67
19th January 2012, 02:57
Set a breakpoint at line 7, change the combo box value, and single step through after it stop at your break oint. Is line 10 being reached? The problem will probably become obvious.

anouar2002
19th January 2012, 03:26
Thank you for answering me.
But i didn't understand what you mean by breakpoint and single step. Can u make them in that code please if you don't mind and thanks again

ChrisW67
19th January 2012, 04:36
A breakpoint is something you set when you are running your program in a debugger, not something you do in the actual code. Start your program in a debugger. If you are using Qt Creator then:

Build a Debug version of your program
Locate the line(s) you want to stop at and press F9 to set/clear a breakpoint.
Press F5 to launch it in the debugger.
Use F10 and F11 to single step. Look at variables in the Locals and Expressions tab. Other options are on the Debug menu.

Lykurg
19th January 2012, 06:29
Looks to me that QDataWidgetMapper could be of interest. Combined with QDataWidgetMappersetCurrentIndex() and QComboBox::currentIndexChanged().

wysota
19th January 2012, 09:44
Lorenz, don't be a spoilsport :)

anouar2002
19th January 2012, 14:09
Thanks everybody for answers.
Chris, I tried to follow your instructions but it was really difficult because it's my first time I use Qt and my version is in French. Anyway, if there is a tutorial video, it will be great. And Thanks again!

Lykurg, I'm not really good at programming and this is my first program in Qt Here I tried to do as you have told me but it did not work.

this->model = new QSqlQueryModel();
model->setQuery("SELECT Nom FROM Projet.dbo.Produit");
ui->comboBox->setModel(model);


QSqlQuery query;
query.prepare("SELECT Matricule FROM Projet.dbo.Produit WHERE Nom=:nom");
query.bindValue(":nom", ui->comboBox->currentText());
if (query.exec() && query.next()) {
ui->lineEdit_3->setText(query.value(0).toString());
}
QObject::connect(ui->comboBox, SIGNAL(currentIndexChanged(int)),ui->lineEdit_3, SLOT(setText(ui->comboBox->currentText())));

What should I do? Thank you in advance

wysota
19th January 2012, 14:12
Your connect statement is invalid. Read about signals and slots, connect() only expects datatypes, not arbitrary expressions. And back to my previous question -- how are you rerunning the code from your post when the combobox current index changes?

anouar2002
19th January 2012, 14:29
Thanks everyone. I used SLOT like you said and it works perfectly !!!:)