customwidget and event help
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
Code:
public slots:
void go_clicked();
private:
group ingrp;
bool *insel;
bool *inblink;
groupwidget.cpp
Code:
groupWidget
::groupWidget(const group
&grp,
QGraphicsScene *scene,
bool *sel,
bool *blink
){
ingrp = grp;
inscene = scene;
insel = sel;
inblink = blink;
combo_pl->addItem("---Select---");
foreach (place p, grp.building)
combo_pl->addItem(p.placename);
combo_pl->setMinimumWidth(200);
btn_go->setText("Go");
btn_go->setMaximumWidth(50);
lbl_grp->setText(grp.grpname);
layout->addWidget(lbl_grp, 0, 0);
layout->addWidget(combo_pl, 1, 0);
layout->addWidget(btn_go,1,1);
setLayout(layout);
connect(btn_go, SIGNAL(clicked()), this, SLOT(go_clicked()));
}
void groupWidget::go_clicked()
{
bool stat = false;
foreach (place p, ingrp.building)
{
if ((combo_pl->currentText().trimmed().compare(p.placename)) == 0)
{
bool ok;
pt.setX(p.x.toInt(&ok,10));
pt.setY(p.y.toInt(&ok,10));
stat = true;
}
if (stat)
break;
}
if (!stat) {
*insel = false;
*inblink = false;
return;
}
item->setRect(pt.x(), pt.y(), ellsize, ellsize);
item->setBrush(Qt::red);
inscene->addItem(item); //seg fault
*insel = true;
*inblink = true;
}
disp.h
Code:
private:
void createPlace();
QWidget *createCellWidget
(group grp
);
....
bool blink_stat, sel_item;
....
disp.cpp
Code:
disp::disp(mapinfo in_map)
{
....
scene->setSceneRect(0, 0, 600, 600);
....
}
....
....
....
QWidget *disp
::createCellWidget(group grp
) {
groupWidget *pgroupWidget = new groupWidget(grp, scene, &sel_item, &blink_stat);
return pgroupWidget;
}
Re: customwidget and event help
please, for another post, try to get the minimum compiled example. most code are unrelated whit the problem,
One cause to the seg faul is that the QGraphicsScene is not created. in the constuctor of groupWidget, you pass a QGraphicsScen pointer. PROBABLY when you create a groupWidget dont pass a valid QGraphicsScene.
you create a QGraphicsScene in disp::disp() but not show when this code are called or how is used...