Issue of QComboBox:currentIndexChanged() and Model



  • Hello,

    QComboBox doesn't show currentIndex correctly after calling any method from its model; How can I fix this issue?

    [code]Requests::Requests(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::Requests)
    {
    ui->setupUi(this);
    ui->comboBox_category->setModel(model_labCategoryList());
    ui->comboBox_category->setModelColumn(1);
    }

    QSqlQueryModel *Requests::model_labCategoryList() const
    {
    _model_labCategoryList->setQuery(query_selectAllCategories);
    return _model_labCategoryList;
    }

    void Requests::on_comboBox_category_currentIndexChanged(int index)
    {
    // current index of comboBox_category disappear here
    // When I comment the line below the GUI works fine
    int categoryId = _db->model_labCategoryList()->record(index).value("id").toInt();
    ui->spinBox->setValue(categoryId);
    }[/code]

    NOTE: on_comboBox_category_currentIndexChanged() works fine but the GUI doesn't show correctly!



  • When I added this debug line:
    [code]void Requests::on_comboBox_category_currentIndexChanged(int index)
    {
    // current index of comboBox_category disappear here
    // When I comment the line below the GUI works fine
    int categoryId = _db->model_labCategoryList()->record(index).value("id").toInt();
    ui->spinBox->setValue(categoryId);
    qDebug() << "categoryId" << categoryId;
    }[/code]

    I got:
    [code]categoryId 0
    categoryId 1[/code]

    Really strange!!!



  • I tried to use the following although the debug result is OK but the GUI still not working fine!

    [code] void Requests::on_comboBox_category_currentIndexChanged(int index)
    {
    if (index > -1) {
    // current index of comboBox_category disappear here
    // When I comment the line below the GUI works fine
    int categoryId = _db->model_labCategoryList()->record(index).value("id").toInt();
    ui->spinBox->setValue(categoryId);
    qDebug() << "categoryId" << categoryId;
    }
    }
    [/code]

    Debug:
    [code]categoryId 1[/code]


Log in to reply
 

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