我正在尝试在QToolbox上使用可变数量的QLineEdit字段来制作可变数量的页面。页数及其名称由字符串向量确定。 QToolbox的每个页面都有两个QLineEdits。调用fillToolbox函数后,将调用printExtractedData函数。如名称中所述,我只是想打印数据的std :: string形式。
void fillToolbox(vector <string> toolboxLabels){
QLineEdit *l1, *l2;
QGridLayout *gridLayout;
for (int i = 0; i < numEntries; i++)
{
gridLayout = new QGridLayout;
l1 = new QLineEdit;
l2 = new QLineEdit;
QFrame *frm = new QFrame;
QLabel *l1Label = new QLabel("Field 1:");
QLabel *l2Label = new QLabel("Field 2:");
gridLayout->addWidget(l1Label, 0, 0);
gridLayout->addWidget(l1, 0, 1);
gridLayout->addWidget(l2Label, 1, 0);
gridLayout->addWidget(l2, 1, 1);
frm->setLayout(gridLayout);
ui.myToolBox->addItem(frm, QString(toolboxLabels[i].c_str()));
}
}
用户将输入添加到LineEdit字段。
void printExtractedData() {
int numPages = ui.myToolBox->count();
cout << numPages << endl;
for (int i = 0; i < numPages; i++)
{
string key = ui.myToolBox->itemText(i).toLocal8Bit().constData();
if (QWidget* wg = qobject_cast<QWidget*>(ui.myToolBox->widget(i))) {
QLayout *layout = wg->layout();
for (int j = 0; j < layout->count(); j++)
{
if (QLineEdit* le = qobject_cast<QLineEdit*>(layout->itemAt(j)->widget())) {
string text = le->text().toLocal8Bit().constData();
cout << le <<endl;
}
}
}
}
}
当我插入两个元素列表时,会收到输出:200000245E5A7726000000245E5A76FE000000245E5A0A51000000245E5B17540期望的结果是QLineEdit字段中内容的打印字符串。任何帮助或建议,将不胜感激。
刚刚意识到我正在打印le而不是文本。我应该多加小心。那是唯一的问题。