diff --git a/widget/chatareawidget.cpp b/widget/chatareawidget.cpp index a6762c673..44d2f6505 100644 --- a/widget/chatareawidget.cpp +++ b/widget/chatareawidget.cpp @@ -34,6 +34,7 @@ ChatAreaWidget::ChatAreaWidget(QWidget *parent) : setAcceptRichText(false); connect(this, &ChatAreaWidget::anchorClicked, this, &ChatAreaWidget::onAnchorClicked); + connect(verticalScrollBar(), SIGNAL(rangeChanged(int,int)), this, SLOT(onSliderRangeChanged())); } ChatAreaWidget::~ChatAreaWidget() @@ -84,6 +85,8 @@ void ChatAreaWidget::insertMessage(ChatAction *msgAction) if (msgAction == nullptr) return; + checkSlider(); + moveCursor(QTextCursor::End); moveCursor(QTextCursor::PreviousCell); QTextCursor cur = textCursor(); @@ -94,3 +97,16 @@ void ChatAreaWidget::insertMessage(ChatAction *msgAction) messages.append(msgAction); } + +void ChatAreaWidget::onSliderRangeChanged() +{ + QScrollBar* scroll = verticalScrollBar(); + if (lockSliderToBottom) + scroll->setValue(scroll->maximum()); +} + +void ChatAreaWidget::checkSlider() +{ + QScrollBar* scroll = verticalScrollBar(); + lockSliderToBottom = scroll && scroll->value() == scroll->maximum(); +} diff --git a/widget/chatareawidget.h b/widget/chatareawidget.h index e42f576f1..ebc76fa06 100644 --- a/widget/chatareawidget.h +++ b/widget/chatareawidget.h @@ -36,10 +36,13 @@ signals: protected: void mouseReleaseEvent(QMouseEvent * event); -public slots: +private slots: void onAnchorClicked(const QUrl& url); + void onSliderRangeChanged(); private: + void checkSlider(); + QList messages; bool lockSliderToBottom; int sliderPosition;