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. Sending static signals, is it possible?
Forum Updated to NodeBB v4.3 + New Features

Sending static signals, is it possible?

Scheduled Pinned Locked Moved Solved General and Desktop
5 Posts 3 Posters 9.1k 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.
  • P Offline
    P Offline
    plaktos
    wrote on last edited by
    #1

    Is it possible to send a static signal?

    I have a list of entry widgets inside a list and I want the container to be notified if one of them gets selected.
    I don't want to connect all the entries idividually so I tought sending a static signal might work

    public signals:
            static void SelectedStateChanged();
    

    I connect it to the QCheckBox

    connect(selectedCheckBox, &QCheckBox::stateChanged,
                [this](){
            emit SelectedStateChanged();
            });
    

    Then in the list widget I connect to the static signal.
    I have tried multiple ways:

    • Qt5 syntax:
    // Connect the static signal from a dummy entry
        DeckScrollListEntry *dummyEntry = new DeckScrollListEntry(this);
        connect(dummyEntry, &DeckScrollListEntry::SelectedStateChanged,
                this, &DeckScrollList::refreshSelections);
        delete dummyEntry;
    

    Compiler error:

    error: no matching function for call to 'DeckScrollList::connect(DeckScrollListEntry*&, void (*)(), DeckScrollList*, void (DeckScrollList::*)())'
                 this, &DeckScrollList::refreshSelections);
    
    • Macro syntax:
    DeckScrollListEntry *dummyEntry = new DeckScrollListEntry(this);
        connect(dummyEntry, SIGNAL(SelectedStateChanged()),
                this, SLOT(refreshSelections));
        delete dummyEntry;
    

    Compiler error:

    error: 'this' is unavailable for static member functions
         QMetaObject::activate(this, &staticMetaObject, 1, nullptr);
                               ^
    

    Is there any way or should I redesign and make connections for every entry.

    1 Reply Last reply
    0
    • sierdzioS Offline
      sierdzioS Offline
      sierdzio
      Moderators
      wrote on last edited by
      #2

      Connections always relay on concrete object instances. It makes no sense for signal to be static, because signal has to come from a "living" object.

      You are creating the DeckScrollListEntry objects somewhere, so connect the signals there, too. Alternatively, use automated connections: connectSlotsByName(). I don't recommend it, though, because it is very easy to make a mistake here. The new connection syntax is definitely the best.

      (Z(:^

      1 Reply Last reply
      6
      • P Offline
        P Offline
        plaktos
        wrote on last edited by
        #3

        I'm not sure if you understood what I was trying to do. It wasn't really clear from what I posted.
        Tough I have since realized that it's a horrible design that would lead to all kinds of bugs down the road.
        What I was trying to do is something like this

        connect(static signal &DeckScrollListEntry::SelectedStateChanged,
                            this, &DeckScrollList::refreshSelections)
        )
        

        So it would connect to a static signal that is sent by any DeckScrollListEntry.
        So in a way it would connect to every DeckScrollListEntry.
        And I also realized this is not possible of course, and it would be horrible if it were possible.

        sierdzioS 1 Reply Last reply
        0
        • P plaktos

          I'm not sure if you understood what I was trying to do. It wasn't really clear from what I posted.
          Tough I have since realized that it's a horrible design that would lead to all kinds of bugs down the road.
          What I was trying to do is something like this

          connect(static signal &DeckScrollListEntry::SelectedStateChanged,
                              this, &DeckScrollList::refreshSelections)
          )
          

          So it would connect to a static signal that is sent by any DeckScrollListEntry.
          So in a way it would connect to every DeckScrollListEntry.
          And I also realized this is not possible of course, and it would be horrible if it were possible.

          sierdzioS Offline
          sierdzioS Offline
          sierdzio
          Moderators
          wrote on last edited by
          #4

          @plaktos said in Sending static signals, is it possible?:

          So it would connect to a static signal that is sent by any DeckScrollListEntry.

          I understood it exactly like that. It's not possible to do.

          (Z(:^

          1 Reply Last reply
          0
          • VRoninV Offline
            VRoninV Offline
            VRonin
            wrote on last edited by
            #5

            Disclaimer

            I'm not saying this is something you'd want to do

            You can use a static/singleton QObject as the source of your signal

            "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
            ~Napoleon Bonaparte

            On a crusade to banish setIndexWidget() from the holy land of Qt

            1 Reply Last reply
            2

            • Login

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