This commit is contained in:
JackLee 2025-03-05 14:59:47 +08:00
parent 2e8d9b29e8
commit 5d4f0b9740
3 changed files with 52 additions and 14 deletions

View File

@ -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<std::mutex> lock(m_mutex);
if (message.postback_stream_mode)

View File

@ -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();
}
}

View File

@ -18,7 +18,6 @@ public:
ctai_history_textedit *current_message() const;
private:
std::map<QString, std::pair<model_data*, int>> message_map_; // int存储行号
QString last_send_id_;
void setup_table();
void update_stored_data(model_data* stored_data, const model_data& message);