From 5d4f0b97404d96e250eb745bcece741ac3ca0218 Mon Sep 17 00:00:00 2001 From: JackLee <809262979@qq.com> Date: Wed, 5 Mar 2025 14:59:47 +0800 Subject: [PATCH] remove --- src/ctai_history_textedit.cpp | 3 +- src/ctai_history_widget.cpp | 62 ++++++++++++++++++++++++++++------- src/ctai_history_widget.h | 1 - 3 files changed, 52 insertions(+), 14 deletions(-) diff --git a/src/ctai_history_textedit.cpp b/src/ctai_history_textedit.cpp index 9f5c468..7ee1825 100644 --- a/src/ctai_history_textedit.cpp +++ b/src/ctai_history_textedit.cpp @@ -7,6 +7,7 @@ ctai_history_textedit::ctai_history_textedit(QWidget *parent) } ctai_history_textedit::~ctai_history_textedit() { + } void ctai_history_textedit::init_layout(msg_type msg_type_mode) { @@ -262,7 +263,7 @@ void ctai_history_textedit::add_user_message(const model_data &message) void ctai_history_textedit::add_system_message(const model_data &message) { QString disp_data; - m_msg_sned_id = QSL(message.send_user_id); + m_msg_sned_id = QSL(message.postback_send_id); std::lock_guard lock(m_mutex); if (message.postback_stream_mode) diff --git a/src/ctai_history_widget.cpp b/src/ctai_history_widget.cpp index 229836d..8c3c59d 100644 --- a/src/ctai_history_widget.cpp +++ b/src/ctai_history_widget.cpp @@ -133,21 +133,59 @@ void ctai_history_widget::on_rows_height_changed(bool fold_state) void ctai_history_widget::on_msg_remove(QString send_id) { - int row = this->rowCount(); - if (row >= 0) + // 添加更多调试信息 + qDebug() << "Removing message with send_id:" << send_id; + qDebug() << "Current row count:" << rowCount(); + + auto it = message_map_.find(send_id); + if (it != message_map_.end()) { - // 从map中删除 - qDebug() << "remove_row:" << row; - auto it = message_map_.find(send_id); - if (it != message_map_.end()) + int row_to_remove = it->second.second; + qDebug() << "it row count:" << it->second.second; + // 验证行号是否有效 + if (row_to_remove < 0 || row_to_remove >= rowCount()) { - qDebug() << "remove it:" << it->second.first; - delete it->second.first; // 删除model_data - message_map_.erase(it); + qDebug() << "Invalid row index:" << row_to_remove; + return; } - qDebug() << "current_row:" << this->currentRow(); - // 移除并删除该项 - removeRow(this->currentRow()); + + // 1. 先处理cell widget + QWidget *widget = cellWidget(row_to_remove, 0); + if (widget) + { + // 断开该widget的所有信号连接 + widget->disconnect(); + // 从表格中移除widget + removeCellWidget(row_to_remove, 0); + // 标记widget待删除 + widget->deleteLater(); + } + + // 2. 删除model数据 + if (it->second.first) + { + delete it->second.first; + it->second.first = nullptr; + } + + // 3. 从map中移除 + message_map_.erase(it); + + // 4. 最后删除行 + blockSignals(true); // 暂时阻止信号 + removeRow(row_to_remove); + blockSignals(false); // 恢复信号 + + // 5. 更新其他行的索引 + for (auto &pair : message_map_) + { + if (pair.second.second > row_to_remove) + { + pair.second.second--; + } + } + + qDebug() << "Row removed successfully. New row count:" << rowCount(); } } diff --git a/src/ctai_history_widget.h b/src/ctai_history_widget.h index bec63b1..8a8b797 100644 --- a/src/ctai_history_widget.h +++ b/src/ctai_history_widget.h @@ -18,7 +18,6 @@ public: ctai_history_textedit *current_message() const; private: std::map> message_map_; // int存储行号 - QString last_send_id_; void setup_table(); void update_stored_data(model_data* stored_data, const model_data& message);