Results 1 to 7 of 7

Thread: Problems with signals and slots [probably c++ issue]

  1. #1
    Join Date
    Apr 2012
    Posts
    8
    Qt products
    Qt4
    Platforms
    Windows

    Default Problems with signals and slots [probably c++ issue]

    Hi guys, I am making and app to allow users to make some simple network diagrams. The app will allow users to draw simple lines and drag and drop devices.
    I have reimplemented the following classes: myScene, myView, myCable, myLine and myRouter. I notice that I mostly instantiate my other objects in myScene and the GUI interacts mostly with myScene. I was wondering if I am doing it wrong without having any inheritance as I frequently encounter situation where I need to connect signal and slot from my other classes to the mainwindow. It got me thinking if my other classes should inherits from myScene class.

    E.g. I reimplemented the mouseDoubleClicked() in myCable which is a subclass of QGraphicsLineItem, I wanted it to emit a signal to update a QTreeWidget but I it has not been instantiated and I could not do it. I only instantiate it in myScene class.

    Thank you all in advance for any advices.

    P.S. I apologise in advance if the question is too simple, I am new to C++ and also QT Programming.
    Last edited by Nayrb; 26th April 2012 at 07:33.

  2. #2
    Join Date
    Dec 2008
    Location
    Istanbul, TURKEY
    Posts
    537
    Thanks
    14
    Thanked 13 Times in 13 Posts
    Qt products
    Qt4
    Platforms
    Windows Android

    Default Re: Problems with signals and slots [probably c++ issue]

    What is the thing that's not instantiated? Is it your "myCable" class?

    If so, please post the code where you think you instantiate the myCable object..

    You also need to use setScene() for your myCable item in order to receive mouse events.
    Last edited by zgulser; 26th April 2012 at 13:48.

  3. #3
    Join Date
    Apr 2012
    Posts
    11
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Problems with signals and slots [probably c++ issue]

    I would not dare to say that I fully understand your problem, but from what I do understand I would say that this more a design issue than a Qt or C++ issue. Before one starts to implement it is always advisable to thoroughly think about the possible designs, the pros and cons of each alternative, and then find the best solution. I understand that this is no answer to your problem, but maybe a tip for future projects.

    Now, in order for us to help you I think you need to elaborate a bit more. Which object is not yet instantiated? The QTreeWidget? And at what time *will* it be instantiated?

  4. #4
    Join Date
    Apr 2012
    Posts
    8
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Problems with signals and slots [probably c++ issue]

    First of all, I did most of the UIs using the Qt Designer. Secondly, two of my classes myScene and myView were instantiated in MainWindow classes and the signals and slots are mostly connected here in MainWindow constructor. The other classes like myCable and myLine etc are only instantiated within myScene class and I notice I could not connect them to the widgets created using the Qt Designer or calling any functions from myScene. I think its a design problem and I hope someone can give me some advise on it.

  5. #5
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Problems with signals and slots [probably c++ issue]

    Quote Originally Posted by Nayrb View Post
    E.g. I reimplemented the mouseDoubleClicked() in myCable which is a subclass of QGraphicsLineItem,
    There is no function mouseDoubleClicked() to reimplement. Perhaps you mean QGraphicsItem::mouseDoubleClickEvent(). Having that function emit a signal is trivial if your item also inherits from QObject somehow. Emitting this signal does not depend on knowing anything about the receiver, which is the entire point of signal/slot loose coupling.

    I wanted it to emit a signal to update a QTreeWidget but I it has not been instantiated and I could not do it. I only instantiate it in myScene class.
    What is "it"?
    • First "it": The graphics item?
    • Second "it": Some other object? Perhaps a desired receiver of the signal from the first "it"?
    • Third "it": The sending of a signal or the connection of a signal to a slot?
    • Fourth "it": Some other object possibly related to the second "it"?

    Clearly you cannot emit a signal without having instantiated an object, so the first"it" and the second and fourth "it" are not the same object. You need instance of both the sender and receiver in order to form a signal/slot connection. Any place in your code that both objects exist is a candidate for placing a connect() call.

    P.S. I apologise in advance if the question is too simple, I am new to C++ and also QT Programming.
    The question is not too simple just too vague.

  6. #6
    Join Date
    Apr 2012
    Posts
    8
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Problems with signals and slots [probably c++ issue]

    I apologise for being too vague.

    The function is the QGraphicsScene::mouseDoubleClickEvent().

    1) The first "it" is the object, I wanted to update the QTreeWidget whenever the object is being double-clicked.
    2) The second "it" refers to the same object which has not been instantiated.
    3) The third "it" refers to the connection that I wanted to connect between the myCable object and myScene object, however I only instantiate the myCable object in one of myScene methods.
    4) The fourth "it" refers to the myCable object which I have instantiate in one of the myScene methods.

    E.g. myScene.cpp
    void myScene::myCableToolsDrawSlot(int x, int y, int x2, int y2)
    {
    myCable cable = new myCable();
    ...
    ...
    }

  7. #7
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Problems with signals and slots [probably c++ issue]

    If "the object" (presumably intended to be a QGraphicsItem) has not been instantiated then it cannot be double-clicked. Your description is not doing you any favours.

    If you have implemented a double-click handler on the scene then you can determine the scene objects that are at the double-click position with QGraphicsScene::itemAt() or QGraphicsScene::items(). Your scene can emit any signal it likes to update a QTableWidget. There is no need for a connection between an item in the scene and the scene (graphics items are not QObjects and cannot emit signals anyway). There's no need for a connection between the graphics item and and external table widget.

    When your scene creates a new item the scene can tell the table widget about it if need be.

Similar Threads

  1. Problems with signals and slots
    By zzz9 in forum Qt Programming
    Replies: 3
    Last Post: 26th September 2011, 09:52
  2. Signals and Slots
    By Maluko_Da_Tola in forum Newbie
    Replies: 3
    Last Post: 30th July 2010, 00:57
  3. Signals and Slots
    By 83.manish in forum Qt Programming
    Replies: 3
    Last Post: 30th June 2008, 11:31
  4. regarding signals/slots
    By jjbabu in forum Qt Programming
    Replies: 2
    Last Post: 4th October 2007, 10:32
  5. Signals and Slots
    By merry in forum Qt Programming
    Replies: 4
    Last Post: 22nd February 2007, 09:11

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.