PDA

View Full Version : Pointer Question related to QLineEdit



ChrisReath
23rd May 2008, 14:33
Hey Guys, I've got a simple pointer question related to QTextEdit. I have 18 QLineEdit boxes. I want to create an Array of QTextEdit pointers to point to the 18 instances of QTextEdit so that I can access them using an index rather then having to call each instance every time. When I do this, however, a runtime error is generated.

The 18 instances of QLineEdit



QLineEdit *byte0Edit;
QLineEdit *byte1Edit;
QLineEdit *byte2Edit;
QLineEdit *Byte3Edit;
QLineEdit *byte6Edit;
QLineEdit *byte4Edit;
QLineEdit *byte7Edit;
QLineEdit *byte5Edit;
QLineEdit *byte13Edit;
QLineEdit *byte12Edit;
QLineEdit *byte10Edit;
QLineEdit *byte14Edit;
QLineEdit *byte8Edit;
QLineEdit *byte11Edit;
QLineEdit *byte15Edit;
QLineEdit *byte9Edit;
QLineEdit *byte16Edit;
QLineEdit *byte17Edit;

byte0Edit = new QLineEdit(groupBox_2);
byte0Edit->setObjectName(QString::fromUtf8("byte0Edit"));
byte0Edit->setGeometry(QRect(177, 40, 31, 20));
byte1Edit = new QLineEdit(groupBox_2);
byte1Edit->setObjectName(QString::fromUtf8("byte1Edit"));
byte1Edit->setGeometry(QRect(217, 40, 31, 20));
byte2Edit = new QLineEdit(groupBox_2);
byte2Edit->setObjectName(QString::fromUtf8("byte2Edit"));
byte2Edit->setGeometry(QRect(257, 40, 31, 20));
Byte3Edit = new QLineEdit(groupBox_2);
Byte3Edit->setObjectName(QString::fromUtf8("Byte3Edit"));
Byte3Edit->setGeometry(QRect(297, 40, 31, 20));
byte6Edit = new QLineEdit(groupBox_2);
byte6Edit->setObjectName(QString::fromUtf8("byte6Edit"));
byte6Edit->setGeometry(QRect(417, 40, 31, 20));
byte4Edit = new QLineEdit(groupBox_2);
byte4Edit->setObjectName(QString::fromUtf8("byte4Edit"));
byte4Edit->setGeometry(QRect(337, 40, 31, 20));
byte7Edit = new QLineEdit(groupBox_2);
byte7Edit->setObjectName(QString::fromUtf8("byte7Edit"));
byte7Edit->setGeometry(QRect(457, 40, 31, 20));
byte5Edit = new QLineEdit(groupBox_2);
byte5Edit->setObjectName(QString::fromUtf8("byte5Edit"));
byte5Edit->setGeometry(QRect(377, 40, 31, 20));
byte13Edit = new QLineEdit(groupBox_2);
byte13Edit->setObjectName(QString::fromUtf8("byte13Edit"));
byte13Edit->setGeometry(QRect(337, 70, 31, 20));
byte12Edit = new QLineEdit(groupBox_2);
byte12Edit->setObjectName(QString::fromUtf8("byte12Edit"));
byte12Edit->setGeometry(QRect(297, 70, 31, 20));
byte10Edit = new QLineEdit(groupBox_2);
byte10Edit->setObjectName(QString::fromUtf8("byte10Edit"));
byte10Edit->setGeometry(QRect(217, 70, 31, 20));
byte14Edit = new QLineEdit(groupBox_2);
byte14Edit->setObjectName(QString::fromUtf8("byte14Edit"));
byte14Edit->setGeometry(QRect(377, 70, 31, 20));
byte8Edit = new QLineEdit(groupBox_2);
byte8Edit->setObjectName(QString::fromUtf8("byte8Edit"));
byte8Edit->setGeometry(QRect(500, 40, 31, 20));
byte11Edit = new QLineEdit(groupBox_2);
byte11Edit->setObjectName(QString::fromUtf8("byte11Edit"));
byte11Edit->setGeometry(QRect(257, 70, 31, 20));
byte15Edit = new QLineEdit(groupBox_2);
byte15Edit->setObjectName(QString::fromUtf8("byte15Edit"));
byte15Edit->setGeometry(QRect(417, 70, 31, 20));
byte9Edit = new QLineEdit(groupBox_2);
byte9Edit->setObjectName(QString::fromUtf8("byte9Edit"));
byte9Edit->setGeometry(QRect(177, 70, 31, 20));
byte16Edit = new QLineEdit(groupBox_2);
byte16Edit->setObjectName(QString::fromUtf8("byte16Edit"));
byte16Edit->setGeometry(QRect(457, 70, 31, 20));
byte17Edit = new QLineEdit(groupBox_2);
byte17Edit->setObjectName(QString::fromUtf8("byte17Edit"));



