Results 1 to 7 of 7

Thread: Need to have columns in QComboBox

  1. #1
    Join Date
    Jun 2007
    Posts
    28
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Windows

    Question Need to have columns in QComboBox

    Hi All,
    I have a peculiar requirement. I need to have 2 columns in the QComboBox control.
    I have a scenario where there are 2 strings coming from different places and getting concatenated and shown as 1 single string in the combobox.
    The problem arises when the strings are a mixture of RTL (right to left) and LTR(left to right) language. In this case my strings distorts. for eg.
    String 1 المتحدة String 2: 28 apr 2007
    In combobox it appears as 28, المتحدة apr 2007
    Somebody told me that I can do away with this problem by making 2 columns in the Combobox. But I have no clue as to how to achieve this.

    Thanks,

  2. #2
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: Need to have columns in QComboBox

    Qt Code:
    1. // main.cpp
    2. #include <QtGui>
    3.  
    4. int main(int argc, char* argv[])
    5. {
    6. QApplication app(argc, argv);
    7.  
    8. QStandardItemModel model(5, 3);
    9. for (int i = 0; i < model.rowCount(); ++i)
    10. {
    11. QStandardItem* col0 = new QStandardItem(QString("foo%0,bar%0").arg(i));
    12. QStandardItem* col1 = new QStandardItem(QString("foo%0").arg(i));
    13. QStandardItem* col2 = new QStandardItem(QString("bar%0").arg(i));
    14. model.setItem(i, 0, col0);
    15. model.setItem(i, 1, col1);
    16. model.setItem(i, 2, col2);
    17. }
    18.  
    19. QComboBox comboBox;
    20. comboBox.setModel(&model);
    21.  
    22. QTreeView* treeView = new QTreeView(&comboBox);
    23. comboBox.setView(treeView);
    24. treeView->setColumnHidden(0, true);
    25. treeView->setSelectionBehavior(QAbstractItemView::SelectRows);
    26. treeView->setAllColumnsShowFocus(true);
    27. treeView->setRootIsDecorated(false);
    28. treeView->header()->hide();
    29.  
    30. comboBox.show();
    31. return app.exec();
    32. }
    To copy to clipboard, switch view to plain text mode 
    J-P Nurmi

  3. #3

    Default Re: Need to have columns in QComboBox

    Hi,
    Tried this solution..
    But still does not solve problem of string concatenation because the string being set in combobox is through the statement
    Qt Code:
    1. QStandardItem* col0 = new QStandardItem(QString("foo%0,bar%0").arg(i));
    To copy to clipboard, switch view to plain text mode 

    and this means that finally string appending comes into picture.

    So how do I set 2 different columns on combo box so that strings remain seperate?

  4. #4
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: Need to have columns in QComboBox

    Just write a model which concatenates strings in data() for column 0 from columns 1 and 2 like you want.
    J-P Nurmi

  5. #5

    Default Re: Need to have columns in QComboBox

    Hi,
    Thanks for the reply JPN- but this still involves string contactenation
    and the problem lies there with RTL & LTR strings
    String 1 المتحدة String 2: 28 apr 2007
    If we concatenate it appears as 28, المتحدة apr 2007 -
    I need to avoid string concatenation i.e i need to maintain seperate columns for the strings ..
    Can I do that..

  6. #6
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: Need to have columns in QComboBox

    Columns apply for the popup view only. The combobox itself is capable of showing a single string, thus concatenation.
    J-P Nurmi

  7. #7
    Join Date
    Jun 2008
    Posts
    88
    Thanks
    4
    Thanked 4 Times in 3 Posts
    Qt products
    Qt3 Qt4
    Platforms
    MacOS X Unix/X11
    Wiki edits
    1

    Default Re: Need to have columns in QComboBox

    I have a similar problem. I have a combo box with a tree widget inside of it with 3 columns. (all of which I want displayed)

    The first column has the information I want displayed when the user selects something. The other two columns are just extra information for the user. Basically, I want to treat the entire row as one entity

    So if someone clicks on an item in the view I would like the text from the first column to show up in the combo box (or text from all three columns concatenated or something).

    However, if the user clicks on a tree widget item, it sets the text to whatever is in the cell they clicked on. Which I suppose is expected... though unexpected for the user since they're expecting either the entire row to show up in the combo box or at least the first column (the name column)

Similar Threads

  1. Width QTableView for QCombobox with multiple columns
    By visor_ua in forum Qt Programming
    Replies: 7
    Last Post: 21st June 2011, 10:54
  2. QComboBox drop list button events
    By maird in forum Qt Programming
    Replies: 5
    Last Post: 20th October 2007, 19:25
  3. QComboBox in QTableWidget : display troubles.
    By Nyphel in forum Qt Programming
    Replies: 2
    Last Post: 13th October 2007, 23:29
  4. using QComboBox as an ItemView
    By EricTheFruitbat in forum Qt Programming
    Replies: 3
    Last Post: 24th January 2007, 16:14
  5. QDataWidgetMapper <=> QComboBox best practice
    By saknopper in forum Qt Programming
    Replies: 1
    Last Post: 18th January 2007, 10:50

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.