简体   繁体   English

将Qt中的变量从QTabWidget的选项卡传递到选项卡

[英]Passing Variable in Qt from tab to tab in QTabWidget

I am newbie to Qt and currently working on one QTabWidget which have two tabs means two widgets, as such 我是Qt的新手,目前正在研究一个QTabWidget,它具有两个选项卡,意味着两个小部件,例如

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. 谁能告诉我如何在第二个标签中使用第一个标签的值。

You can maintain variables at tabWidget calling level. 您可以在tabWidget调用级别维护变量。 and pass variables by reference to both the tabs. 并通过引用两个选项卡传递变量。

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. 当这个整数和浮点数变化时, First_Widget发出信号,而Second_Widget将这个信号连接到相关的插槽。

Second_Widget only has to know and interface that you can pass as constructor parameter: Second_Widget只需要知道和可以作为构造函数参数传递的接口即可:

// 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"));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM