如何从QtObjects(lineedit,combobox)获取?

时间:2019-07-11 11:00:58

标签: c++ qt

enter image description here

我使用for(int i = 0; i

赞:

i=0 1.Runner distance ,percentage = %80 Place = ComboBoxValue 
i=1 1.Runner distance ,percentage = %60 Place = ComboBoxValue

当我更改组合框或百分比值时。最后,我要获取百分比和comboBox值。 像这样:

QString Runner1 = QString(_Numberlabel-> text()+ label-> text()+ lineEdit-> text()+ comboBox-> currentText());  QString Runner2 = QString(_Numberlabel-> text()+ label-> text()+ lineEdit-> text()+ comboBox-> currentText());

enter code here

for(int i=0; i < runnerList.size() ;i++)
{
Layout = new QHBoxLayout();
Layout->setSpacing(6);
_Numberlabel= new QLabel();
_Numberlabel->setObjectName(QString::fromUtf8("_Numberlabel"));
_Numberlabel->setText(QString("First Runner").arg(i+1).arg(runnerList[i][0]).arg(runnerList[i][1]));

 QFont font;
 font.setFamily(QString::fromUtf8("Calibri"));
 font.setPointSize(10);
 font.setBold(true);
 font.setWeight(75);
 _Numberlabel->setFont(font);

 Layout->addWidget(_Numberlabel);

 label = new QLabel();
 label->setObjectName(QString::fromUtf8("label"));
 label->setMaximumSize(QSize(60, 16777215));
 label->setFont(font);
 label->setText("Percantage:");
 Layout->addWidget(label);

 lineEdit = new QLineEdit();
 lineEdit->setObjectName(QString::fromUtf8("lineEdit"));
 lineEdit->setMaximumSize(QSize(50, 16777215));
 lineEdit->setText("%");

 Layout->addWidget(lineEdit);
 comboBox = new QComboBox();
 comboBox->setObjectName(QString::fromUtf8("comboBox"));
 comboBox->setMinimumSize(QSize(0, 25));
 comboBox->setMaximumSize(QSize(90, 16777215));
 comboBox->addItem("1");
 comboBox->addItem("2");
 comboBox->addItem("3");
 comboBox->addItem("4");
 Layout->addWidget(comboBox);
 this->ui.verticalLayout->addLayout(Layout);
 }

1 个答案:

答案 0 :(得分:0)

任何一个都需要保留创建的对象的句柄:

// definition:
QList<QLineEdit*> lineEdits;
QList<QComboBox*> combos;

这些列表应该是班级成员。

// usage:
for(int i=0; i < runnerList.size() ;i++)
{
    //...
    lineEdits << lineEdit;
    combos << comboBox;
}

然后,您可以稍后获取这些值:

... = QString(lineEdits[i]->text() + combos[i]->currentText());


或者,您可以通过其唯一的对象名称找到它们:

ui.verticalLayout->findChild<QLineEdit*>(QString("lineEdit_%1").arg(i))->text();
// I didn't try to compile this code!

为此,您需要为每个QLineEdit / QComboBox指定一个唯一的对象名称。

或者,您搜索所有QLineEdit,并希望它们的顺序定义明确:

ui.verticalLayout->findChildren<QLineEdit*>("lineEdit")[i]->text();


免责声明:这非常丑陋,但是您应该能够看到它的工作原理。

无论您选择什么,都要注意:

您的行

_Numberlabel->setText(QString("First Runner").arg(i+1).arg(runnerList[i][0]).arg(runnerList[i][1]));

包含错误,因为您的QString(...)不包含占位符%1%2,...,但后面跟随着.arg()调用。除了在控制台上创建警告之外,它什么也不会做。