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. using another class object for signal and slots inside qt
Forum Updated to NodeBB v4.3 + New Features

using another class object for signal and slots inside qt

Scheduled Pinned Locked Moved Unsolved General and Desktop
4 Posts 2 Posters 315 Views 2 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.
  • Ramkumar MohanR Offline
    Ramkumar MohanR Offline
    Ramkumar Mohan
    wrote on last edited by
    #1

    using another class object for signal and slots inside qt?

    1 Reply Last reply
    0
    • Chris KawaC Offline
      Chris KawaC Offline
      Chris Kawa
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Sorry, but I've no idea what you're asking.
      You might want to try and rephrase the question or provide a bit more details on what you want to do.

      Ramkumar MohanR 1 Reply Last reply
      3
      • Chris KawaC Chris Kawa

        Sorry, but I've no idea what you're asking.
        You might want to try and rephrase the question or provide a bit more details on what you want to do.

        Ramkumar MohanR Offline
        Ramkumar MohanR Offline
        Ramkumar Mohan
        wrote on last edited by
        #3

        @Chris-Kawa how to access a class from another class?

        1 Reply Last reply
        0
        • Chris KawaC Offline
          Chris KawaC Offline
          Chris Kawa
          Lifetime Qt Champion
          wrote on last edited by
          #4

          Well first of all I think you mean to access an instance of a class from a method of another class. Class is a type, like an int, so instances of them can access each other, not their classes (well, unless you want to go into funky metaprogramming, but let's not complicate it).

          Anyway, to "access" an instance of a class you have to have a pointer or a reference to it. For example:

          class A { public: int x; };
          class B
          {
          public: 
             void doSomethingWithA(A& a) {  a.x = 42; }
          };
          
          A instanceOfA;
          B instanceOfB;
          instanceofB.doSomethingWithA(instanceofA);
          

          Another example is when a class has a member of another class:

          class A { public int x; };
          class B 
          { 
             A a;
          public:
             void doSomethingWithAMember() {  a.x = 42; }
          };
          
          B instanceOfB;
          instanceofB.doSomethingWithAMember();
          

          Or you can store a pointer to instance of one class in an instance of another class. for example through a constructor:

          class A { public int x; };
          class B
          {
             A* a;
          public:
             B(A* ptr) : a(ptr) {}
             void doSomethingWithAPointer() { a->x = 42; }
          };
          
          A instanceOfA;
          B instanceOfB(&instanceofA);
          instanceofB.doSomethingWithAPointer();
          

          Or you can create an instance in place:

          class A { public: int x; };
          class B
          {
          public: 
             void doSomethingWithLocalA() {  A a; a.x = 42; }
          };
          
          B instanceOfB;
          instanceofB.doSomethingWithLocalA();
          

          Qt has also a signal/slot mechanism. You can connect two instances so that when one signals something a slot of another is called:

          QPushButton button("Press me");
          QCheckBox checkbox("Hello");
          
          QObject::connect(&button, &QPushButton::clicked, &checkbox, &QCheckBox::toggle);
          

          And there's million other ways to make instances interact. You should start with what you want to do, not how, and then seek the right approach to accomplish that.

          1 Reply Last reply
          3

          • Login

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