Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. comboBox with Qt::Alignement as itemData
Qt 6.11 is out! See what's new in the release blog

comboBox with Qt::Alignement as itemData

Scheduled Pinned Locked Moved General and Desktop
4 Posts 2 Posters 2.0k Views 2 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • H Offline
    H Offline
    HappyCoder
    wrote on last edited by
    #1

    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

    1 Reply Last reply
    0
    • K Offline
      K Offline
      koahnig
      wrote on last edited by
      #2

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

      Vote the answer(s) that helped you to solve your issue(s)

      1 Reply Last reply
      0
      • H Offline
        H Offline
        HappyCoder
        wrote on last edited by HappyCoder
        #3

        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.

        1 Reply Last reply
        0
        • H Offline
          H Offline
          HappyCoder
          wrote on last edited by
          #4

          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.

          1 Reply Last reply
          0

          • Login

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • Users
          • Groups
          • Search
          • Get Qt Extensions
          • Unsolved