Results 1 to 3 of 3

Thread: Automatically add items to a QComboBox (Qt4)

  1. #1
    Join Date
    May 2006
    Posts
    70
    Thanks
    12
    Thanked 4 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Automatically add items to a QComboBox (Qt4)

    Hello everyone,

    I have a QComboBox that is connected to a QSqlTableModel (via setModel). The combobox displays the items from the database just fine. I have the editable and autocomplete properties of the combobox set to true. I want the user to be able to either: 1) select an existing item from the list, 2) start typing and let the autocomplete select an existing item, and 3) add a new item if it doesn't already exist.

    I've got 1) and 2) working but the adding of new items doesn't seem to work well. I know that the QComboBox is not a proper 'View' in Qt4, so maybe I'm not doing things right.

    Thanks in advance for the help.

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Automatically add items to a QComboBox (Qt4)

    Can you show us some code?

  3. #3
    Join Date
    May 2006
    Posts
    70
    Thanks
    12
    Thanked 4 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Automatically add items to a QComboBox (Qt4)

    Actually I may have figured it out (after much debugging) although I have no idea if i'm doing it the "right" way. Is there a better way to do this?

    Here is what I have done:

    In the constructor to my window i create my model and assign it to my combobox. I then connect the dataChanged signal from the model, to a slot that submits the data to the database.

    In QtDesigner I have these properties set on the combobox:
    editable = true
    autoCompletion = true
    insertPolicy = QComboBox::InsertAtBottom
    modelColumn = true

    Qt Code:
    1. //Constructor
    2. MyWindow::MyWindow(QWidget *parent) : QWidget(parent)
    3. {
    4. setupUi(this); //i'm using Qt Designer for my GUI design.
    5.  
    6. myModel = new QSqlTableModel(this);
    7. myModel->setTable("mydatabasetable");
    8. myModel->setEditStrategy(QSqlTableModel::OnManualSubmit);
    9. myModel->select();
    10.  
    11. myComboBox->setModel(myModel);
    12. myComboBox->setModelColumn(1); //collumn 0 is an auto ID, column 1 is text, which i want displayed
    13.  
    14. connect(myModel, SIGNAL(dataChanged(const QModelIndex &, const QModelIndex &)), this, SLOT(myModelDataChanged(const QModelIndex &, const QModelIndex &)));
    15. }
    16.  
    17. //DataChanged Slot
    18. void MyWindow::myModelDataChanged(const QModelIndex &, const QModelIndex &)
    19. {
    20. myModel->submitAll();
    21. }
    To copy to clipboard, switch view to plain text mode 

    I've tried different edit strategies and it seems that only OnManualSubmit works.

Similar Threads

  1. Replies: 1
    Last Post: 15th January 2009, 10:34
  2. QComboBox not showing all items
    By musikit in forum Qt Programming
    Replies: 3
    Last Post: 10th April 2008, 16:01
  3. Occurance of Duplicate items in QComboBox
    By merry in forum Qt Programming
    Replies: 8
    Last Post: 12th September 2007, 15:05
  4. QcomboBox items
    By therealjag in forum Newbie
    Replies: 5
    Last Post: 27th March 2006, 08:21
  5. [QT3] QComboBox: Disable adding items on Enter-keypress
    By BrainB0ne in forum Qt Programming
    Replies: 7
    Last Post: 14th January 2006, 19:43

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.