Variable can't get a value



  • So, I have this:

    double A, B, C;
    

    In my Window.h file and also some QComboBoxes and some QDoubleSpinBoxes like this:

    QDoubleSpinBox *a;
    QDoubleSpinBox *b;
    QDoubleSpinBox *c; 
    QComboBox *o1;
    QComboBox *o2;
    QComboBox *o3;
    

    Then I use them like this:

    if(o1->currentData(o1->currentIndex()) == 0)
        {
            A = a->value();
        }
        else if(o1->currentData(o1->currentIndex()) == 1)
        {
            A = a->value();
            A *= -1;
        }
    
        if(o2->currentData(o2->currentIndex()) == 0)
        {
            B = b->value();
        }
        else if(o2->currentData(o2->currentIndex()) == 1)
        {
            B = b->value();
            B *= -1;
        }
    
        if(o3->currentData(o3->currentIndex()) == 0)
        {
            C = c->value();
        }
        else if(o3->currentData(o3->currentIndex()) == 1)
        {
            C = c->value();
            C *= -1;
        }
    

    (each QComboBox has 2 items, "+" and "-") but the A, B, C are always equal to 0, and I cannot figure out why.

    Could you help me? Thanks in advance.



  • @Gluon666
    Definition of function currentData is:

    QVariant currentData(int role = Qt::UserRole) const
    

    So it take as an argument role and not index.
    May be you should use

    if(o1->currentData() == 0)
    

    or

    if(o1->currentIndex() == 0)
    

    instead both at the same time.



  • I tried but still, A, B and C are equal to 0:

    if(o1->currentIndex() == 0)
        {
            A = a->value();
        }
        else if(o1->currentIndex() == 1)
        {
            A = a->value() * -1;
        }
    

    I also tried with:

    if(o1->itemData(o1->currentIndex()) == 0)
        {
            A = a->value();
        }
        else if(o1->itemData(o1->currentIndex()) == 1)
        {
            A = a->value() * -1;
        }
    

    But yeah, no better results...

    Mayhaps, I thought that it could be the slot that I use that is not working properly:

    delta = ((B * B) - (4 * (A * C)));
    
        if(delta > 0)
        {
            S1 = ((-B) + sqrt(delta) / (2 * A));
            S2 = ((-B) - sqrt(delta) / (2 * A));
    
            msg += "S = { ";
            msg += QString::number(S1);
            msg += " ; ";
            msg += QString::number(S2);
            msg += " }";
        }
        else if(delta == 0)
        {
            S = (-B / (2 * A));
    
            msg += "S = { ";
            msg += QString::number(S);
            msg += " }";
        }
        else if(delta < 0)
        {
            msg = "Impossible";
        }
    
        QObject::connect(calculate, SIGNAL(clicked(bool)), this, SLOT(displayMsg()));
    }
    
    void Window::displayMsg()
    {
        output->setText(msg);
    }
    

    This is the actual code (you probably recognized an equation solver) and mybe its the msg that is corrupted or something. No errors while debbugging, though



  • @Gluon666
    How do you know that A, B and C are equal to 0? If they are, then your program will give error when you click on "calculate" - "Division by zero".
    Could you put some debug statements in your code?
    Like this:

    qDebug() << "A: " << A << "\tB: " << B << "\tC: " << C;
    delta = ((B * B) - (4 * (A * C)));
    

Log in to reply
 

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