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;
bool *insel;
bool *inblink;
public slots:
void go_clicked();
private:
group ingrp;
QGraphicsScene *inscene;
bool *insel;
bool *inblink;
QLabel *lbl_grp;
QComboBox *combo_pl;
QPushButton *btn_grp;
To copy to clipboard, switch view to plain text mode
groupwidget.cpp
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;
}
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;
}
To copy to clipboard, switch view to plain text mode
disp.h
private:
void createPlace();
QWidget *createCellWidget
(group grp
);
....
bool blink_stat, sel_item;
....
private:
void createPlace();
QWidget *createCellWidget(group grp);
....
bool blink_stat, sel_item;
QGraphicsScene *scene;
....
To copy to clipboard, switch view to plain text mode
disp.cpp
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;
}
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;
}
To copy to clipboard, switch view to plain text mode
Bookmarks