简体   繁体   中英

Connect signals and slots of different classes in Qt

In Qt, my MainController class creates a ViewController and a NetworkController in it's constructor.

After creation I need to connect the QPushButton::clicked signal to the onStartListening slot in the NetworkController class.

Connection code:

connect(m_viewController->getStartWindow()->getStartListeningButton(),
&QPushButton::clicked, m_networkController, &NetworkController::onStartListening);

When I try to compile my code, I get the following error:

Unhandled exception at 0x66D641BA (Qt5Cored.dll) in bla.exe: 0xC0000005: Access violation reading location 0xCDCDCDD1.

What am I doing wrong?

One technique is to get the object inside of connect() and connect the signal of the object to the slot. In your case you are getting the button of start window of viewController.

Another technique is to create a signal in the class and connect this signal to the slot in the parent class. It required a bit more code, but then you don't need to care if object is created or not, because if object is Null then no signal will be emitted.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM