解决自适应问题

This commit is contained in:
JackLee 2025-03-04 21:02:39 +08:00
parent 6a16887dd8
commit 966a57302c
7 changed files with 69 additions and 66 deletions

View File

@ -24,7 +24,7 @@ size_t stream_callback(void *buffer, size_t sz, size_t nmemb, void *userdata)
{ {
ctx->instance->send_stream(ctx->m_data, chunk); ctx->instance->send_stream(ctx->m_data, chunk);
} }
qDebug() << "steam data chunk:" << chunk; //qDebug() << "steam data chunk:" << chunk;
return sz * nmemb; return sz * nmemb;
} }
// 非流式输出回调 // 非流式输出回调
@ -33,7 +33,7 @@ size_t not_stream_callback(void *buffer, size_t sz, size_t nmemb, void *userdata
std::string *psResponse = static_cast<std::string *>(userdata); std::string *psResponse = static_cast<std::string *>(userdata);
size_t size = sz * nmemb; size_t size = sz * nmemb;
psResponse->append(static_cast<char *>(buffer), size); psResponse->append(static_cast<char *>(buffer), size);
qDebug() << "not steam data chunk:" << *psResponse; //qDebug() << "not steam data chunk:" << *psResponse;
return sz * nmemb; return sz * nmemb;
} }
void ctai_curl::set_send_post_headers(std::string head_str) void ctai_curl::set_send_post_headers(std::string head_str)
@ -123,7 +123,6 @@ std::vector<std::string> ctai_curl::steam_extract(const std::string &input)
// 提取捕获组中的内容 // 提取捕获组中的内容
result.push_back(match.str(1)); result.push_back(match.str(1));
} }
qDebug() << "result:" << result;
return result; return result;
} }
std::string ctai_curl::send_timestamp_to_time(time_t timestamp) std::string ctai_curl::send_timestamp_to_time(time_t timestamp)
@ -150,14 +149,12 @@ void ctai_curl::send_stream(model_data &data, std::string response_data)
send_not_stream(data, str); send_not_stream(data, str);
emit send_post_out_data(data); emit send_post_out_data(data);
} }
qDebug() << "send_stream:" << str;
} }
} }
} }
void ctai_curl::send_not_stream(model_data &data, std::string response_data) void ctai_curl::send_not_stream(model_data &data, std::string response_data)
{ {
json response = json::parse(response_data); json response = json::parse(response_data);
qDebug()<<"msg_type_mode:"<<data.msg_type_mode;
if (data.postback_stream_mode) if (data.postback_stream_mode)
{ {
if (response.contains("choices") && !response["choices"].empty()) if (response.contains("choices") && !response["choices"].empty())
@ -224,7 +221,7 @@ std::string ctai_curl::send_send_body(model_data m_data)
{"messages", {{{"role", "user"}, {"content", m_data.send_user_data}}}}, {"messages", {{{"role", "user"}, {"content", m_data.send_user_data}}}},
{"temperature", 0.7}, {"temperature", 0.7},
{"stream", m_data.postback_stream_mode}}; {"stream", m_data.postback_stream_mode}};
qDebug() << "request_body_str:" << _request_body.dump(); qDebug() << "request_body_str:" << _request_body.dump(4);
return _request_body.dump(); return _request_body.dump();
} }
void ctai_curl::set_send_post_option() void ctai_curl::set_send_post_option()

View File

