Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

QListWidget current index/item



  • Hello guys

    I changed ComboBox into QListWidget but I can't resolve this problem.

    Before was:
    Kcals[0]+=ItemKcals[ItemMap[ui->comboBox->currentText()]];

    I tried:
    //double Kcals[]
    Kcals[0]+=ItemKcals[ItemMap[ui->qListTest->currentIndex()];
    Kcals[0]+=ItemKcals[ItemMap[ui->qListTest->currentIndex()];

    //mainwindow.cpp:78: error: no viable overloaded operator[] for type 'QMap<QString, int>'

    Can you help me with how to read the current Index or Item to this variable Kcals? Before I used QMap and that worked, but I dont have any more ideas.


  • Lifetime Qt Champion

    Hi

    QString text;
    if (ui->listWidget->currentItem())
       text = ui->listWidget->currentItem()->text();
    

    Do note that currentItem() might be NULL so you must check it or your app will crash.


  • Lifetime Qt Champion

    Hi

    QString text;
    if (ui->listWidget->currentItem())
       text = ui->listWidget->currentItem()->text();
    

    Do note that currentItem() might be NULL so you must check it or your app will crash.



  • @mrjj said in QListWidget current index/item:

    text = ui->listWidget->currentItem()->text();

    That's works! Thanks man!


Log in to reply