Qt中连接不同类的信号和槽
Connect signals and slots of different classes in Qt
在 Qt 中,我的 MainController
class 在其构造函数中创建了一个 ViewController
和一个 NetworkController
。
创建后,我需要将 QPushButton::clicked
信号连接到 NetworkController
class 中的 onStartListening
插槽。
连接码:
connect(m_viewController->getStartWindow()->getStartListeningButton(),
&QPushButton::clicked, m_networkController, &NetworkController::onStartListening);
当我尝试编译我的代码时,出现以下错误:
Unhandled exception at 0x66D641BA (Qt5Cored.dll) in bla.exe:
0xC0000005: Access violation reading location 0xCDCDCDD1.
我做错了什么?
一种技术是在 connect() 中获取对象并将对象的信号连接到插槽。在您的情况下,您将获得 viewController 的开始 window 按钮。
另一种技术是在 class 中创建一个信号并将该信号连接到父 class 中的插槽。它需要更多的代码,但是你不需要关心是否创建了对象,因为如果对象为 Null 则不会发出任何信号。
在 Qt 中,我的 MainController
class 在其构造函数中创建了一个 ViewController
和一个 NetworkController
。
创建后,我需要将 QPushButton::clicked
信号连接到 NetworkController
class 中的 onStartListening
插槽。
连接码:
connect(m_viewController->getStartWindow()->getStartListeningButton(),
&QPushButton::clicked, m_networkController, &NetworkController::onStartListening);
当我尝试编译我的代码时,出现以下错误:
Unhandled exception at 0x66D641BA (Qt5Cored.dll) in bla.exe: 0xC0000005: Access violation reading location 0xCDCDCDD1.
我做错了什么?
一种技术是在 connect() 中获取对象并将对象的信号连接到插槽。在您的情况下,您将获得 viewController 的开始 window 按钮。
另一种技术是在 class 中创建一个信号并将该信号连接到父 class 中的插槽。它需要更多的代码,但是你不需要关心是否创建了对象,因为如果对象为 Null 则不会发出任何信号。