A log watch box is required to make a recent project. I used Qtextedit to display my log information, but I found that every time I append my content, the scroll bar in Qtextedit does not pull down automatically, it is troublesome to pull it down every time. Found some information to achieve it. Today to summarize, there are deficiencies in the place, the trouble we point out, together with progress.
1 Create Qtextedit object void Customclass::inittextedit ()
{
M_ptextedit = new Qtextedit (this);
Connect (M_ptextedit, SIGNAL (textChanged ()), this, SLOT (Onautoscroll ()));
}
2 The following two methods can be used to implement the automatic drop-down method of the scroll bar : Directly set the value of the vertical scrollbar of the Qtextedit object (remember to add a header file when using #include <QAbstractScrollArea>)
void Customclass::onautoscroll ()
{
M_ptextedit->verticalscrollbar ()->setvalue (M_ptextedit->verticalscrollbar ()->maximum ());
}
Method Two: Modify the Qtextedit text cursor to implement the ScrollBar drop-down (remember to add a header file when using #include<qtextcursor>)
void Customclass::onautoscroll ()
{
Qtextcursor cursor = M_ptextedit->textcursor ();
Cursor.moveposition (Qtextcursor::end);
M_ptextedit->settextcursor (cursor);
}
This way, when M_ptextedit calls the Append () function, the scroll bar will automatically drop down.