Results 1 to 2 of 2

Thread: Pointer Question related to QLineEdit

  1. #1
    Join Date
    Apr 2008
    Posts
    7
    Thanks
    2
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Pointer Question related to QLineEdit

    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

    Qt Code:
    1. QLineEdit *byte0Edit;
    2. QLineEdit *byte1Edit;
    3. QLineEdit *byte2Edit;
    4. QLineEdit *Byte3Edit;
    5. QLineEdit *byte6Edit;
    6. QLineEdit *byte4Edit;
    7. QLineEdit *byte7Edit;
    8. QLineEdit *byte5Edit;
    9. QLineEdit *byte13Edit;
    10. QLineEdit *byte12Edit;
    11. QLineEdit *byte10Edit;
    12. QLineEdit *byte14Edit;
    13. QLineEdit *byte8Edit;
    14. QLineEdit *byte11Edit;
    15. QLineEdit *byte15Edit;
    16. QLineEdit *byte9Edit;
    17. QLineEdit *byte16Edit;
    18. QLineEdit *byte17Edit;
    19.  
    20. byte0Edit = new QLineEdit(groupBox_2);
    21. byte0Edit->setObjectName(QString::fromUtf8("byte0Edit"));
    22. byte0Edit->setGeometry(QRect(177, 40, 31, 20));
    23. byte1Edit = new QLineEdit(groupBox_2);
    24. byte1Edit->setObjectName(QString::fromUtf8("byte1Edit"));
    25. byte1Edit->setGeometry(QRect(217, 40, 31, 20));
    26. byte2Edit = new QLineEdit(groupBox_2);
    27. byte2Edit->setObjectName(QString::fromUtf8("byte2Edit"));
    28. byte2Edit->setGeometry(QRect(257, 40, 31, 20));
    29. Byte3Edit = new QLineEdit(groupBox_2);
    30. Byte3Edit->setObjectName(QString::fromUtf8("Byte3Edit"));
    31. Byte3Edit->setGeometry(QRect(297, 40, 31, 20));
    32. byte6Edit = new QLineEdit(groupBox_2);
    33. byte6Edit->setObjectName(QString::fromUtf8("byte6Edit"));
    34. byte6Edit->setGeometry(QRect(417, 40, 31, 20));
    35. byte4Edit = new QLineEdit(groupBox_2);
    36. byte4Edit->setObjectName(QString::fromUtf8("byte4Edit"));
    37. byte4Edit->setGeometry(QRect(337, 40, 31, 20));
    38. byte7Edit = new QLineEdit(groupBox_2);
    39. byte7Edit->setObjectName(QString::fromUtf8("byte7Edit"));
    40. byte7Edit->setGeometry(QRect(457, 40, 31, 20));
    41. byte5Edit = new QLineEdit(groupBox_2);
    42. byte5Edit->setObjectName(QString::fromUtf8("byte5Edit"));
    43. byte5Edit->setGeometry(QRect(377, 40, 31, 20));
    44. byte13Edit = new QLineEdit(groupBox_2);
    45. byte13Edit->setObjectName(QString::fromUtf8("byte13Edit"));
    46. byte13Edit->setGeometry(QRect(337, 70, 31, 20));
    47. byte12Edit = new QLineEdit(groupBox_2);
    48. byte12Edit->setObjectName(QString::fromUtf8("byte12Edit"));
    49. byte12Edit->setGeometry(QRect(297, 70, 31, 20));
    50. byte10Edit = new QLineEdit(groupBox_2);
    51. byte10Edit->setObjectName(QString::fromUtf8("byte10Edit"));
    52. byte10Edit->setGeometry(QRect(217, 70, 31, 20));
    53. byte14Edit = new QLineEdit(groupBox_2);
    54. byte14Edit->setObjectName(QString::fromUtf8("byte14Edit"));
    55. byte14Edit->setGeometry(QRect(377, 70, 31, 20));
    56. byte8Edit = new QLineEdit(groupBox_2);
    57. byte8Edit->setObjectName(QString::fromUtf8("byte8Edit"));
    58. byte8Edit->setGeometry(QRect(500, 40, 31, 20));
    59. byte11Edit = new QLineEdit(groupBox_2);
    60. byte11Edit->setObjectName(QString::fromUtf8("byte11Edit"));
    61. byte11Edit->setGeometry(QRect(257, 70, 31, 20));
    62. byte15Edit = new QLineEdit(groupBox_2);
    63. byte15Edit->setObjectName(QString::fromUtf8("byte15Edit"));
    64. byte15Edit->setGeometry(QRect(417, 70, 31, 20));
    65. byte9Edit = new QLineEdit(groupBox_2);
    66. byte9Edit->setObjectName(QString::fromUtf8("byte9Edit"));
    67. byte9Edit->setGeometry(QRect(177, 70, 31, 20));
    68. byte16Edit = new QLineEdit(groupBox_2);
    69. byte16Edit->setObjectName(QString::fromUtf8("byte16Edit"));
    70. byte16Edit->setGeometry(QRect(457, 70, 31, 20));
    71. byte17Edit = new QLineEdit(groupBox_2);
    72. byte17Edit->setObjectName(QString::fromUtf8("byte17Edit"));
    To copy to clipboard, switch view to plain text mode 

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

    Qt Code:
    1. QLineEdit * LineEditArray[18];
    2.  
    3. LineEditArray[0] = ui.byte0Edit;
    4. LineEditArray[1] = ui.byte1Edit;
    5. LineEditArray[2] = ui.byte2Edit;
    6. LineEditArray[3] = ui.Byte3Edit;
    7. LineEditArray[4] = ui.byte4Edit;
    8. LineEditArray[5] = ui.byte5Edit;
    9. LineEditArray[6] = ui.byte6Edit;
    10. LineEditArray[7] = ui.byte7Edit;
    11. LineEditArray[8] = ui.byte8Edit;
    12. LineEditArray[9] = ui.byte9Edit;
    13. LineEditArray[10] = ui.byte10Edit;
    14. LineEditArray[11] = ui.byte11Edit;
    15. LineEditArray[12] = ui.byte12Edit;
    16. LineEditArray[13] = ui.byte13Edit;
    17. LineEditArray[14] = ui.byte14Edit;
    18. LineEditArray[15] = ui.byte15Edit;
    19. LineEditArray[16] = ui.byte16Edit;
    20. LineEditArray[17] = ui.byte17Edit;
    To copy to clipboard, switch view to plain text mode 

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

    Qt Code:
    1. for(int i = 0; i < 18; i++)
    2. {
    3. LineEditArray[i]->setText("");
    4. }
    To copy to clipboard, switch view to plain text mode 

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

  2. #2
    Join Date
    May 2006
    Posts
    788
    Thanks
    49
    Thanked 48 Times in 46 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Pointer Question related to QLineEdit

    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 ....

    Qt Code:
    1. enum { NumComandButton = 13 }; /* on header */
    2.  
    3. /* valid action */
    4. QList<QAction *> actionlist = Layer->MainActions();
    5. Q_ASSERT ( actionlist.size() == NumComandButton );
    6. for (int i = 0; i < NumComandButton; ++i) {
    7. tbutton[i]->SetAction(actionlist[i]);
    8. tbutton[i]->setEnabled(true);
    9. }
    To copy to clipboard, switch view to plain text mode 


    The model sample:

    write new model 2 column
    Qt Code:
    1. QStandardItemModel *PageXmlHandler::ModelBase()
    2. {
    3. bold_base_font = qApp->font();
    4. flags_yes = Qt::ItemIsEditable | Qt::ItemIsEnabled | Qt::ItemIsSelectable;
    5. flags_no = Qt::ItemIsEnabled | Qt::ItemIsSelectable;
    6. bold_base_font.setBold ( true );
    7. model = new QStandardItemModel();
    8.  
    9. QStandardItem *Hitem1 = new QStandardItem(tr("Name"));
    10. QStandardItem *Hitem2 = new QStandardItem(tr("Value"));
    11. Hitem1->setData(bold_base_font,Qt::FontRole);
    12. Hitem2->setData(bold_base_font,Qt::FontRole);
    13. model->setHorizontalHeaderItem(0,Hitem1);
    14. model->setHorizontalHeaderItem(1,Hitem2);
    15. model->invisibleRootItem()->appendRow(ReadOnlyValue("epoche",tr("ID:")));
    16. model->invisibleRootItem()->appendRow(TagValue("cat",tr("Category:")));
    17. model->invisibleRootItem()->appendRow(TagName("tit_",tr("Title:"),5));
    18. model->invisibleRootItem()->appendRow(TagName("desc_",tr("Description:"),15));
    19. model->invisibleRootItem()->appendRow(TagName("note_",tr("Note:")));
    20. model->invisibleRootItem()->appendRow(TagName("autor_",tr("Autor:")));
    21. model->invisibleRootItem()->appendRow(TagValue("active",tr("Priority:")));
    22. model->invisibleRootItem()->appendRow(TagValue("start",tr("Start time:")));
    23. model->invisibleRootItem()->appendRow(TagValue("stop",tr("Stop time:")));
    24. model->invisibleRootItem()->appendRow(TagValue("worksource",tr("Source work:")));
    25. model->invisibleRootItem()->appendRow(TagValue("bgcolor",tr("Background color:")));
    26.  
    27. return model;
    28.  
    29. }
    30. /* each line row */
    31. QList<QStandardItem *> PageXmlHandler::TagValue( const QString shortname , const QString HumanName )
    32. {
    33. QStandardItem *noedit = new QStandardItem(HumanName);
    34. noedit->setFlags(flags_no);
    35. noedit->setData(bold_base_font,Qt::FontRole);
    36. noedit->setData(grundierung,Qt::BackgroundColorRole);
    37. QList<QStandardItem *> diritto;
    38. QStandardItem *Hitem2 = new QStandardItem(Current->Root_Tag(shortname).toString());
    39. Hitem2->setFlags(flags_yes);
    40. Hitem2->setToolTip ( shortname );
    41. Hitem2->setData(shortname,TAGID);
    42. Hitem2->setData(2,T_FORMAT);
    43. Hitem2->setData(Current->Root_Tag(shortname),OLDVALUE);
    44. diritto.append(noedit);
    45. diritto.append(Hitem2);
    46. return diritto;
    47. }
    To copy to clipboard, switch view to plain text mode 

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

Similar Threads

  1. directory related question
    By abrou in forum Newbie
    Replies: 1
    Last Post: 18th April 2008, 20:34
  2. base pointer question
    By mickey in forum General Programming
    Replies: 11
    Last Post: 11th March 2008, 16:43
  3. KeyPressEvent and QLineEdit question
    By impeteperry in forum Qt Programming
    Replies: 1
    Last Post: 21st August 2007, 17:18
  4. simple pointer question
    By mickey in forum General Programming
    Replies: 6
    Last Post: 16th June 2006, 10:19

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.