Passing Variable in Qt from tab to tab in QTabWidget

711 Views Asked by At

I am newbie to Qt and currently working on one QTabWidget which have two tabs means two widgets, as such

tabWidget->addTab(new First_Widget(),tr("Home"));
tabWidget->addTab(new Second_Widget(), tr("Download"));

First Widget have some integers and floats values which i want to use in the second widget. I can make a constructor of second widget like this

tabWidget->addTab(new Second_Widget(argument1, argument2,argument3), tr("Download"));

and i think i can call second tab form the first widget but in that case the second tab will be hide until i don't click the respective button. Can anyone tell me how i can use first tab's values in the second.

2

There are 2 best solutions below

0
On

You can maintain variables at tabWidget calling level. and pass variables by reference to both the tabs.

3
On

I suggest you to use emit-connect mechanism.

First_Widget emits signals when this integers and floats change and Second_Widget connects this signals to associated slots.

Second_Widget only has to know and interface that you can pass as constructor parameter:

// demo code, not tested
class IEmmiter
{
    Q_OBJECT
    signal:
       void Integer1Changed(int new_value);
       .....
};

class First_Widget : public IEmmiter
{

};

class Second_Widget
{
    Second_Widget(const IEmmiter & emmiter)
    {
        connect(&emmiter, SIGNAL(Integer1Changed(int)), this, SLOT(Integer1Changed(int)));
        ....
    }

};

// on your programm
auto fw = First_Widget();
auto sw = Second_Widget(*fw);

tabWidget->addTab(fw, tr("Home"));
tabWidget->addTab(sw, tr("Download"));