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. Variable can't get a value
QtWS25 Last Chance

Variable can't get a value

Scheduled Pinned Locked Moved Unsolved General and Desktop
c++
4 Posts 2 Posters 993 Views
  • 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.
  • G Offline
    G Offline
    Gluon666
    wrote on last edited by Gluon666
    #1

    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.

    S 1 Reply Last reply
    0
    • G Gluon666

      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.

      S Offline
      S Offline
      Stoyan
      wrote on last edited by
      #2

      @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.

      1 Reply Last reply
      1
      • G Offline
        G Offline
        Gluon666
        wrote on last edited by Gluon666
        #3

        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

        S 1 Reply Last reply
        0
        • G Gluon666

          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

          S Offline
          S Offline
          Stoyan
          wrote on last edited by Stoyan
          #4

          @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)));
          
          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