Results 1 to 10 of 10

Thread: QSqlRelationalTableModel and comboBox

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    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.

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,373
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Thanks
    3
    Thanked 5,019 Times in 4,795 Posts
    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.

  3. #3
    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.

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,373
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Thanks
    3
    Thanked 5,019 Times in 4,795 Posts
    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.

  5. #5
    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.

  6. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,373
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Thanks
    3
    Thanked 5,019 Times in 4,795 Posts
    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, 13:54
  2. [Qt 4.3.1]A problem with combobox style
    By Tamara in forum Qt Programming
    Replies: 3
    Last Post: 19th September 2007, 11:49
  3. Replies: 8
    Last Post: 15th May 2007, 10:21
  4. Combobox Signals
    By b1 in forum Qt Programming
    Replies: 3
    Last Post: 1st August 2006, 11:21
  5. Filling combobox from database
    By Philip_Anselmo in forum Qt Programming
    Replies: 3
    Last Post: 11th May 2006, 18: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
  •  
Qt is a trademark of The Qt Company.