Results 1 to 6 of 6

Thread: better way to set data in a QCombobox

  1. #1
    Join Date
    Jun 2015
    Posts
    28
    Qt products
    Qt5
    Platforms
    Unix/X11

    Default better way to set data in a QCombobox

    Hello,
    I'm implementing my "QCombobox set current data" and I’m in doubt about what is the better way?

    1) using variable and QMapIterarot to manipulate data, and QCombobox.setCurrentText()
    Qt Code:
    1. QMapIterator<QString, int> i(_cbNames);
    2. while (i.hasNext()) {
    3. i.next();
    4.  
    5. if (query2.value(1) == i.key())
    6. {
    7. ui->qcomboboxNames->setCurrentText(i.key());
    8. }
    To copy to clipboard, switch view to plain text mode 


    2) using directly combobox to manipulate data and QCombobox.setCurrentIndex()
    Qt Code:
    1. for (int i=0;i<=ui->qcomboboxNames->count();i++)
    2. {
    3. ui->qcomboboxNames->setCurrentIndex(i);
    4. if (query2.value(1) == ui->qcomboboxNames->currentText())
    5. {
    6. break;
    7. }
    8. }
    To copy to clipboard, switch view to plain text mode 

    Thanks!
    Juliano

  2. #2
    Join Date
    Dec 2009
    Location
    New Orleans, Louisiana
    Posts
    791
    Thanks
    13
    Thanked 153 Times in 150 Posts
    Qt products
    Qt5
    Platforms
    MacOS X

    Default Re: better way to set data in a QCombobox

    I write the best type of code possible, code that I want to write, not code that someone tells me to write!

  3. #3
    Join Date
    Jun 2015
    Posts
    28
    Qt products
    Qt5
    Platforms
    Unix/X11

    Default Re: better way to set data in a QCombobox

    Quote Originally Posted by jefftee View Post
    Hello, thanks for your reply, but I dont have doubts about add item. My doubt is What of 2 methods (or another) are the Best way to set the current data in a combobox witch Already have items.

    Thanks
    Juliano

  4. #4
    Join Date
    Dec 2009
    Location
    New Orleans, Louisiana
    Posts
    791
    Thanks
    13
    Thanked 153 Times in 150 Posts
    Qt products
    Qt5
    Platforms
    MacOS X

    Default Re: better way to set data in a QCombobox

    If that's all you're asking, surely you can time both methods and decide for yourself which is better, right?
    I write the best type of code possible, code that I want to write, not code that someone tells me to write!

  5. #5
    Join Date
    Mar 2008
    Location
    Kraków, Poland
    Posts
    1,536
    Thanked 284 Times in 279 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: better way to set data in a QCombobox

    Why not like this :
    Qt Code:
    1. int index = ui->qcomboboxNames->findText(query2.value(1));
    2. if( index >= 0 )
    3. ui->qcomboboxNames->setCurrentIndex(index);
    To copy to clipboard, switch view to plain text mode 

  6. #6
    Join Date
    Jun 2015
    Posts
    28
    Qt products
    Qt5
    Platforms
    Unix/X11

    Default Re: better way to set data in a QCombobox

    Quote Originally Posted by Lesiok View Post
    Why not like this :
    Qt Code:
    1. int index = ui->qcomboboxNames->findText(query2.value(1));
    2. if( index >= 0 )
    3. ui->qcomboboxNames->setCurrentIndex(index);
    To copy to clipboard, switch view to plain text mode 
    Yeah! This code is more simple and clear, thanks bro!

Similar Threads

  1. MySql data not shown in QcomboBox
    By diyanafadzil in forum Newbie
    Replies: 3
    Last Post: 17th September 2015, 15:59
  2. Replies: 8
    Last Post: 16th July 2015, 19:44
  3. Replies: 1
    Last Post: 20th May 2015, 11:21
  4. Find data in QCombobox invisible column
    By Gretsch in forum Qt Programming
    Replies: 6
    Last Post: 1st July 2013, 08:45
  5. Replies: 5
    Last Post: 10th June 2011, 10:58

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.