PDA

View Full Version : QComboBox, model and other values - how to get them?



zuberek
22nd February 2012, 07:38
Hello!

First of all, Im new here, so I just want to say Hi to everybody ;)

Ok, here is my problem.

Look at this code:


QSqlQueryModel *modelClientsList = new QSqlQueryModel;
modelClientsList->setQuery("SELECT client_id, client_fullName, client_city, client_street, client_code, client_nip, client_regon, client_contactName, client_phone, client_fax, client_url, client_email, client_notes FROM scm_clients");
modelClientsList->setHeaderData(0, Qt::Horizontal, QObject::trUtf8("ID"));
modelClientsList->setHeaderData(1, Qt::Horizontal, QObject::trUtf8("Name"));
modelClientsList->setHeaderData(2, Qt::Horizontal, QObject::trUtf8("City"));
AND

ui->clientComboBox->setModel(modelClientsList);
ui->clientComboBox->setModelColumn(1);
ui->clientComboBox->setCurrentIndex(-1);
AND

void StackedWidget::on_clientComboBox_currentIndexChang ed(int index)
{
qDebug() << ui->clientComboBox->itemData(index);
qDebug() << ui->clientComboBox->itemData(currentIndex()).toString();
}

What I want to do is when I choose something from comboBox it should automatically put values (i.e. client_fullName, client_city, client_street ...) into lineEdit positions.
And here is the question: How can I get other values set by model from comboBox?

Second thing is, I use the very same model for displaying data in QTableView and it works fine. I use QStackedWidget for displaying whole stuff. I have a "new invoice" button next to the QTableView. Question: How to set QComboBox position to be the same as selected position from QTableView?

I tried to pass var CurrentClientId (which is client_id from database), but QComboBox provides default autoincrement values for positions in combobox. So CurrentClientId from QTableView is not the same as currentIndex() from QComboBox.

I hope I've explained everything clearly and You can understand what my problem is :-)

EDIT:
Q1: I guess I can make it work by adding items one by one in while(...) loop using addItem(name, id) and then querying each position separately, but it doesn't feels right. Any hint?

Thanks for any help!

Cheers!

zuberek
22nd February 2012, 12:32
Ok, I managed to do it another way.

I made query (select name & id from DB) and I used "while" loop to put everything in ComboBox using addItem(name, id).
And then I used function on_comboBox_currentIndexChanged(int index) which exec another query and get all necessary values from DB (get id from itemData(currentIndex()) ) and setText to all lineEdit positions.

It works, but I dont like the way I did it. I think I connect to DB too many times, but I have no idea how to do it another way.

Second problem is also solved. I just used this ->

ui->comboBox->setCurrentIndex(ui->comboBox->findData(CurrentClientId));

As I said, problem is solved, but I think there should be nicer way of doing this. If anybody have another solution it would be nice to share ;)