PDA

View Full Version : Changing text label depending on combobox selection



Dirty_Dylan
6th July 2012, 03:54
Hi,
I have a problem that is probably very simple, but I don't know how to do it.
I have a basic contacts book where essentially there is a combo box that you can use to scroll through names and once a name is selected it should change a text box with the persons phone number. The problem it all seems to work exept the query of the phone number.

QString *phone = ui->cmbMembers->currentText();
phone = "SELECT phone FROM contacts WHERE name = " + phone + ";";

QSqlQuery queryNumbers(phone);
ui->lblPhone->clear();
ui->lblPhone->setText(phone);

Thanks :)

ChrisW67
6th July 2012, 05:24
QComboBox::currentText() does not return a pointer to a QString. This code will not compile.

If phone did, somehow, contain a person's name then your attempt to build an SQL query will produce invalid SQL (except in a few unrealistic circumstances). Building SQL this way is a bad idea anyway; you should read about Approaches to Binding Values.

If the SQL was valid, you would need to execute it, say with QSqlQuery::exec(), before you could get results. The method you have used to construct the query would execute it for you if the SQL were valid.

If you executed it you would need to retrieve the returned value(s) , say with QSqlQuery::next() and QSqlQuery::value(), before you could set the label.