Hi all,

I need got this problem which I have no idea why it happens and how to remedy it.

I got a customWidget (groupwidget) with an event button clicked. I pass a QGraphicsScene pointer in the main class (disp) to the customWidget (groupwidget).

when I add a QGraphicsItem into the QGraphicsScene, I got a seg fault. I am am sure that the inputs to graphicsitem->setRect exists and is correct.

How to solve this problem?

groupwidget.h
Qt Code:
  1. public slots:
  2. void go_clicked();
  3.  
  4. private:
  5. group ingrp;
  6. QGraphicsScene *inscene;
  7. bool *insel;
  8. bool *inblink;
  9.  
  10. QLabel *lbl_grp;
  11. QComboBox *combo_pl;
  12. QPushButton *btn_grp;
To copy to clipboard, switch view to plain text mode 

groupwidget.cpp
Qt Code:
  1. groupWidget::groupWidget(const group &grp, QGraphicsScene *scene, bool *sel, bool *blink)
  2. {
  3. ingrp = grp;
  4. inscene = scene;
  5. insel = sel;
  6. inblink = blink;
  7.  
  8. combo_pl = new QComboBox;
  9.  
  10. combo_pl->addItem("---Select---");
  11.  
  12. foreach (place p, grp.building)
  13. combo_pl->addItem(p.placename);
  14.  
  15. combo_pl->setMinimumWidth(200);
  16.  
  17. btn_go = new QPushButton;
  18. btn_go->setText("Go");
  19. btn_go->setMaximumWidth(50);
  20.  
  21. lbl_grp = new QLabel;
  22. lbl_grp->setText(grp.grpname);
  23.  
  24. QGridLayout *layout = new QGridLayout;
  25. layout->addWidget(lbl_grp, 0, 0);
  26. layout->addWidget(combo_pl, 1, 0);
  27. layout->addWidget(btn_go,1,1);
  28.  
  29. setLayout(layout);
  30. connect(btn_go, SIGNAL(clicked()), this, SLOT(go_clicked()));
  31. }
  32.  
  33. void groupWidget::go_clicked()
  34. {
  35. QPoint pt;
  36. bool stat = false;
  37.  
  38. foreach (place p, ingrp.building)
  39. {
  40. if ((combo_pl->currentText().trimmed().compare(p.placename)) == 0)
  41. {
  42. bool ok;
  43. pt.setX(p.x.toInt(&ok,10));
  44. pt.setY(p.y.toInt(&ok,10));
  45. stat = true;
  46. }
  47.  
  48. if (stat)
  49. break;
  50. }
  51.  
  52. if (!stat) {
  53. *insel = false;
  54. *inblink = false;
  55. return;
  56. }
  57.  
  58. item->setRect(pt.x(), pt.y(), ellsize, ellsize);
  59. item->setBrush(Qt::red);
  60. inscene->addItem(item); //seg fault
  61.  
  62. *insel = true;
  63. *inblink = true;
  64. }
To copy to clipboard, switch view to plain text mode 

disp.h
Qt Code:
  1. private:
  2. void createPlace();
  3. QWidget *createCellWidget(group grp);
  4. ....
  5. bool blink_stat, sel_item;
  6. ....
To copy to clipboard, switch view to plain text mode 

disp.cpp
Qt Code:
  1. disp::disp(mapinfo in_map)
  2. {
  3. ....
  4. scene = new QGraphicsScene;
  5. scene->setSceneRect(0, 0, 600, 600);
  6. ....
  7. }
  8. ....
  9. ....
  10. ....
  11. QWidget *disp::createCellWidget(group grp)
  12. {
  13. groupWidget *pgroupWidget = new groupWidget(grp, scene, &sel_item, &blink_stat);
  14. return pgroupWidget;
  15. }
To copy to clipboard, switch view to plain text mode