comboBox with Qt::Alignement as itemData



  • Hi,

    i have a comboBox filled like this:

    ui->comboBoxPos->addItem( "Top Left", (int)(Qt::AlignTop | Qt::AlignLeft) );
    ui->comboBoxPos->addItem( "Top Centered", (int)(Qt::AlignTop | Qt::AlignHCenter) );
    ui->comboBoxPos->addItem( "Top Right", (int)(Qt::AlignTop | Qt::AlignRight) );
    

    or this

    ui->comboBoxPos->addItem( "Top Left", 33 );
    ui->comboBoxPos->addItem( "Top Centered", 36 );
    ui->comboBoxPos->addItem( "Top Right", 34 );
    

    With

    void MainWindow::on_comboBoxPos_currentIndexChanged(int index)
    {
    qDebug() << Q_FUNC_INFO << ui->comboBoxPos->itemData(index).toInt();
    }

    I never get back the itemData? What i'm doing wrong?
    Thx


  • Moderators

    Do you get the proper text (e.g. "Top Left") back?



  • yes, if i use the signal currentIndexChanged(QString). But i thought that for the first index (int)(Qt::AlignTop | Qt::AlignLeft) is the
    data to that item and is stored as QVariant. I think i will make a switch case on the index, that is working.



  • i found a solution:

    fill comBox with QStringList _PlotAlignMents and QList<qint32> _PlotAlignmentsData:

    _PlotAlignments << tr("Top Left")       << tr("Top Centered")       << tr("Top Right")
                    << tr("Centered Left")  << tr("Centered Centered")  << tr("Centered Right")
                    << tr("Bottom Left")    << tr("Bottom Centered")    << tr("Bottom Right");
    ui->comboBoxAlignment->addItems( _PlotAlignments );
    _PlotAlignmentsData << (int)(Qt::AlignTop | Qt::AlignLeft)
                        << (int)(Qt::AlignTop | Qt::AlignHCenter)
                        << (int)(Qt::AlignTop | Qt::AlignRight)
                        << (int)(Qt::AlignVCenter | Qt::AlignLeft)
                        << (int)(Qt::AlignVCenter | Qt::AlignHCenter)
                        << (int)(Qt::AlignVCenter | Qt::AlignRight)
                        << (int)(Qt::AlignBottom | Qt::AlignLeft)
                        << (int)(Qt::AlignBottom | Qt::AlignHCenter)
                        << (int)(Qt::AlignBottom | Qt::AlignRight);
    for(int i=0; i<_PlotAlignmentsData.size(); i++)
        ui->comboBoxAlignment->setItemData(i,_PlotAlignmentsData[i]);
    

    now i can get the data:

    void ui_plot_settings::on_comboBoxAlignment_currentIndexChanged(int index)
    {
    qDebug() << Q_FUNC_INFO << ui->comboBoxAlignment->itemData(index).toInt();
    }

    This works now.


Log in to reply
 

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