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. Calling slots between classes where one class is not the parent
Forum Updated to NodeBB v4.3 + New Features

Calling slots between classes where one class is not the parent

Scheduled Pinned Locked Moved General and Desktop
2 Posts 2 Posters 727 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.
  • N Offline
    N Offline
    nicky j
    wrote on last edited by
    #1

    Hello,

    I am wondering how I can call a slot from a different class when the class with the slot is not necessarily the parent. I have a bunch of classes that have parents in this order:
    mainWindow-->mainWidget-->tabWidget-->browseTab. basically mainWindow is the parent of mainWidget which is the parent of tabWidget, etc. How can I have a button in browseTab, that, when pressed, calls a slot in mainWindow. I know how to make it call a signal in its parent class (tabWidget), but I am stuck on how to make it call a slot in its 'grandparent' or 'great grandparent' classes. Thanks!

    1 Reply Last reply
    0
    • JKSHJ Offline
      JKSHJ Offline
      JKSH
      Moderators
      wrote on last edited by
      #2

      Hi,

      There are a few different ways you can do this:

      1) Chain signals together:
      @
      /*
      This example shows how to propagate a signal
      from a BrowseTab button to a MainWidget.

      It follows Qt naming conventions:
      Class names start with an uppercase letter,
      variable names start with a lowercase letter.
      

      */
      // BrowseTab constructor
      {
      this->button = new QPushButton("Special Button");
      connect(button, SIGNAL(clicked()),
      this, SIGNAL(buttonClicked()));
      }

      // TabWidget constructor
      {
      this->browseTab = new BrowseTab(this);
      connect(browseTab, SIGNAL(buttonclicked()),
      this, SIGNAL(browseTabButtonClicked()));
      }

      // MainWidget constructor
      {
      this->tabWidget = new TabWidget(this);
      connect(tabWidget, SIGNAL(browseTabButtonclicked()),
      this, SLOT(doStuff()));
      }
      @

      2) Find a way to pass the pointer of the button up to the MainWidget. Then, call QObject::connect() in the MainWidget to connect the button's signal to the MainWidget's slot.

      Qt Doc Search for browsers: forum.qt.io/topic/35616/web-browser-extension-for-improved-doc-searches

      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