Problem with checkbox ,spinbox and qcustomplot



  • Hello,
    I have a qcustomplot with four graphs plus about 50 values .I want that only up to four graphs are shown at a time. So i gave each value and according checkbox a number,this is stored in the spinbox.So when I check the checkbox, the spinbox will get and hold the according number.When I check the checkbox again the spin box is set to zero and the according graph is deleted.That all works if I check only one checkbox.When I have checked four and check a fifth one or when I want to delete one graph my programm aborts.
    Solution or other ideas to solve this problem are highly appreciated.
    If somebody wants to help and needs the complete code, i will send it , it is free to everybody.
    Here is part of the code:
    if (m_ui->cB_rpm->isChecked() )
    {

             m_ui->curves->graph(0)->addData(key,rpm);
             m_ui->curves->graph(0)->setName("rpm");
    
             }
    

    and the slot
    //1*****************************************************************************
    void MainWindow::on_cB_rpm_stateChanged(int arg1)
    {

        if ((m_ui->sB1->value() == 0)&& (m_ui->cB_rpm->isChecked()))
        {
             m_ui->sB1->setValue(1);
    
    
     qDebug()<< m_ui->sB1->value() ;
     qDebug()<< m_ui->cB_rpm->isChecked();
    
        }
      else if ((m_ui->sB2->value() == 0)&&( m_ui->cB_rpm->isChecked()))
            {
               m_ui->sB2->setValue(1);
    
         qDebug()<< m_ui->sB2->value() ;
         qDebug()<< m_ui->cB_rpm->isChecked();
    
             }
    
    else if (( m_ui->sB3->value() == 0)&&( m_ui->cB_rpm->isChecked()))
        {
              m_ui->sB3->setValue(1);
                  qDebug()<< m_ui->sB3->value() ;
                  qDebug()<< m_ui->cB_rpm->isChecked();
    
         }
    

    else if (( m_ui->sB4->value()== 0)&& ( m_ui->cB_rpm->isChecked()))
    {
    m_ui->sB4->setValue(1);

         qDebug()<< m_ui->sB4->value() ;
         qDebug()<< m_ui->cB_rpm->isChecked();
    
         }
    // ausschalten
    

    else if (m_ui->sB1->value()== 1)
    {
    m_ui->sB1->setValue(0);
    m_ui->curves->removeGraph(0);
    m_ui->curves->replot();
    qDebug()<<m_ui->cB_rpm->isChecked();
    qDebug()<<m_ui->sB1->value();
    }
    else if ( m_ui->sB2->value()== 1)
    {
    m_ui->sB2->setValue(0);
    m_ui->curves->removeGraph(1);
    m_ui->curves->replot();
    qDebug()<<m_ui->cB_rpm->isChecked();
    qDebug()<< m_ui->sB2->value();
    }
    else if (m_ui->sB3->value()== 1)
    {

          m_ui->sB3->setValue(0);
          m_ui->curves->removeGraph(2);
          m_ui->curves->replot();
        qDebug()<<m_ui->cB_rpm->isChecked();
        qDebug()<< m_ui->sB3->value();
        }
    

    else if (m_ui->sB4->value() == 1)
    {

          m_ui->sB4->setValue(0);
          m_ui->curves->removeGraph(3);
          m_ui->curves->replot();
          qDebug()<<m_ui->cB_rpm->isChecked();
          qDebug()<< m_ui->sB4->value();
        }
    

    }
    //2*****************************************************************************
    void MainWindow::on_cB_WaterT_stateChanged(int arg1)
    {

        if ((m_ui->sB1->value() == 0)&& (m_ui->cB_WaterT->isChecked()))
        {
             m_ui->sB1->setValue(2);
    
             qDebug()<< m_ui->sB1->value() ;
             qDebug()<< m_ui->cB_WaterT->isChecked();
    
        }
      else if ((m_ui->sB2->value() == 0)&&( m_ui->cB_WaterT->isChecked()))
            {
               m_ui->sB2->setValue(2);
    
             qDebug()<< m_ui->sB2->value() ;
             qDebug()<< m_ui->cB_WaterT->isChecked();
    
             }
    
    else if (( m_ui->sB3->value() == 0)&&( m_ui->cB_WaterT->isChecked()))
             {
              m_ui->sB3->setValue(2);
              qDebug()<< m_ui->sB3->value() ;
              qDebug()<< m_ui->cB_WaterT->isChecked();
    
             }
    

    else if (( m_ui->sB4->value()== 0)&& ( m_ui->cB_WaterT->isChecked()))
    {
    m_ui->sB4->setValue(2);

                 qDebug()<< m_ui->sB4->value() ;
                 qDebug()<< m_ui->cB_WaterT->isChecked();
    
                }
    // ausschalten
    else if (m_ui->sB1->value()== 2)
            {
    
             m_ui->sB1->setValue(0);
             m_ui->curves->removeGraph(0);
             m_ui->curves->replot();
            qDebug()<<m_ui->cB_WaterT->isChecked();
            }
    

    else if ( m_ui->sB2->value()== 2)
    {

          m_ui->sB2->setValue(0);
          m_ui-> curves->removeGraph(1);
          m_ui->curves->replot();
    

    qDebug()<<m_ui->cB_WaterT->isChecked();
    }
    else if(m_ui->sB3->value()== 2)
    {

          m_ui->sB3->setValue(0);
          m_ui-> curves->removeGraph(2);
          m_ui->curves->replot();
    

    qDebug()<<m_ui->cB_WaterT->isChecked();
    }
    else if (m_ui->sB4->value() == 2)
    {

          m_ui->sB4->setValue(0);
          m_ui-> curves->removeGraph(3);
          m_ui->curves->replot();
    

    qDebug()<<m_ui->cB_WaterT->isChecked();
    qDebug()<< m_ui->sB4->value();
    }
    }


  • Lifetime Qt Champion

    Hi and welcome to devnet,

    Usually when you want to have a maximum number of things doable, you disable everything else when you reach that number.

    However, you seem to have a pretty complex code base for such a task.

    Can you maybe give more details about the goals of your application ?



  • Hi SGaist,
    I see you live in switzerland so I write in german.
    Das Programm ist ein Testprogramm für austin mini Motorsteuerung ursprünglich von Colin Bourassa unter GPL geschrieben. Ich habe es um einige Anzeigen erweitert und möchte die Ausgabewerte auch in einem Plotfenster als Kurve ausgeben.Dazu zu jedem Wert eine checkbox.!Einschränkungen habe ich ja dadurch gemacht, das die Spinbox auf 0 stehen muß,ich könnte noch einen else Zweig hinzufügen falls keines der Kriterien erfüllt ist.Mit diesem Fehler bricht das Programm ab, wo muß ich suchen.:QCPGraph* QCustomPlot::graph(int) const index out of bounds: 3
    Gerne schicke ich auch den kompletten source code.Da meine c Kenntnise nur grundlegender Natur sind komme ich nicht weiter.
    Gruß
    Leopold


  • Qt Champions 2016

    @Leopold said in Problem with checkbox ,spinbox and qcustomplot:

    removeGraph

    Hi, did you check if remaining graphs keep their index ?
    say you insert 4
    so u have 0,1,2,3
    now we remove 2
    will index be
    0,1,NULL,3
    or will be contracted so we have
    0,1,2
    and hence the old 3 index is now invalid ?

    if Yes it contracts, then my guess is on that as you use absolute indexes.



  • Hi mrjj,
    you are right, the graph is completly removed, but I see no possibility to clear only one graph. Will have a look in qcustomplot.h how clearGraphs() is build.
    But i have another problem.I couldn't do it with every checkbox a own number so I changed the code and wanted to work with a variable which holds the number of the graph. e.g if spinbox one then m=0 graph 0
    if spinbox two then m=1 graph 1
    if spinbox three then m=2 graph 2
    if spinbox four then m=3 graph 3
    now when I check four boxes the first 3 boxes work on the same value of the last variable.
    Here is part of the code:
    if (m_ui->cB_ambT->isChecked() )
    {
    m_ui->curves->graph(m)->addData(key,ambT);
    m_ui->curves->graph(m)->setName("AmbientTemp");

         }
    

    if (m_ui->cB_intakeA->isChecked() )
    {
    m_ui->curves->graph(m)->addData(key,intakeA);
    m_ui->curves->graph(m)->setName("intakeAirT");

      }
    

    if (m_ui->cB_FuelT->isChecked() )
    {
    m_ui->curves->graph(m)->addData(key,FuelT);
    m_ui->curves->graph(m)->setName("fuel temp");

           }
    

    //1*****************************************************************************
    void MainWindow::on_cB_rpm_stateChanged(int arg1)
    {

        if ((m_ui->sB1->value() == 0)&& (m_ui->cB_rpm->isChecked()))
        {
             m_ui->sB1->setValue(1);
            m=0;
    
     qDebug()<< m_ui->sB1->value() ;
     qDebug()<< m_ui->cB_rpm->isChecked();
    
        }
      else if ((m_ui->sB2->value() == 0)&&( m_ui->cB_rpm->isChecked()))
            {
               m_ui->sB2->setValue(1);
               m=1;
         qDebug()<< m_ui->sB2->value() ;
         qDebug()<< m_ui->cB_rpm->isChecked();
    
             }
    
    else if (( m_ui->sB3->value() == 0)&&( m_ui->cB_rpm->isChecked()))
        {
              m_ui->sB3->setValue(1);
              m=2;
                  qDebug()<< m_ui->sB3->value() ;
                  qDebug()<< m_ui->cB_rpm->isChecked();
    
         }
    

    else if (( m_ui->sB4->value()== 0)&& ( m_ui->cB_rpm->isChecked()))
    {
    m_ui->sB4->setValue(1);

         qDebug()<< m_ui->sB4->value() ;
         qDebug()<< m_ui->cB_rpm->isChecked();
          m=3;
         }
    // ausschalten
    

    else if (m_ui->sB1->value()== 1)
    {

             m_ui->sB1->setValue(0);
             m_ui->curves->clearGraphs();
             m_ui->curves->replot();
            qDebug()<<m_ui->cB_rpm->isChecked();
            qDebug()<<m_ui->sB1->value();
         }
    

    else if ( m_ui->sB2->value()== 1)
    {

          m_ui->sB2->setValue(0);
          m_ui->curves->removeGraph(1);
          m_ui->curves->replot();
            qDebug()<<m_ui->cB_rpm->isChecked();
            qDebug()<< m_ui->sB2->value();
        }
    

    else if (m_ui->sB3->value()== 1)
    {

          m_ui->sB3->setValue(0);
          m_ui->curves->removeGraph(2);
          m_ui->curves->replot();
        qDebug()<<m_ui->cB_rpm->isChecked();
        qDebug()<< m_ui->sB3->value();
        }
    

    else if (m_ui->sB4->value() == 1)
    {

          m_ui->sB4->setValue(0);
          m_ui->curves->removeGraph(3);
          m_ui->curves->replot();
          qDebug()<<m_ui->cB_rpm->isChecked();
          qDebug()<< m_ui->sB4->value();
        }
     qDebug()<< m_ui->curves->graphCount();
    

    }
    Is there a possibility to lock the graph when it has got a value?


  • Qt Champions 2016

    Hi
    Frankly, im not sure what the code does.

    I think redesigned it around
    bool QCustomPlot::removeGraph ( QCPGraph * graph);
    would be cleaner than trying to keep track of index.

    Is the design that 1-4 QCPGraph are shown ad each can show a random dataset or is it
    always fixed, so if u show say G3 then its data X each time ?


  • Lifetime Qt Champion

    Except that Switzerland has 4 national languages ;-)

    In any case, the official language of the forum is English except in the International sub-forums so lets keep that one. Also that allows other fellow developers to chime in like @mrjj did.

    Your handling of the plots looks overly complicated.

    Not knowing the rest of the code base I can't really guess how the data are managed however one thing comes to mind: why not just have one widget (e.g. PlotWidget) with a plot and a combobox ? That way you can show the stuff matching your combobox selection. If you want to show more than one plot, then just create an additional PlotWidget and show it.



  • @SGaist,
    you are right.so in english again.
    Of course one plot for each value is easy and I realized this for only one spezial value.
    I want up to four graphs in one plot to compare them or in other words to see what one value does when an other value changes.



  • @mrjj
    hi,
    i have about 50 different values and want to show four values at a time.So possibly i only have to stop input of certain data and continue with data of other value. I will try this. Thank you for brainstorming.


  • Lifetime Qt Champion

    Ok, sorry, I misunderstood your problem.

    So basically you have a set of 50 values and only one plot widget and you want to show at most four of these values.

    One possible way could be to have two QListWidget with the one from the left containing all "showable" values and on the right, the currently shown values. Between the two list you can have a pair of button with an arrow in each direction then when you have 4 values in the right widget you disable the "right to left" arrow and only enable it again when you move back one of the value from right to left.



  • Hi,
    i could solve the problem.I added a second spinbox to each existing.Now the number of the value goes to the second spinbox.This way I can delete the graph.```
    if (m_ui->cB_rpm->isChecked() )
    {
    if ((m_ui->sB1->value() == 1)&&(m_ui->sB1_2->value() == 1))
    {m=0;}
    else if ((m_ui->sB2->value() == 2)&&(m_ui->sB2_2->value() == 1))
    { m=1;}
    else if ((m_ui->sB3->value() == 3)&&(m_ui->sB3_2->value() == 1))
    { m=2;}
    else if ((m_ui->sB4->value() == 4)&&(m_ui->sB4_2->value() == 1))
    { m=3;}

             m_ui->curves->graph(m)->addData(key,rpm);
             m_ui->curves->graph(m)->setName("rpm");
    
             }
    

    if (m_ui->cB_WaterT->isChecked() )
    {
    if ((m_ui->sB1->value() == 1)&&(m_ui->sB1_2->value() == 2))
    {m=0;}
    else if ((m_ui->sB2->value() == 2)&&(m_ui->sB2_2->value() == 2))
    { m=1;}
    else if ((m_ui->sB3->value() == 3)&&(m_ui->sB3_2->value() == 2))
    { m=2;}
    else if ((m_ui->sB4->value() == 4)&&(m_ui->sB4_2->value() == 2))
    { m=3;}
    m_ui->curves->graph(m)->addData(key,WaterT);
    m_ui->curves->graph(m)->setName("WaterTemp");

           }
    

    ~
    and the slot:```
    //1*****************************************************************************
    void MainWindow::on_cB_rpm_stateChanged(int arg1)
    {

        if ((m_ui->sB1->value() == 0)&& (m_ui->cB_rpm->isChecked()))
    	//only spinbox with value 0 and checkbox state changed to checked!
        {
             m_ui->sB1->setValue(1);
    		 // if spinbox1 was "0" it gets 1, if it was spinbox 2 then "2" and so on
             m_ui->sB1_2->setValue(1);
    		 // second spinbox gets the number of the value in this case "1"
    
     qDebug()<< m_ui->sB1->value() ;
     qDebug()<< m_ui->cB_rpm->isChecked();
    
        }
      else if ((m_ui->sB2->value() == 0)&&( m_ui->cB_rpm->isChecked()))
            {
               m_ui->sB2->setValue(2);
               m_ui->sB2_2->setValue(1);
    
         qDebug()<< m_ui->sB2->value() ;
         qDebug()<< m_ui->cB_rpm->isChecked();
    
             }
    
    else if (( m_ui->sB3->value() == 0)&&( m_ui->cB_rpm->isChecked()))
        {
              m_ui->sB3->setValue(3);
              m_ui->sB3_2->setValue(1);
    
                  qDebug()<< m_ui->sB3->value() ;
                  qDebug()<< m_ui->cB_rpm->isChecked();
    
         }
    

    else if (( m_ui->sB4->value()== 0)&& ( m_ui->cB_rpm->isChecked()))
    {
    m_ui->sB4->setValue(4);
    m_ui->sB4_2->setValue(1);
    qDebug()<< m_ui->sB4->value() ;
    qDebug()<< m_ui->cB_rpm->isChecked();

         }
    // ausschalten  switch off of graph
    

    else if ((m_ui->sB1->value()== 1)&& (m_ui->sB1_2->value()== 1))

    // spinbox1 and spinbox 1_2 both must have settings
    {

             m_ui->sB1->setValue(0);
    		 //spinbox one is set to "0" so it can be filled by an other value
            m_ui->curves->graph(0)->data()->clear();
    		// graph is cleared
            m_ui->curves->graph(0)->setName("graph0");
    		//label of graph is set back
             m_ui->curves->replot();
    		 // replot
            qDebug()<<m_ui->cB_rpm->isChecked();
            qDebug()<<m_ui->sB1->value();
         }
    

    else if (( m_ui->sB2->value()== 2)&& (m_ui->sB2_2->value()== 1))
    {

          m_ui->sB2->setValue(0);
          m_ui->curves->graph(1)->data()->clear();
          m_ui->curves->graph(1)->setName("graph1");
          m_ui->curves->replot();
            qDebug()<<m_ui->cB_rpm->isChecked();
            qDebug()<< m_ui->sB2->value();
        }
    

    else if ((m_ui->sB3->value()== 3)&& (m_ui->sB3_2->value()== 1))
    {

          m_ui->sB3->setValue(0);
          m_ui->curves->graph(2)->data()->clear();
          m_ui->curves->graph(2)->setName("graph2");
          m_ui->curves->replot();
        qDebug()<<m_ui->cB_rpm->isChecked();
        qDebug()<< m_ui->sB3->value();
        }
    

    else if ((m_ui->sB4->value() == 4)&& (m_ui->sB4_2->value()== 1))
    {

          m_ui->sB4->setValue(0);
          m_ui->curves->graph(3)->data()->clear();
          m_ui->curves->graph(3)->setName("graph3");
          m_ui->curves->replot();
          qDebug()<<m_ui->cB_rpm->isChecked();
          qDebug()<< m_ui->sB4->value();
        }
    else {m_ui->cB_rpm->setChecked(0);}
    //if all four boxesare already ocupied, checkbox is set to unchecked at any state
    
     qDebug()<< m_ui->curves->graphCount();
    

    }
    ~
    `Thank you for the brainstorming!


Log in to reply
 

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