Results 1 to 17 of 17

Thread: signal/slot

  1. #1
    Join Date
    Jul 2007
    Location
    Romania, Bucarest
    Posts
    12
    Thanks
    3
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default signal/slot

    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.

  2. #2
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Thanks
    8
    Thanked 541 Times in 521 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: signal/slot

    This is because the "view" object is uninitialized at that point.
    So instantiate the view first, and then connect signals to its slots.

    Regards

  3. The following user says thank you to marcel for this useful post:

    popai (31st July 2007)

  4. #3
    Join Date
    Jul 2007
    Location
    Romania, Bucarest
    Posts
    12
    Thanks
    3
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: signal/slot

    Quote Originally Posted by marcel View Post
    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 ?
    Last edited by popai; 31st July 2007 at 18:36.

  5. #4
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Thanks
    8
    Thanked 541 Times in 521 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: signal/slot

    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

  6. #5
    Join Date
    Jul 2007
    Location
    Romania, Bucarest
    Posts
    12
    Thanks
    3
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: signal/slot

    Quote Originally Posted by marcel View Post
    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
    }
    Last edited by popai; 1st August 2007 at 08:24.

  7. #6
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Thanks
    8
    Thanked 541 Times in 521 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: signal/slot

    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

  8. #7
    Join Date
    Jul 2007
    Location
    Romania, Bucarest
    Posts
    12
    Thanks
    3
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: signal/slot

    Quote Originally Posted by marcel View Post
    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

  9. #8
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Thanks
    8
    Thanked 541 Times in 521 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: signal/slot

    Then at least call
    Qt Code:
    1. scene->update(item.last()->boundingRect());
    To copy to clipboard, switch view to plain text mode 

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

    Regards

  10. The following user says thank you to marcel for this useful post:

    popai (1st August 2007)

  11. #9
    Join Date
    Jul 2007
    Location
    Romania, Bucarest
    Posts
    12
    Thanks
    3
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: signal/slot

    Quote Originally Posted by marcel View Post
    Then at least call
    Qt Code:
    1. scene->update(item.last()->boundingRect());
    To copy to clipboard, switch view to plain text mode 

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

    Regards
    Don't work,

  12. #10
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Thanks
    8
    Thanked 541 Times in 521 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: signal/slot

    What about this:
    Qt Code:
    1. QRect bbox = item.last()->boundingBox();
    2. QPointF topLeft = item.last()->mapToScene(bbox.topLeft());
    3. QPointF bottomRight = item.last()->mapToScene(bbox.bottomRight());
    4. QRect mappedBBox(topLeft, bottomRight);
    5. scene->update(mappedBBox);
    To copy to clipboard, switch view to plain text mode 

    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

  13. #11
    Join Date
    Jul 2007
    Location
    Romania, Bucarest
    Posts
    12
    Thanks
    3
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: signal/slot

    Quote Originally Posted by marcel View Post
    What about this:
    Qt Code:
    1. QRect bbox = item.last()->boundingBox();
    2. QPointF topLeft = item.last()->mapToScene(bbox.topLeft());
    3. QPointF bottomRight = item.last()->mapToScene(bbox.bottomRight());
    4. QRect mappedBBox(topLeft, bottomRight);
    5. scene->update(mappedBBox);
    To copy to clipboard, switch view to plain text mode 

    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&)

    .....

  14. #12
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Thanks
    8
    Thanked 541 Times in 521 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: signal/slot

    Repace this:
    Qt Code:
    1. QRect mappedBBox(topLeft, bottomRight);
    To copy to clipboard, switch view to plain text mode 

    with:
    Qt Code:
    1. QRectF mappedBBox(topLeft, bottomRight);
    To copy to clipboard, switch view to plain text mode 

    Regards

  15. #13
    Join Date
    Jul 2007
    Location
    Romania, Bucarest
    Posts
    12
    Thanks
    3
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: signal/slot

    Quote Originally Posted by marcel View Post
    Repace this:
    Qt Code:
    1. QRect mappedBBox(topLeft, bottomRight);
    To copy to clipboard, switch view to plain text mode 

    with:
    Qt Code:
    1. QRectF mappedBBox(topLeft, bottomRight);
    To copy to clipboard, switch view to plain text mode 

    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&)

    ...

  16. #14
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Thanks
    8
    Thanked 541 Times in 521 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: signal/slot

    That constructor was introduced in 4.3. I'm guessing you're using an older version.
    Try:
    Qt Code:
    1. QRectF mappedBBox;
    2. mappedBBox.setTopLeft(topLeft);
    3. mappedBBox.setBottomRight(bottomRight);
    To copy to clipboard, switch view to plain text mode 


    Regards

  17. The following user says thank you to marcel for this useful post:

    popai (1st August 2007)

  18. #15
    Join Date
    Jul 2007
    Location
    Romania, Bucarest
    Posts
    12
    Thanks
    3
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: signal/slot

    Quote Originally Posted by marcel View Post
    Repace this:
    Qt Code:
    1. QRect mappedBBox(topLeft, bottomRight);
    To copy to clipboard, switch view to plain text mode 

    with:
    Qt Code:
    1. QRectF mappedBBox(topLeft, bottomRight);
    To copy to clipboard, switch view to plain text mode 

    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&)

    ....

  19. #16
    Join Date
    Jul 2007
    Location
    Romania, Bucarest
    Posts
    12
    Thanks
    3
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: signal/slot

    Quote Originally Posted by marcel View Post
    That constructor was introduced in 4.3. I'm guessing you're using an older version.
    Try:
    Qt Code:
    1. QRectF mappedBBox;
    2. mappedBBox.setTopLeft(topLeft);
    3. mappedBBox.setBottomRight(bottomRight);
    To copy to clipboard, switch view to plain text mode 


    Regards
    Yes 4.2.2

  20. #17
    Join Date
    Jul 2007
    Location
    Romania, Bucarest
    Posts
    12
    Thanks
    3
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: signal/slot

    Quote Originally Posted by popai View Post
    Yes 4.2.2
    Thanks, neaw work

Similar Threads

  1. What argument types for Signal/Slot?
    By DPinLV in forum Qt Programming
    Replies: 5
    Last Post: 2nd August 2006, 20:08
  2. What if not Signal/Slot
    By munna in forum General Discussion
    Replies: 2
    Last Post: 26th May 2006, 07:48
  3. Signal/slot connections of deleted items
    By Michiel in forum Newbie
    Replies: 2
    Last Post: 24th March 2006, 17:44
  4. Replies: 4
    Last Post: 23rd January 2006, 17:51
  5. What's the relationship between signal/slot and event?
    By twosnowman in forum Qt Programming
    Replies: 4
    Last Post: 11th January 2006, 18:13

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.