I then declare an array of QLineEdit Pointers. And point at the 18 instances of QLineEdit.




QLineEdit * LineEditArray[18];

LineEditArray[0] = ui.byte0Edit;
LineEditArray[1] = ui.byte1Edit;
LineEditArray[2] = ui.byte2Edit;
LineEditArray[3] = ui.Byte3Edit;
LineEditArray[4] = ui.byte4Edit;
LineEditArray[5] = ui.byte5Edit;
LineEditArray[6] = ui.byte6Edit;
LineEditArray[7] = ui.byte7Edit;
LineEditArray[8] = ui.byte8Edit;
LineEditArray[9] = ui.byte9Edit;
LineEditArray[10] = ui.byte10Edit;
LineEditArray[11] = ui.byte11Edit;
LineEditArray[12] = ui.byte12Edit;
LineEditArray[13] = ui.byte13Edit;
LineEditArray[14] = ui.byte14Edit;
LineEditArray[15] = ui.byte15Edit;
LineEditArray[16] = ui.byte16Edit;
LineEditArray[17] = ui.byte17Edit;



I then try to set the text of the QLineEdit. This is where the error is generated on the first call to setText.




for(int i = 0; i < 18; i++)
{
LineEditArray[i]->setText("");
}



I can tell I'm overlooking something. Any ideas? Thanks.

patrik08
23rd May 2008, 15:13
Why you not use a model? QStandardItemModel to fille a QTreeView
2 columns column 0 as label and 1 editable to data ... like QDesigner Propriety...


Your way ....



enum { NumComandButton = 13 }; /* on header */

/* valid action */
QList<QAction *> actionlist = Layer->MainActions();
Q_ASSERT ( actionlist.size() == NumComandButton );
for (int i = 0; i < NumComandButton; ++i) {
tbutton[i]->SetAction(actionlist[i]);
tbutton[i]->setEnabled(true);
}




The model sample:

write new model 2 column


QStandardItemModel *PageXmlHandler::ModelBase()
{
bold_base_font = qApp->font();
flags_yes = Qt::ItemIsEditable | Qt::ItemIsEnabled | Qt::ItemIsSelectable;
flags_no = Qt::ItemIsEnabled | Qt::ItemIsSelectable;
bold_base_font.setBold ( true );
model = new QStandardItemModel();

QStandardItem *Hitem1 = new QStandardItem(tr("Name"));
QStandardItem *Hitem2 = new QStandardItem(tr("Value"));
Hitem1->setData(bold_base_font,Qt::FontRole);
Hitem2->setData(bold_base_font,Qt::FontRole);
model->setHorizontalHeaderItem(0,Hitem1);
model->setHorizontalHeaderItem(1,Hitem2);
model->invisibleRootItem()->appendRow(ReadOnlyValue("epoche",tr("ID:")));
model->invisibleRootItem()->appendRow(TagValue("cat",tr("Category:")));
model->invisibleRootItem()->appendRow(TagName("tit_",tr("Title:"),5));
model->invisibleRootItem()->appendRow(TagName("desc_",tr("Description:"),15));
model->invisibleRootItem()->appendRow(TagName("note_",tr("Note:")));
model->invisibleRootItem()->appendRow(TagName("autor_",tr("Autor:")));
model->invisibleRootItem()->appendRow(TagValue("active",tr("Priority:")));
model->invisibleRootItem()->appendRow(TagValue("start",tr("Start time:")));
model->invisibleRootItem()->appendRow(TagValue("stop",tr("Stop time:")));
model->invisibleRootItem()->appendRow(TagValue("worksource",tr("Source work:")));
model->invisibleRootItem()->appendRow(TagValue("bgcolor",tr("Background color:")));

return model;

}
/* each line row */
QList<QStandardItem *> PageXmlHandler::TagValue( const QString shortname , const QString HumanName )
{
QStandardItem *noedit = new QStandardItem(HumanName);
noedit->setFlags(flags_no);
noedit->setData(bold_base_font,Qt::FontRole);
noedit->setData(grundierung,Qt::BackgroundColorRole);
QList<QStandardItem *> diritto;
QStandardItem *Hitem2 = new QStandardItem(Current->Root_Tag(shortname).toString());
Hitem2->setFlags(flags_yes);
Hitem2->setToolTip ( shortname );
Hitem2->setData(shortname,TAGID);
Hitem2->setData(2,T_FORMAT);
Hitem2->setData(Current->Root_Tag(shortname),OLDVALUE);
diritto.append(noedit);
diritto.append(Hitem2);
return diritto;
}




at end QTreeView setmodel editable and grab model back data....