Difference between QObject::connect() and connect()

155 views Asked by At

I am new to Qt and I want to try to understand it better.

I know from an inheritance perspective that a QMainWindow is derived from QObject. (Directly/Indirectly)

This allows for us to connect signals to slots in the following ways within the QMainWindow:

`

1- QObject::connect(sender, &QObject::signal, this, &MyObject::slot);
2- connect(sender, &QObject::signal, this, &MyObject::slot);

`

Even tough both ways are a possibility, I never understood what the major differences between them are.

Here are my questions:

1- Which method is more performant and why?

2- Why do programmers sometimes use one over the other?

I used both of these methods and they both seem to work similarly.

1

There are 1 answers

0
mugiseyebrows On

Consider following code.

class Foo { 
  static void fn(); 
};
class Bar: public Foo {
  void bar() {
    // 1
  }
};
void main() {
  // 2
}

If you want to call Foo::fn() at 1, you can just write fn(); since static functions is "visible" inside methods in derived classes, you can also write Foo::fn() and it will do exactly the same. If you want to call it at 2, you can only use full name Foo::fn().