Results 1 to 10 of 10

Thread: QSqlRelationalTableModel and comboBox

  1. #1
    Join Date
    Oct 2007
    Posts
    9
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default QSqlRelationalTableModel and comboBox

    I am trying to understand the QSqlRelationalTableModel and playing with an example coming with qt4 examples/sql/relationaltablemodel
    I want to add some properties to this example.
    There are two comboboxes one of is country and the other is city. I want to see cities only in cityComboBox belong to chosed country in countryComboBox.
    How can i do and if it possible I need an example.
    Thanks
    Last edited by musti; 11th October 2007 at 09:21.

  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: QSqlRelationalTableModel and comboBox

    Your question is too general, looks like you want to add filtering, but I'm not sure. Try writing some code and post it here and we'll try to help you correct it if it doesn't work.

  3. #3
    Join Date
    Oct 2007
    Posts
    9
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QSqlRelationalTableModel and comboBox

    I filled comboBoxCountry and comboBoxCity with another QSqlTableModel not with my main QSqlRelationalTableModel and than mapped
    mapper->addMapping(comboBoxCity, cityIdx);
    mapper->addMapping(comboBoxCountry, countryIdx);
    I cannot filter the my main model. Filtering works or whole Sql query.
    I added a signal to comboBoxCountry that it filters the city model. Every things ok.
    If i submit changes to sql table, main country and city ids are updating to sql table form comboboxes. Because city and country indexes not changed in main model.
    I must submit the mapper contents not model.
    Any ideas?

  4. #4
    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: QSqlRelationalTableModel and comboBox

    Could we see something compilable?

  5. #5
    Join Date
    Oct 2007
    Posts
    9
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Lightbulb Re: QSqlRelationalTableModel and comboBox

    connect(comboBox_country, SIGNAL(activated(int)), this, SLOT(countryChanged(int)));
    ...
    ...
    ..
    myModel1 = new QSqlTableModel(this);
    myModel1->setTable("city");
    myModel1->select();
    comboBox_city->setModel(myModel1);
    comboBox_city->setModelColumn(1);

    myModel2 = new QSqlTableModel(this);
    myModel2->setTable("country");
    myModel2->select();
    comboBox_country->setModel(myModel2);
    comboBox_country->setModelColumn(1);
    ...
    ..

    void myQtApp::countryChanged(int rownum)
    {
    QVariant country_id = myModel2->data(myModel2->index(rownum,0) );
    myModel1->setFilter(tr("cid=%1").arg(country_id.toInt())) ;
    }

    modelling and mapping section

    model= new QSqlRelationalTableModel(tableView);
    model->setTable("person");
    model->setEditStrategy(QSqlTableModel::OnManualSubmit) ;
    cityIdx = model->fieldIndex("city");
    countryIdx = model->fieldIndex("country");
    ...
    ...
    model->setRelation(cityIdx, QSqlRelation("city", "id", "name"));
    model->setRelation(countryIdx, QSqlRelation("country", "id", "name"));
    mapper->addMapping(comboBox_city, cityIdx);
    mapper->addMapping(comboBox_country, countryIdx);

    ...
    connect(tableView->selectionModel(), SIGNAL(currentRowChanged(QModelIndex,QModelIndex)) ,
    mapper, SLOT(setCurrentModelIndex(QModelIndex)));
    ...

    I think i submit mapper changes to the model.

  6. #6
    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: QSqlRelationalTableModel and comboBox

    But what is mapper? QDataWidgetMapper? What do you need it for? Please provide a complete compilable project so that I can run it and see what your problem is.

  7. #7
    Join Date
    Oct 2007
    Posts
    9
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QSqlRelationalTableModel and comboBox

    The example is in the following adress.
    http://doc.trolltech.com/4.3/sql-rel...ablemodel.html
    If i select Germany from Country combobox, i want to see only German cities in city combobox.

  8. #8
    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: QSqlRelationalTableModel and comboBox

    I know where the example is, but you paste some code which is not part of the example. As I said - if you want to see only German cities in the combobox, you have to do some filtering. You can do that by providing your own delegate that will filter out items, that don't have a specific country relation. But it's a bit complex change, so you can't base it on the above mentioned example.

  9. #9
    Join Date
    Oct 2007
    Posts
    9
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QSqlRelationalTableModel and comboBox

    Forget my work. Because i must paste whole work to the form. My question is simple. If i change country, i want to see cities only that belong my chosen country in cityComboBox not other cities. Naturally i added country id to city table. I am asking only that if there is a simplest way to do this. Otherwise i do it manually.

  10. #10
    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: QSqlRelationalTableModel and comboBox

    As I said, you can implement your own delegate (as a subclass to QSqlRelationalDelegate) that will do the filtering inside QAbstractItemDelegate::setEditorData().

Similar Threads

  1. combobox and item colors
    By zorro68 in forum Qt Programming
    Replies: 16
    Last Post: 23rd September 2007, 12:54
  2. [Qt 4.3.1]A problem with combobox style
    By Tamara in forum Qt Programming
    Replies: 3
    Last Post: 19th September 2007, 10:49
  3. Replies: 8
    Last Post: 15th May 2007, 09:21
  4. Combobox Signals
    By b1 in forum Qt Programming
    Replies: 3
    Last Post: 1st August 2006, 10:21
  5. Filling combobox from database
    By Philip_Anselmo in forum Qt Programming
    Replies: 3
    Last Post: 11th May 2006, 17:53

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.