Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. [SOLVED] Signals and Slots for dynamically created instances of classes
Forum Updated to NodeBB v4.3 + New Features

[SOLVED] Signals and Slots for dynamically created instances of classes

Scheduled Pinned Locked Moved General and Desktop
5 Posts 3 Posters 6.0k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • R Offline
    R Offline
    rizoritis
    wrote on last edited by
    #1

    Hello,

    I have two classes that I create instances of during run time. I am trying to connect a signal and a slot of the two instances, however I am not sure how to do this because upon initialization the instances point to nothing and only get created somewhere in the middle of my application running. I'm not sure how to connect the two signals and slots unless they are created before I call the connect() function.

    I don;t know if it matters, but this is the line I use:

    @connect(directory_Widget, SIGNAL(editProgramSignal()), editProgram_Widget, SLOT(editProgramSlot()));@

    The problem is that directoty_Widget and editProgram_Widget (which are pointer) are NULL at initialization and only get point to memory after a equence of things happen from the user upon run time. Any ideas on how to connect the signals and slots in this fashion??

    Thanks!

    1 Reply Last reply
    0
    • K Offline
      K Offline
      koahnig
      wrote on last edited by
      #2

      If the instances of the objects are not existing you cannot connect them. You need to create the instances of the instances before connecting.

      See this example from the "documentation of signals and slots":http://qt-project.org/doc/qt-4.8/signalsandslots.html
      @
      Counter a, b;
      QObject::connect(&a, SIGNAL(valueChanged(int)),
      &b, SLOT(setValue(int)));

       a.setValue(12);     // a.value() == 12, b.value() == 12
       b.setValue(48);     // a.value() == 12, b.value() == 48
      

      @

      There you see that you have to connect the 'addresses' it is not important this is a pointer.

      Vote the answer(s) that helped you to solve your issue(s)

      1 Reply Last reply
      0
      • D Offline
        D Offline
        dbzhang800
        wrote on last edited by
        #3

        I can not understand why not call connect just after both your directory_Widget and editProgram_Widget have been created.

        1 Reply Last reply
        0
        • R Offline
          R Offline
          rizoritis
          wrote on last edited by
          #4

          Because I did not allocate an address space for the widgets until runtime. I guess there is no way around this and I should allocate them upon compiling. I just had a lot and wanted to do this through a simple function upon runtime. I just used the brute force method and got it to work though.

          1 Reply Last reply
          0
          • K Offline
            K Offline
            koahnig
            wrote on last edited by
            #5

            Wondering what you are considering as brute force.
            It is the typical practice to allocate memory for class instance. Afterwards ensure that the signals send by your widget are received somewhere. The same is with receiver of signals. You ensure that you are receiving the signals you required from somewhere.

            Have a closer look to the example I have provided above. It is from the signal and slot documentation and it explains in short what is happening.
            Since the signal is sent from "a" to "b" and not the reverse, only values set in "a" will be automatically copied to "b". See also that this is the same class definition.
            You can connect 10 or 100 instances (e.g. b00 up to b99) to "a". As soon as you assign a value to "a" it will be automatically copied to "b"s. However, the assignment to any of "b"s will not affect the contents of the others.
            You have to provide the addresses of the instances for connecting. If the address happens to be in a pointer that is fine. The address is copied from the pointer. Not the address of the pointer.

            Vote the answer(s) that helped you to solve your issue(s)

            1 Reply Last reply
            0

            • Login

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • Users
            • Groups
            • Search
            • Get Qt Extensions
            • Unsolved