1

i'm making a simple calculator using Qt with QT Creator I want to convert a value from QCombobox (that conatain the operations :'+' , '-', '*','/') to int so i have used this :

// operation is the name of my QComboBox :)

QVariant i = ui -> operation -> itemData(ui -> operation -> currentIndex()); 
int val = i.toInt();

When trying to print the value of i to test it i get :

printf("valeur %d \n",i);

Output

valeur 1219552

valeur 1219552

valeur 1219552

valeur 1219552

valeur 1219552

The conversion is give me the same value that it's not corresponding to the index of the QComboBox whenver i choose any of the operations . However it make the addition operation successfully !!!
Calculator
This is the hole file to that demonstrate what i'm trying to accomplish :

#include "dialog.h"
#include "ui_dialog.h"

Dialog::Dialog(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::Dialog)
{
    ui->setupUi(this); // lance la construction de la fenêtre.
    connect(ui->boutonEgale, SIGNAL(clicked()), this,SLOT(calculerOperation()));

}

Dialog::~Dialog()
{
    delete ui;
}


void Dialog::calculerOperation()
{
    QVariant i =   ui->operation->itemData(ui->operation->currentIndex());  
    int val = i.toInt();

    int rst = 0;
    switch(val)
    {
    case 0:  // +
    rst = ui->nb1->value() + ui->nb2->value();
    ui->result->setText(QString::number(rst));  
    break;
    case 1:  // -
    rst = ui->nb1->value() - ui->nb2->value();
    ui->result->setText(QString::number(rst));  
    break;
    case 2: // *
    rst = ui->nb1->value() * ui->nb2->value();
    ui->result->setText(QString::number(rst));   
    break;
    case 3: // /
    rst = ui->nb1->value() / ui->nb2->value();
    ui->result->setText(QString::number(rst));   
    break;
    default:
    rst = ui->nb1->value() + ui->nb2->value();
    ui->result->setText(QString::number(rst));
    }
}

I have used the graphical interface to put the value for the comboBox comboBox  Values

Any suggestions?

The Beast
  • 1,629
  • 2
  • 29
  • 42
  • You know you have to set the itemData before using it, don't you? Then where how and what did you set it to? – Mehrdad Jan 23 '16 at 14:27
  • @MehrdadMomeny i have used the graphical interface to do it , i will included to my post :) – The Beast Jan 23 '16 at 14:31

2 Answers2

3

You meant to write:

int val = ui->operation->currentIndex();

This gives the selected combo-box index (0 is the first, "+", 1 the second, "-" and so on).

itemData is relevant only if you attached data to the item using setItemData.

jpo38
  • 20,821
  • 10
  • 70
  • 151
1

You seem to be confusing two values that a QComboBox can contain: the text (what your'e editing in the Qt Creator dialog screenshot) and the actual useful payload, stored in a QVariant QCombobox::setItemData(int, QVariant, int) http://doc.qt.io/qt-4.8/qcombobox.html#setItemData. If you want to save and later retrieve an int alongside every combobox entry, use the latter function and teh corresponding QComboBox::itemData(int, int) for retrieval.

Strictly speaking, there is a full-blown QStandardItemModel inside a QComboBox used to store the data. To quote the docs:

QComboBox uses the model/view framework for its popup list and to store its items. By default a QStandardItemModel stores the items and a QListView subclass displays the popuplist. You can access the model and view directly (with model() and view()), but QComboBox also provides functions to set and get item data (e.g., setItemData() and itemText()). You can also set a new model and view (with setModel() and setView()). For the text and icon in the combobox label, the data in the model that has the Qt::DisplayRole and Qt::DecorationRole is used.

iksemyonov
  • 4,106
  • 1
  • 22
  • 42