PDA

View Full Version : signal/slot



popai
31st July 2007, 16:22
Hi,

I am relatively new to Qt and am having a problem with a signal/slot configuration. I am using Qt version 4.2.2, Win.

I have a QMainWindow ho have a toolbar and QGraphicsView .
I want to draw a QGraphicsItem when a action(toolbar button) is clicked.

h file
class FoApp : public QMainWindow
{
Q_OBJECT
......
QAction *warkCj
QToolBar *workToolbar;

FoMap *view;
}

cpp file
void FoApp::initActions()
{
....
warkCj = new QAction(tr("C&j"), this);
warkCj->setShortcut(tr("Ctrl+J"));
warkCj->setStatusTip(tr("Insert CJ in the document"));
warkCj->setIcon(QIcon(QString::fromUtf8("../bitmaps/service.png")));
connect(warkCj, SIGNAL(triggered()), view, SLOT(AddCj));
....
}

h file
class FoMap : public QGraphicsView
{
Q_OBJECT
......
public slots:
virtual void AddCj();
...
private:
QGraphicsScene *scene;

}

cpp file

void FoMap::AddCablu()
{
item << new Cablu(scene,200, 100);
item.last()->setPos(QPointF(0, 100));
scene->addItem(item.last());
scene->update();
}


The code builds and there are no error messages displayed , but whwn i run the program, the program crash

If i comment connect(warkCj, SIGNAL(triggered()), view, SLOT(AddCj));
program run.

marcel
31st July 2007, 16:25
This is because the "view" object is uninitialized at that point.
So instantiate the view first, and then connect signals to its slots.

Regards

popai
31st July 2007, 18:23
This is because the "view" object is uninitialized at that point.
So instantiate the view first, and then connect signals to its slots.

Regards

ok i instantiate the view first and program run, but don't draw the QGraphicsItem when a action(toolbar button) is clicked. :(

If i zoom in out the QGraphicsItem apair, why hi dont apeir when i click the button ?

marcel
31st July 2007, 20:55
This is most likely because of the cordinates you give to the item. They might be outside of the scene rect.

You can consider increasing the scene rect or give the item some in-scene coordinates.

Regrds

popai
1st August 2007, 08:19
This is most likely because of the cordinates you give to the item. They might be outside of the scene rect.

You can consider increasing the scene rect or give the item some in-scene coordinates.

Regrds

thank
The cordinates ar not in outside of the scene rect, i observ if mouve the mouse over the place wher the QGraphicsItem must be he apair, i mast cool same function to repaint the view (or scene)?

void FoMap::AddCablu()
{
item << new Cablu(scene,200, 100);
item.last()->setPos(QPointF(0, 100));
scene->addItem(item.last());
scene->update(); // with theat dont show the QGraphicsItem
}

marcel
1st August 2007, 08:39
Well, you shouldn't call update every time you add a new item to the scene. The view should update by itself.

Do you have any event filters installed on the scene?

Regards

popai
1st August 2007, 11:36
Well, you shouldn't call update every time you add a new item to the scene. The view should update by itself.

Do you have any event filters installed on the scene?

Regards

i don't have any event filters installed on the scene

marcel
1st August 2007, 11:40
Then at least call


scene->update(item.last()->boundingRect());


There is no need to update the entire scene. Just the the new item.

Regards

popai
1st August 2007, 13:43
Then at least call


scene->update(item.last()->boundingRect());


There is no need to update the entire scene. Just the the new item.

Regards

Don't work,

marcel
1st August 2007, 13:49
What about this:


QRect bbox = item.last()->boundingBox();
QPointF topLeft = item.last()->mapToScene(bbox.topLeft());
QPointF bottomRight = item.last()->mapToScene(bbox.bottomRight());
QRect mappedBBox(topLeft, bottomRight);
scene->update(mappedBBox);


It is just a matter of passing the correct bounding box.
Updating just the affected area in the view is better and faster, because once your scene gets very loaded, updating the entire scene will get very slow.

Regards

popai
1st August 2007, 14:13
What about this:


QRect bbox = item.last()->boundingBox();
QPointF topLeft = item.last()->mapToScene(bbox.topLeft());
QPointF bottomRight = item.last()->mapToScene(bbox.bottomRight());
QRect mappedBBox(topLeft, bottomRight);
scene->update(mappedBBox);


It is just a matter of passing the correct bounding box.
Updating just the affected area in the view is better and faster, because once your scene gets very loaded, updating the entire scene will get very slow.

Regards


QRectF bbox = item.last()->boundingRect(); ?
QPointF topLeft = item.last()->mapToScene(bbox.topLeft());
QPointF bottomRight = item.last()->mapToScene(bbox.bottomRight());
QRect mappedBBox(topLeft, bottomRight);
not compile
errors:

no matching function for call to `QPoint::QPoint(QPointF&)'
candidates are: QPoint::QPoint(const QPoint&)

.....

marcel
1st August 2007, 14:20
Repace this:


QRect mappedBBox(topLeft, bottomRight);


with:


QRectF mappedBBox(topLeft, bottomRight);


Regards

popai
1st August 2007, 14:32
Repace this:


QRect mappedBBox(topLeft, bottomRight);


with:


QRectF mappedBBox(topLeft, bottomRight);


Regards
I did and

Errors:
no matching function for call to `QRectF::QRectF(QPointF&, QPointF&)'
candidates are: QRectF::QRectF(const QRectF&)
....

QRectF::QRectF(const QPointF&, const QSizeF&)

...

marcel
1st August 2007, 14:37
That constructor was introduced in 4.3. I'm guessing you're using an older version.
Try:


QRectF mappedBBox;
mappedBBox.setTopLeft(topLeft);
mappedBBox.setBottomRight(bottomRight);



Regards

popai
1st August 2007, 14:41
Repace this:


QRect mappedBBox(topLeft, bottomRight);


with:


QRectF mappedBBox(topLeft, bottomRight);


Regards

I did end and not compaile
Error:
no matching function for call to `QRectF::QRectF(QPointF&, QPointF&)'
candidates are: QRectF::QRectF(const QRectF&)
....
QRectF::QRectF(const QPointF&, const QSizeF&)

....

popai
1st August 2007, 14:48
That constructor was introduced in 4.3. I'm guessing you're using an older version.
Try:


QRectF mappedBBox;
mappedBBox.setTopLeft(topLeft);
mappedBBox.setBottomRight(bottomRight);



Regards

Yes 4.2.2 :(

popai
1st August 2007, 14:56
Yes 4.2.2 :(

Thanks, neaw work