问题
在QDialog
被关闭并析构时,连接的信号和槽会被自动断开。但是,Lambda表达式槽是一个匿名函数,无法直接断开连接。
所以当Lambda槽函数有调用this时,QDialog关闭后触发
Lambda中槽函数,由this已经被析构则会引发崩溃。
解决办法
- 不使用Lambda
- 在析构时断开Lambda连接
QList<QMetaObject::Connection> m_connections;
m_connections.append(connect(senderObject1, &SenderClass::someSignal, receiverObject, &ReceiverClass::someSlot));
m_connections.append(connect(senderObject2, &SenderClass::anotherSignal, receiverObject, &ReceiverClass::anotherSlot));
// 析构函数
AboutDialog::~AboutDialog()
{
// 在适当的时机断开连接
foreach (const QMetaObject::Connection &connection, m_connections) {
disconnect(connection);
}
}