PDA

View Full Version : customwidget and event help



eva2002
29th January 2010, 00:54
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


public slots:
void go_clicked();

private:
group ingrp;
QGraphicsScene *inscene;
bool *insel;
bool *inblink;

QLabel *lbl_grp;
QComboBox *combo_pl;
QPushButton *btn_grp;


groupwidget.cpp


groupWidget::groupWidget(const group &grp, QGraphicsScene *scene, bool *sel, bool *blink)
{
ingrp = grp;
inscene = scene;
insel = sel;
inblink = blink;

combo_pl = new QComboBox;

combo_pl->addItem("---Select---");

foreach (place p, grp.building)
combo_pl->addItem(p.placename);

combo_pl->setMinimumWidth(200);

btn_go = new QPushButton;
btn_go->setText("Go");
btn_go->setMaximumWidth(50);

lbl_grp = new QLabel;
lbl_grp->setText(grp.grpname);

QGridLayout *layout = new QGridLayout;
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()
{
QPoint pt;
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;
}

QGraphicsEllipseItem *item = new QGraphicsEllipseItem;
item->setRect(pt.x(), pt.y(), ellsize, ellsize);
item->setBrush(Qt::red);
inscene->addItem(item); //seg fault

*insel = true;
*inblink = true;
}


disp.h


private:
void createPlace();
QWidget *createCellWidget(group grp);
....
bool blink_stat, sel_item;
QGraphicsScene *scene;
....


disp.cpp


disp::disp(mapinfo in_map)
{
....
scene = new QGraphicsScene;
scene->setSceneRect(0, 0, 600, 600);
....
}
....
....
....
QWidget *disp::createCellWidget(group grp)
{
groupWidget *pgroupWidget = new groupWidget(grp, scene, &sel_item, &blink_stat);
return pgroupWidget;
}

ecanela
29th January 2010, 06:37
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...