@ -3,6 +3,7 @@
ctai_history_textedit::ctai_history_textedit(QWidget *parent) ctai_history_textedit::ctai_history_textedit(QWidget *parent)
: QWidget(parent) : QWidget(parent)
{ {
setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);
} }
ctai_history_textedit::~ctai_history_textedit() ctai_history_textedit::~ctai_history_textedit()
{ {
@ -92,9 +93,6 @@ void ctai_history_textedit::init_layout(msg_type msg_type_mode)
m_msg_history->setAcceptRichText(true); m_msg_history->setAcceptRichText(true);
m_msg_history->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff); m_msg_history->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
m_msg_history->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed); m_msg_history->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);
// 添加性能优化设置
m_msg_history->setUndoRedoEnabled(false); // 禁用撤销重做
m_msg_history->setTextInteractionFlags(Qt::TextSelectableByMouse | Qt::TextSelectableByKeyboard);
m_msg_history->setObjectName("m_msg_history"); m_msg_history->setObjectName("m_msg_history");
m_msg_history->setReadOnly(true); m_msg_history->setReadOnly(true);
historyLayout->addWidget(m_msg_history); historyLayout->addWidget(m_msg_history);
@ -206,7 +204,7 @@ void ctai_history_textedit::on_fold_clicked()
m_msg_fold->setIcon(QIcon(m_is_folded ? ":res/img/btn/btn_info_down.png" : ":res/img/btn/btn_info_up.png")); m_msg_fold->setIcon(QIcon(m_is_folded ? ":res/img/btn/btn_info_down.png" : ":res/img/btn/btn_info_up.png"));
updateGeometry(); updateGeometry();
emit height_changed(); emit fold_row_height_changed();
} }
void ctai_history_textedit::on_text_height() void ctai_history_textedit::on_text_height()
{ {
@ -232,7 +230,7 @@ void ctai_history_textedit::on_text_height()
static const int HEIGHT_THRESHOLD = 5; // 5像素的阈值 static const int HEIGHT_THRESHOLD = 5; // 5像素的阈值
if (abs(m_msg_history->height() - newHeight) > HEIGHT_THRESHOLD) if (abs(m_msg_history->height() - newHeight) > HEIGHT_THRESHOLD)
{ {
m_original_height = newHeight; m_original_height = newHeight+15;
// 批量更新布局 // 批量更新布局
bool wasBlocked = m_msg_history->signalsBlocked(); bool wasBlocked = m_msg_history->signalsBlocked();
@ -242,7 +240,7 @@ void ctai_history_textedit::on_text_height()
updateGeometry(); updateGeometry();
m_msg_history->blockSignals(wasBlocked); m_msg_history->blockSignals(wasBlocked);
emit height_changed(); emit current_row_height_changed();
// 延迟发送布局更新请求 // 延迟发送布局更新请求
if (QWidget *parent = parentWidget()) if (QWidget *parent = parentWidget())

View File

@ -67,7 +67,8 @@ private slots:
void on_text_height(); void on_text_height();
signals: signals:
void delete_requested(QString); // 请求删除此消息 void delete_requested(QString); // 请求删除此消息
void height_changed(); // 新增高度变化信号 void current_row_height_changed();
void fold_row_height_changed();
private: private:
void connect_signals(msg_type msg_type_mode); // 连接信号和槽 void connect_signals(msg_type msg_type_mode); // 连接信号和槽
private: private:

View File

@ -96,24 +96,30 @@ void ctai_history_widget::add_message(const model_data &message)
// 连接删除信号 // 连接删除信号
connect(message_widget, SIGNAL(delete_requested(QString)), this, SLOT(on_msg_remove(QString))); connect(message_widget, SIGNAL(delete_requested(QString)), this, SLOT(on_msg_remove(QString)));
// 连接高度变化信号 // 连接高度变化信号
connect(message_widget, &ctai_history_textedit::height_changed, connect(message_widget, &ctai_history_textedit::current_row_height_changed,this, &ctai_history_widget::on_current_rows_height_changed);
this, &ctai_history_widget::on_widget_height_changed); connect(message_widget, &ctai_history_textedit::fold_row_height_changed,this, &ctai_history_widget::on_rows_height_changed);
// 滚动到新消息 // 滚动到新消息
scrollToItem(item(row, 0)); scrollToItem(item(row, 0));
} }
// 立即滚动到底部 // 立即滚动到底部
scrollToBottom(); scrollToBottom();
} }
void ctai_history_widget::on_widget_height_changed() //适合信息折叠
void ctai_history_widget::on_rows_height_changed()
{ {
int row = currentRow(); //全部行高自适应
if (row >= 0 && row < rowCount()) { for (int row = 0; row < rowCount(); ++row) {
int currentHeight = rowHeight(row);
setRowHeight(row, currentHeight + 100);
resizeRowToContents(row); resizeRowToContents(row);
} }
scrollToBottom(); scrollToBottom();
} }
//适合自适应流式输出内容
void ctai_history_widget::on_current_rows_height_changed()
{
// 当前行高自适应
resizeRowToContents(rowCount());
scrollToBottom();
}
void ctai_history_widget::on_msg_remove(QString send_id) void ctai_history_widget::on_msg_remove(QString send_id)
{ {
int row = this->rowCount(); int row = this->rowCount();

View File

@ -24,6 +24,7 @@ private:
void update_stored_data(model_data* stored_data, const model_data& message); void update_stored_data(model_data* stored_data, const model_data& message);
private slots: private slots:
void on_msg_remove(QString send_id); void on_msg_remove(QString send_id);
void on_widget_height_changed(); // 新增槽函数处理高度变化 void on_rows_height_changed(); // 新增槽函数处理高度变化
void on_current_rows_height_changed();
}; };
#endif #endif

View File

@ -15,7 +15,7 @@ QString md_to_html(const QString& text) {
// 转换回QString并处理代码块 // 转换回QString并处理代码块
QString result = QString::fromUtf8(html); QString result = QString::fromUtf8(html);
free(html); free(html);
qDebug()<<"处理的块:"<<result; //qDebug()<<"处理的块:"<<result;
return process_code_blocks(result.toUtf8()); return process_code_blocks(result.toUtf8());
} }
QString process_code_blocks(QString html) { QString process_code_blocks(QString html) {

View File

@ -48,7 +48,7 @@ void ctai_session_info::init_sub_layout()
m_sub_systeam_widget->setLayout(m_session_systeam_info_layout); m_sub_systeam_widget->setLayout(m_session_systeam_info_layout);
m_session_user_group_layout = new QVBoxLayout(); m_session_user_group_layout = new QVBoxLayout();
m_session_user_edit_info = new QTextEdit(tr("写一篇关于天然气的论文")); m_session_user_edit_info = new QTextEdit(tr("1+1为什么等于2"));
m_session_user_edit_layout = new QVBoxLayout(); m_session_user_edit_layout = new QVBoxLayout();
m_session_user_edit_layout->addWidget(m_session_user_edit_info); m_session_user_edit_layout->addWidget(m_session_user_edit_info);