Results 1 to 10 of 10

Thread: QComboBox drop down list as table

  1. #1
    Join Date
    Dec 2013
    Posts
    15
    Thanks
    3
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: QComboBox drop down list as table

    Guys, do you know how could i make drop down table for QComboBox where selected values from different columns and rows would have different Indexes and were displayed as current item?
    It's not necessary to transform list to table, but there should be multiple columns and rows with different values and different indexes for each.
    It would be awesome if you have some examples
    Last edited by ROCKSTAR; 3rd February 2014 at 06:56.

  2. #2
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QComboBox drop down list as table

    You can set your own model on the combobox and set your own item view.
    See QComboBox::setModel() and QComboBox::setView()

    Try using a table model and a table view

    Cheers,
    _

  3. #3
    Join Date
    Dec 2013
    Posts
    15
    Thanks
    3
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: QComboBox drop down list as table

    Quote Originally Posted by anda_skoa View Post
    You can set your own model on the combobox and set your own item view.
    See QComboBox::setModel() and QComboBox::setView()

    Try using a table model and a table view

    Cheers,
    _
    I get it

    Qt Code:
    1.  
    2. QStandardItem* col0 = new QStandardItem(QString("t").arg(0).arg(0));
    3. QStandardItem* col1 = new QStandardItem(QString("M").arg(0).arg(1));
    4. QStandardItem* col2 = new QStandardItem(QString("H").arg(0).arg(2));
    5. QStandardItem* col3 = new QStandardItem(QString("P1").arg(1).arg(0));
    6. QStandardItem* col4 = new QStandardItem(QString("T1").arg(1).arg(1));
    7. model->setItem(0,0,col0);
    8. model->setItem(0,1,col1);
    9. model->setItem(0,2,col2);
    10. model->setItem(1,0,col3);
    11. model->setItem(1,1,col4);
    12.  
    13. QTableView* coilView = new QTableView(this); // create the tableview
    14. ui->comboBox->setView(coilView); // set it to the comboBox before making changes
    15. coilView->setSelectionMode(QAbstractItemView::SingleSelection);
    16. coilView->setSelectionBehavior(QAbstractItemView::SelectItems);
    17. ui->comboBox->setModel(model); //set the model
    To copy to clipboard, switch view to plain text mode 

    but when i build this code a current item in combobox does not change if i select items in columns 2-5 (1st item of a row becomes a current item).

  4. #4
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: QComboBox drop down list as table

    A QComboBox is for selecting one item from a list. Not many items from many lists. The custom view ability allows you to display a table that, for example, shows the combo box value and meaning in two columns.

    If you want the user to be able to select one option from each column of a model then use multiple QComboBoxes with a shared tabular model and tie each to a different column: QComboBox::setModelColumn()

    If you want to be able to select any single value from among all those in the model then make it a simple list.

    BTW: Your use of QString::arg() without place markers makes no sense at all.

  5. The following user says thank you to ChrisW67 for this useful post:

    ROCKSTAR (6th February 2014)

  6. #5
    Join Date
    Dec 2013
    Posts
    15
    Thanks
    3
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: QComboBox drop down list as table

    Quote Originally Posted by ChrisW67 View Post
    A QComboBox is for selecting one item from a list. Not many items from many lists. The custom view ability allows you to display a table that, for example, shows the combo box value and meaning in two columns.

    If you want the user to be able to select one option from each column of a model then use multiple QComboBoxes with a shared tabular model and tie each to a different column: QComboBox::setModelColumn()

    If you want to be able to select any single value from among all those in the model then make it a simple list.

    BTW: Your use of QString::arg() without place markers makes no sense at all.
    I think i don't get it I've tried this code and had an empty list
    Qt Code:
    1. QTableView* coilView = new QTableView(this); // create the tableview
    2. ui->comboBox->setView(coilView);
    3.  
    4. QStringList itemsList=(QStringList()<<"time"<<"M"<<"H"<<"P1"<<"T1");
    5. ui->comboBox->addItems(itemsList);
    6. ui->comboBox->setCurrentIndex(1);
    7. QStringList itemsList_2=(QStringList()<<"N1OTN"<<"N2OTN"<<"GT"<<"R"<<"DKY");
    8. ui->comboBox_2->addItems(itemsList_2);
    9. ui->comboBox_2->setCurrentIndex(2);
    10.  
    11. ui->comboBox->setModel(model); //set the model
    12. ui->comboBox->setModelColumn(0);
    13. ui->comboBox_2->setModel(model); //set the model
    14. ui->comboBox_2->setModelColumn(1);
    To copy to clipboard, switch view to plain text mode 
    Last edited by ROCKSTAR; 6th February 2014 at 07:06.

  7. #6
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QComboBox drop down list as table

    Quote Originally Posted by ROCKSTAR View Post
    I think i don't get it I've tried this code and had an empty list
    There is no content in "model" so the combobox showing that model is empty as well.

    You don't add any data into the model, where to you expect it to come from?

    Cheers,
    _

  8. The following user says thank you to anda_skoa for this useful post:

    ROCKSTAR (6th February 2014)

  9. #7
    Join Date
    Dec 2013
    Posts
    15
    Thanks
    3
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: QComboBox drop down list as table

    Oh, now i understand you guys, so i could only make 3 comboboxes (for 3 columns model) and user must check each of them to find what he need (cause i need tabular combobox to choose Y-axis title of a plot), and that is sad.
    Last edited by ROCKSTAR; 6th February 2014 at 09:22.

  10. #8
    Join Date
    Dec 2013
    Posts
    15
    Thanks
    3
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: QComboBox drop down list as table

    I've had an idea to connect signals from combobox->view() to function which changes currentText and currentIndex, but it seems that signal does not emit
    Qt Code:
    1. connect (ui->comboBox->view(), SIGNAL(clicked(QModelIndex)), this, SLOT(setComboText(QModelIndex)));
    To copy to clipboard, switch view to plain text mode 
    Qt Code:
    1. void MainWindow::setComboText(const QModelIndex & index) {
    2. ui->comboBox->setCurrentText(comboModel->data(index).toString());
    3. }
    To copy to clipboard, switch view to plain text mode 
    Could you help me, please?

  11. #9
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QComboBox drop down list as table

    Maybe try signal activated?

    Cheers,
    _

  12. #10
    Join Date
    Dec 2013
    Posts
    15
    Thanks
    3
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: QComboBox drop down list as table

    Quote Originally Posted by anda_skoa View Post
    Maybe try signal activated?

    Cheers,
    _
    I tried this one, but it doesn't work too. Full code:
    Qt Code:
    1. comboModel = new QStandardItemModel(5,5);
    2.  
    3. comboModel->setItem(0,0,col0);
    4. comboModel->setItem(0,1,col1);
    5. comboModel->setItem(1,2,col2);
    6.  
    7. coilView = new QTableView(this); // create the tableview
    8. ui->comboBox->setView(coilView); // set it to the comboBox before making changes
    9.  
    10. /* TableView configuration */
    11. coilView->setSelectionMode(QAbstractItemView::SingleSelection);
    12. coilView->setSelectionBehavior(QAbstractItemView::SelectItems);
    13. /********************/
    14.  
    15. ui->comboBox->setModel(comboModel); //set the model
    16.  
    17. coilView->setModel(comboModel);
    18.  
    19. // connect (ui->comboBox, SIGNAL(activated(int)), this, SLOT(setComboText_2(int))); // this one works but it's not what i want
    20.  
    21. connect (ui->comboBox->view(), SIGNAL(activated(QModelIndex)), this, SLOT(setComboText(QModelIndex)));
    22. // connect (coilView, SIGNAL(activated(QModelIndex)), this, SLOT(setComboText(QModelIndex))); // doesn't emit too
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. void MainWindow::setComboText(const QModelIndex & index) {
    2. // ui->comboBox->setCurrentText(comboModel->data(index).toString()); // commented, because i'm trying to test emmiting of signal by simple code below
    3. std::cout << "hello!";
    4. }
    To copy to clipboard, switch view to plain text mode 

Similar Threads

  1. Replies: 6
    Last Post: 29th April 2014, 04:04
  2. Drop on table and widget
    By core_st in forum Newbie
    Replies: 2
    Last Post: 15th November 2010, 21:10
  3. QComboBox relational sql table
    By BrainStorm in forum Qt Programming
    Replies: 0
    Last Post: 16th August 2010, 17:18
  4. QComboBox drop list button events
    By maird in forum Qt Programming
    Replies: 5
    Last Post: 20th October 2007, 19:25
  5. get mysql table into QComboBox
    By eleanor in forum Qt Programming
    Replies: 17
    Last Post: 10th October 2007, 15:35

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.