解决自适应问题

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);
}
qDebug() << "steam data chunk:" << chunk;
//qDebug() << "steam data chunk:" << chunk;
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);
size_t size = sz * nmemb;
psResponse->append(static_cast<char *>(buffer), size);
qDebug() << "not steam data chunk:" << *psResponse;
//qDebug() << "not steam data chunk:" << *psResponse;
return sz * nmemb;
}
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));
}
qDebug() << "result:" << result;
return result;
}
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);
emit send_post_out_data(data);
}
qDebug() << "send_stream:" << str;
}
}
}
void ctai_curl::send_not_stream(model_data &data, std::string response_data)
{
json response = json::parse(response_data);
qDebug()<<"msg_type_mode:"<<data.msg_type_mode;
if (data.postback_stream_mode)
{
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}}}},
{"temperature", 0.7},
{"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();
}
void ctai_curl::set_send_post_option()

View File

@ -3,6 +3,7 @@
ctai_history_textedit::ctai_history_textedit(QWidget *parent)
: QWidget(parent)
{
setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);
}
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->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
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->setReadOnly(true);
historyLayout->addWidget(m_msg_history);
@ -206,56 +204,56 @@ 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"));
updateGeometry();
emit height_changed();
emit fold_row_height_changed();
}
void ctai_history_textedit::on_text_height()
{
// 防抖动处理如果上次更新在100ms内则延迟处理
static QTimer debounceTimer;
debounceTimer.setSingleShot(true);
// 防抖动处理如果上次更新在100ms内则延迟处理
static QTimer debounceTimer;
debounceTimer.setSingleShot(true);
if (debounceTimer.isActive())
{
return;
}
// 直接使用documentLayout获取高度避免多次计算
int textHeight = m_msg_history->document()->documentLayout()->documentSize().height();
if (debounceTimer.isActive())
{
return;
}
// 直接使用documentLayout获取高度避免多次计算
int textHeight = m_msg_history->document()->documentLayout()->documentSize().height();
if (textHeight > 0)
{
// 计算实际需要的高度 (包含边距和视口边框)
int newHeight = textHeight +
m_msg_history->document()->documentMargin() * 2 +
m_msg_history->frameWidth() * 2;
if (textHeight > 0)
{
// 计算实际需要的高度 (包含边距和视口边框)
int newHeight = textHeight +
m_msg_history->document()->documentMargin() * 2 +
m_msg_history->frameWidth() * 2;
// 添加高度阈值判断,避免微小变化触发更新
static const int HEIGHT_THRESHOLD = 5; // 5像素的阈值
if (abs(m_msg_history->height() - newHeight) > HEIGHT_THRESHOLD)
{
m_original_height = newHeight;
// 添加高度阈值判断,避免微小变化触发更新
static const int HEIGHT_THRESHOLD = 5; // 5像素的阈值
if (abs(m_msg_history->height() - newHeight) > HEIGHT_THRESHOLD)
{
m_original_height = newHeight+15;
// 批量更新布局
bool wasBlocked = m_msg_history->signalsBlocked();
m_msg_history->blockSignals(true);
// 批量更新布局
bool wasBlocked = m_msg_history->signalsBlocked();
m_msg_history->blockSignals(true);
m_msg_history->setFixedHeight(newHeight);
updateGeometry();
m_msg_history->setFixedHeight(newHeight);
updateGeometry();
m_msg_history->blockSignals(wasBlocked);
emit height_changed();
m_msg_history->blockSignals(wasBlocked);
emit current_row_height_changed();
// 延迟发送布局更新请求
if (QWidget *parent = parentWidget())
{
QTimer::singleShot(100, [parent]()
{
// 延迟发送布局更新请求
if (QWidget *parent = parentWidget())
{
QTimer::singleShot(100, [parent]()
{
QEvent e(QEvent::LayoutRequest);
QApplication::sendEvent(parent, &e); });
}
}
}
// 设置下一次检查的延迟
debounceTimer.start(100);
}
}
}
// 设置下一次检查的延迟
debounceTimer.start(100);
}
void ctai_history_textedit::add_user_message(const model_data &message)
{

View File

@ -67,7 +67,8 @@ private slots:
void on_text_height();
signals:
void delete_requested(QString); // 请求删除此消息
void height_changed(); // 新增高度变化信号
void current_row_height_changed();
void fold_row_height_changed();
private:
void connect_signals(msg_type msg_type_mode); // 连接信号和槽
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, &ctai_history_textedit::height_changed,
this, &ctai_history_widget::on_widget_height_changed);
connect(message_widget, &ctai_history_textedit::current_row_height_changed,this, &ctai_history_widget::on_current_rows_height_changed);
connect(message_widget, &ctai_history_textedit::fold_row_height_changed,this, &ctai_history_widget::on_rows_height_changed);
// 滚动到新消息
scrollToItem(item(row, 0));
}
// 立即滚动到底部
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()) {
int currentHeight = rowHeight(row);
setRowHeight(row, currentHeight + 100);
//全部行高自适应
for (int row = 0; row < rowCount(); ++row) {
resizeRowToContents(row);
}
scrollToBottom();
}
//适合自适应流式输出内容
void ctai_history_widget::on_current_rows_height_changed()
{
// 当前行高自适应
resizeRowToContents(rowCount());
scrollToBottom();
}
void ctai_history_widget::on_msg_remove(QString send_id)
{
int row = this->rowCount();

View File

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

View File

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