toggle QLineSeries visibility with QPushButton & connect?



  • Hi
    I just wonder how I can accomplish a toggle visibility of a bunch of QLineSeries with some QPushButton's. I have 2 QLineSeries and 2 QPushButtons. Then I wrote 2 public slot functions for each series/button and connected them to each other.

    Is there a more elegant way like having only one toggle function that gets passed the series name as parameter? But how to connect then signal and slot?

    I have now:

    tabwidget.cpp:

    TabWidget::TabWidget(QTabWidget *parent) :
        QTabWidget(parent)
    {...
    seriesPressure = new QtCharts::QLineSeries();
    seriesTempIn = new QtCharts::QLineSeries();
    ...
    pbTempIn = new QPushButton("Pressure",tab3);
    pbTempIn->setCheckable(1);
    pbTempIn->setChecked(1);
    pbTempOut = new QPushButton("Temp In",tab3);
    pbTempOut->setCheckable(1);
    pbTempOut->setChecked(1);
    
    connect(pbTempIn,&QPushButton::clicked,this,&TabWidget::toggleSeriesTempIn);
    connect(pbTempOut,&QPushButton::clicked,this,&TabWidget::toggleSeriesTempOut);
    ...}
    
    void TabWidget::toggleSeriesTempIn()
    {
        if(pbTempIn->isChecked())
        {
            seriesTempIn->show();
        }
        else
            seriesTempIn->hide();
    }
    void TabWidget::toggleSeriesTempOut()
    {
        if(pbTempIn->isChecked())
        {
            seriesTempIn->show();
        }
        else
            seriesTempIn->hide();
    }
    


  • @pauledd hi, this should work just as well:

    connect(pbTempIn,&QPushButton::toggled,seriesTempIn,&QLineSeries::setVisible);
    


  • great! That works. With that I can spare the toggle functions at all.
    Thanks!



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