跨 QStackedWidget 页面共享变量
我有 3 个页面的 StackedWidget,每个页面包含 3 个 lineEdit,现在在我经过的每个页面上,我想将这 3 个 lineEdit 的内容保存到一个变量中以便以后能够处理它。 我有这段代码:
connect(ui->stackedWidget, SIGNAL(currentChanged(int)), this, SLOT(getInputs(int)));
和槽:
void ConfSetup::getInputs(int index)
{
QString para;
switch(index)
{
case 1:
ui->backButton0->setEnabled(false);
break;
case 2:
inputs << ui->serverEdit->text();
inputs << ui->portEdit->text();
break;
case 3:
inputs << ui->userDbEdit->text();
inputs << ui->passwordDbEdit->text();
break;
case 6:
foreach(para, inputs)
ui->comboBox->addItem(para);
//ui->lineEdit->setText(QString::number(para.length()));
break;
default:
ui->backButton0->setEnabled(true);
break;
}
}
comboBox
小部件现在应该包含 4 个值,但它包含一个空白文本,而且 para.length()
返回lineEdit
小部件中的 0。
I have StackedWidget with 3 pages, every page contain 3 lineEdit, now at every page I pass, I want to save the content of this 3 lineEdit into a variable to be able later handle it.
I have this piece of code:
connect(ui->stackedWidget, SIGNAL(currentChanged(int)), this, SLOT(getInputs(int)));
And the slot:
void ConfSetup::getInputs(int index)
{
QString para;
switch(index)
{
case 1:
ui->backButton0->setEnabled(false);
break;
case 2:
inputs << ui->serverEdit->text();
inputs << ui->portEdit->text();
break;
case 3:
inputs << ui->userDbEdit->text();
inputs << ui->passwordDbEdit->text();
break;
case 6:
foreach(para, inputs)
ui->comboBox->addItem(para);
//ui->lineEdit->setText(QString::number(para.length()));
break;
default:
ui->backButton0->setEnabled(true);
break;
}
}
The comboBox
widget should now contains 4 values, but rather then it contains a blank text, also para.length()
return 0 in lineEdit
widget.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
为什么不在主窗口中使用成员变量?只需在头文件中声明它们并在 cpp 中初始化即可。
当切换 QStackedWidget 的页面时,您会读取编辑的内容并将它们保存到您的成员变量中。只需使用与您已经使用的相同的信号即可:
QStackedWidget::currentChanged ( int index )
Why don't you use member variables in your main window? Just declare them in your header file and initialize in your cpp.
And when switching a page of your QStackedWidget you read the content of the edits and save them to your member variable. Just use them same signal as you already do:
QStackedWidget::currentChanged ( int index )
一位朋友告诉我,我在更改索引页时正在填充
inputs
变量,这意味着当时lineEdit
小部件是空的。解决方案非常简单,首先,我避免 getInputs(int ) 插槽,并替换为:A friend tell me that I am filling the
inputs
variable at time I change the index page, witch mean of course thelineEdit
widgets are empty at that time. The solution is very simple, first, I avoidgetInputs(int )
slot, and in replacement I make this: