主窗口信号治理
mainwindow.cpp · L145–L352
MainWindow::bindLogicSignals
按登录态、好友、群组、通话和更新等领域集中绑定 ClientLogic 信号。
原型
void MainWindow::bindLogicSignals()调用时机
MainWindow 完成页面构建后调用。
返回说明
无返回值。
参数
无显式参数。
执行流程
- 校验 logic 对象
- 绑定强制退出
- 绑定好友与通知
- 绑定群组事件
- 绑定通话事件
工程说明
集中连接降低构造函数复杂度并便于生命周期审查。
关联接口
查看完整实现
void MainWindow::bindLogicSignals()
{
if (!logic) {
return;
}
connect(logic, &ClientLogic::sigForceLogout,
this, &MainWindow::handleForceLogout);
connect(logic, &ClientLogic::sigAddFriendResult, this,
[=](bool success, QString, QString msg) {
if (success) {
QMessageBox::information(this, "提示", msg);
} else {
QMessageBox::warning(this, "添加失败", msg);
}
});
connect(logic, &ClientLogic::sigFriendListReceived, this,
[=](QJsonArray friends) {
if (!friendListWidget) {
return;
}
friendListWidget->clear();
for (int i = 0; i < friends.size(); ++i) {
QString friendId = friends[i].toString();
addFriendToList(friendId, "👤");
}
});
connect(logic, &ClientLogic::sigLoginResult, this,
[=](bool success, const QString& message) {
m_loginRequestPending = false;
if (loginBtn) {
loginBtn->setEnabled(true);
loginBtn->setText("登录");
}
if (success) {
stackedWidget->setCurrentIndex(1);
updatePersonalInfo(logic->getCurrentUser(), "👤");
logic->requestPullFriends();
logic->requestPullGroups();
logic->requestPullNotifications();
QMessageBox::information(this, "登录成功", "欢迎回来,客户端已连接到服务器。");
} else {
QMessageBox::warning(this, "登录失败", message);
}
});
connect(logic, &ClientLogic::sigRegisterResult, this,
[=](bool success, QString msg) {
m_registerRequestPending = false;
if (registerBtn) {
registerBtn->setEnabled(true);
registerBtn->setText("创建账号");
}
if (success) {
QMessageBox::information(this, "成功", msg);
} else {
QMessageBox::warning(this, "注册失败", msg);
}
});
connect(logic, &ClientLogic::sigCreateGroupResult,
this,
[=](bool success,
qulonglong groupId,
const QString& groupName,
const QString& msg) {
if (success) {
QMessageBox::information(
this,
"创建群成功",
QString("群组创建成功。\n群名:%1\n群ID:%2")
.arg(groupName)
.arg(groupId));
logic->requestPullGroups();
} else {
QMessageBox::warning(this, "创建群失败", msg);
}
});
connect(logic, &ClientLogic::sigInviteGroupMemberResult,
this,
[=](bool success, const QString& msg) {
m_pendingGroupInviteKeys.clear();
if (success) {
QMessageBox::information(this, "入群邀请", msg);
} else {
QMessageBox::warning(this, "入群邀请失败", msg);
}
if (logic) {
logic->requestPullGroups();
logic->requestPullNotifications();
}
});
connect(logic, &ClientLogic::sigGroupsReceived,
this,
[=](const QJsonArray& groups) {
refreshGroupListUI(groups);
});
connect(logic, &ClientLogic::sigGroupMembersReceived,
this,
[=](qulonglong groupId, const QJsonArray& members) {
showGroupMembersDialog(groupId, members);
});
connect(logic, &ClientLogic::sigGroupError,
this,
[=](const QString& msg) {
QMessageBox::warning(this, "群组错误", msg);
});
connect(logic, &ClientLogic::sigCallInviteReceived,
this,
&MainWindow::handleIncomingCallInvite);
connect(logic, &ClientLogic::sigCallReady,
this,
[=](const QString& peer,
const QString& callId,
const QString& mode,
const QString& callType) {
if (peer.isEmpty() || callId.isEmpty()) {
qDebug() << "[Call] invalid call_ready, peer or callId empty";
return;
}
if (mode != "relay") {
QMessageBox::warning(this,
"通话暂不可用",
"当前网络环境暂不支持该通话模式,请稍后重试。");
return;
}
openOrActivateCallWindow(peer, callId, callType);
});
connect(logic, &ClientLogic::sigCallRejected,
this,
[=](const QString& peer, const QString& callId) {
Q_UNUSED(callId);
if (!callWindowMap.contains(peer)) {
QMessageBox::information(this,
"通话结束",
QString("[%1] 拒绝了通话。").arg(peer));
}
});
connect(logic, &ClientLogic::sigCallEnded,
this,
[=](const QString& peer, const QString& callId) {
Q_UNUSED(callId);
if (!callWindowMap.contains(peer)) {
qDebug() << "[Call]" << peer
<< "ended call, but no CallWindow exists.";
}
});
}