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. Trying to dynamically create objects and have them send to a slot in MainWindow.
Forum Updated to NodeBB v4.3 + New Features

Trying to dynamically create objects and have them send to a slot in MainWindow.

Scheduled Pinned Locked Moved Unsolved General and Desktop
7 Posts 3 Posters 786 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.
  • J Offline
    J Offline
    JoeJoe_000
    wrote on last edited by JoeJoe_000
    #1

    Hello,

    I've created an object lets just say it's called TestObject it's just a QFrame with a button inside of it.

    in MainWindow I can create this object and everything is good to go. I've been able to create a signal/slot in both TestObject (the emiter) and in MainWindow(the slot). In MainWindow I have code as such:

    *TestObject ObjectA = new TestObject(this, Qpoint(20, 20));

    This creates a new TestObject and is visible in MainWindow.

    I've then have a function which sets text in a label.

    void MainWindow::RxText()
    {
    label->setText("TestObject button signal Rx'd");
    }

    I then can change that text via a call to connect as such

    connect(ObjectA, &TestObject::TxText, this, MainWindow::RxText); // (This is in the MainWindow Constructor)

    This works beautifully. However my next step I would like to do is create a line of code that will respond to the signal of any button from any TestObject I create.

    for instance something like this would be handy.

    connect(TestObject <anytestobject>, &TestObject::TxText, this, MainWIndow::RxText);
    Would anyone know of a good way to do this? I can't seem to figure this part out.

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi and welcome to devnet,

      You can build a list of these objects and then use a loop to connect each of them to the slot you want.

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      2
      • dheerendraD Offline
        dheerendraD Offline
        dheerendra
        Qt Champions 2022
        wrote on last edited by
        #3

        connect(ObjectA, &TestObject::TxText, this, MainWindow::RxText);

        What is the meaning of anytestobject ? Are you creating the multiple objects ? if yes, are you strong them somewhere ? You have already connected the single object. Signal/slot is object to object. Connect statement takes individual object as argument. You make connect the moment object is created or build list of object, iterate through & then connect as @SGaist suggested.

        if this is not the case, you need to tell what do you mean by anytestobject.

        Dheerendra
        @Community Service
        Certified Qt Specialist
        http://www.pthinks.com

        J 1 Reply Last reply
        0
        • J Offline
          J Offline
          JoeJoe_000
          wrote on last edited by
          #4

          Thank you SGaist and Dheerendra - that was sufficent info. Still getting the hang of C++.

          1 Reply Last reply
          0
          • dheerendraD dheerendra

            connect(ObjectA, &TestObject::TxText, this, MainWindow::RxText);

            What is the meaning of anytestobject ? Are you creating the multiple objects ? if yes, are you strong them somewhere ? You have already connected the single object. Signal/slot is object to object. Connect statement takes individual object as argument. You make connect the moment object is created or build list of object, iterate through & then connect as @SGaist suggested.

            if this is not the case, you need to tell what do you mean by anytestobject.

            J Offline
            J Offline
            JoeJoe_000
            wrote on last edited by
            #5

            @dheerendra Hi I'm just toying with Object creation with GUI objects/QObjects.

            With QObjects the main thing I've learned here is that you need to use a pointers when using a QList or anyother container.

            QList<TestObject> TObjectList;*

            1 Reply Last reply
            0
            • dheerendraD Offline
              dheerendraD Offline
              dheerendra
              Qt Champions 2022
              wrote on last edited by dheerendra
              #6

              Any qobject type instances to be stored in qlist shoud be dynamically created objects only. Data types in containers should be assignable I.e they shud have copy constructor and assignment operator. Both are disabled for qobject types. Hence objects should be pointer types.

              Dheerendra
              @Community Service
              Certified Qt Specialist
              http://www.pthinks.com

              1 Reply Last reply
              2
              • J Offline
                J Offline
                JoeJoe_000
                wrote on last edited by
                #7

                Dheerendra, I did not know this, thank you very much. I'm still very much a noob to C++, Qt and programming in general. Thanks again!

                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