[SOLVED] [QComboBox and QSqlRelationalDelegate] How to show conditional content in a QComboBox



  • Hello Qt Devs!

    For my GUI to a PostgreSQL DB, I want to show data in combo boxes. However, some of my data has something alike a tree-structure. In short, I want the data in a second ComboBox to be affected by the data in a first ComboBox.

    It seems to be doable with a relational delegate, but it doesn't quite work.

    @// Populate the group ComboBox
    groupModel = new QSqlRelationalTableModel(this, QSqlDatabase::database("******"));
    groupModel->setTable(""TB_Groupe"");
    groupModel->setSort(groupModel->fieldIndex("GR_Nom"), Qt::AscendingOrder);
    groupModel->select();

    ui->MW_groupComboBox->setModel(groupModel);
    ui->MW_groupComboBox->setModelColumn(groupModel->fieldIndex("GR_Nom"));
    
    catModel = new QSqlRelationalTableModel(this, QSqlDatabase::database("*****"));
    catModel->setTable("\"TB_Categorie\"");
    catModel->setSort(catModel->fieldIndex("CAT_Nom"), Qt::AscendingOrder);
    catModel->setRelation(1, QSqlRelation("\"TB_Groupe\"","IndexCategorie", "GR_Nom"));
    catModel->select();
    
    ui->MW_catComboBox->setModel(catModel);
    ui->MW_catComboBox->setModelColumn(catModel->fieldIndex("CAT_Nom"));@
    

    Any ideas?



  • This does not seem to work, either.

    @// Populate the group ComboBox
    groupModel = new QSqlRelationalTableModel(this, QSqlDatabase::database("*****"));
    groupModel->setTable(""TB_Groupe"");
    groupModel->setSort(groupModel->fieldIndex("GR_Nom"), Qt::AscendingOrder);
    groupModel->select();

    ui->MW_groupComboBox->setModel(groupModel);
    ui->MW_groupComboBox->setModelColumn(groupModel->fieldIndex("GR_Nom"));
    
    // Populate category ComboBox according to Group ComboBox
    catModel = new QSqlRelationalTableModel(this, QSqlDatabase::database("*******"));
    catModel->setTable("\"TB_Categorie\"");
    catModel->setSort(catModel->fieldIndex("CAT_Nom"), Qt::AscendingOrder);
    catModel->setFilter("CAT_IndexGroupe =" % ui->MW_groupComboBox->currentIndex());
    catModel->select();
    
    ui->MW_catComboBox->setModel(catModel);
    ui->MW_catComboBox->setModelColumn(catModel->fieldIndex("CAT_Nom"));@


  • Found easy solution by using signals and slots. This system rocks!

    @// Populate the group ComboBox
    groupModel = new QSqlRelationalTableModel(this, QSqlDatabase::database("******"));
    groupModel->setTable(""TB_Groupe"");
    groupModel->setSort(groupModel->fieldIndex("GR_Nom"), Qt::AscendingOrder);
    groupModel->select();

    ui->MW_groupComboBox->setModel(groupModel);
    ui->MW_groupComboBox->setModelColumn(groupModel->fieldIndex("GR_Nom"));
    
    // Populate category ComboBox according to Group ComboBox
    catModel = new QSqlRelationalTableModel(this, QSqlDatabase::database("*******"));
    catModel->setTable("\"TB_Categorie\"");
    catModel->setFilter("\"CAT_IndexGroupe\" = " % QString::number(ui->MW_groupComboBox->currentIndex()+1));
    catModel->setSort(catModel->fieldIndex("CAT_Nom"), Qt::AscendingOrder);
    catModel->select();
    
    ui->MW_catComboBox->setModel(catModel);
    ui->MW_catComboBox->setModelColumn(catModel->fieldIndex("CAT_Nom"));
    
    connect(ui->MW_groupComboBox,
            SIGNAL(currentIndexChanged(int)),
            this,
            SLOT(RefreshCategory(int)));
    

    }

    void MainWindow::RefreshCategory(int choice)
    {
    catModel->setFilter(""CAT_IndexGroupe" = " % QString::number(choice+1));
    catModel->select();

    ui->MW_catComboBox->setModel(catModel);
    ui->MW_catComboBox->setModelColumn(catModel->fieldIndex("CAT_Nom"));
    

    }@


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.