Results 1 to 2 of 2

Thread: QComboBox, model and other values - how to get them?

  1. #1
    Join Date
    Feb 2012
    Location
    Olsztyn, Poland
    Posts
    3
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default QComboBox, model and other values - how to get them?

    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:

    Qt Code:
    1. QSqlQueryModel *modelClientsList = new QSqlQueryModel;
    2. 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");
    3. modelClientsList->setHeaderData(0, Qt::Horizontal, QObject::trUtf8("ID"));
    4. modelClientsList->setHeaderData(1, Qt::Horizontal, QObject::trUtf8("Name"));
    5. modelClientsList->setHeaderData(2, Qt::Horizontal, QObject::trUtf8("City"));
    To copy to clipboard, switch view to plain text mode 
    AND
    Qt Code:
    1. ui->clientComboBox->setModel(modelClientsList);
    2. ui->clientComboBox->setModelColumn(1);
    3. ui->clientComboBox->setCurrentIndex(-1);
    To copy to clipboard, switch view to plain text mode 
    AND
    Qt Code:
    1. void StackedWidget::on_clientComboBox_currentIndexChanged(int index)
    2. {
    3. qDebug() << ui->clientComboBox->itemData(index);
    4. qDebug() << ui->clientComboBox->itemData(currentIndex()).toString();
    5. }
    To copy to clipboard, switch view to plain text mode 

    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!
    Last edited by zuberek; 22nd February 2012 at 07:55.

  2. #2
    Join Date
    Feb 2012
    Location
    Olsztyn, Poland
    Posts
    3
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default Re: QComboBox, model and other values - how to get them?

    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 ->
    Qt Code:
    1. ui->comboBox->setCurrentIndex(ui->comboBox->findData(CurrentClientId));
    To copy to clipboard, switch view to plain text mode 

    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

Similar Threads

  1. model and default columns values
    By m15ch4 in forum Qt Programming
    Replies: 0
    Last Post: 8th May 2011, 21:57
  2. Replies: 7
    Last Post: 3rd November 2010, 23:18
  3. Replies: 1
    Last Post: 18th November 2009, 18:06
  4. Replies: 1
    Last Post: 3rd June 2009, 19:08
  5. QComboBox values?
    By dbrmik in forum Newbie
    Replies: 8
    Last Post: 9th January 2009, 08:32

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.