To implement the status information display box using Qtextedit:
1, using qtexttable, organization status information
2, when the status information reaches a certain amount, the first record is automatically deleted. (For this check for a long time Qtextedit delete content method to achieve this function, so leave this record, for reuse. )
void mainwindow::onstatusinfo (int flag,qstring msg) {QString text;
Qtextcharformat chfmt;
Set Status information label switch (flag) {Case 1:text = "info";
Chfmt.setforeground (Qbrush (qt::black));
Break
Case 2:text = "WARN";
Chfmt.setforeground (Qbrush (Qt::yellow));
Break
Case 3:text = "ERROR";
Chfmt.setforeground (Qbrush (qt::red));
Break
Default:break;
}//Append status information after the label text.append ("" +msg);
Set the insertion position to Qtextedit last line qtextcursor cur = ui->statustextediter->textcursor ();
Cur.moveposition (Qtextcursor::end);
Qtexttabelformate tableformate;
Tableformate.setborder (0);
Insert table for displaying status information qtexttable *table = cur.inserttable (2,1,tableformate);
Table->cellat (0,0). Firstcursorposition (). InsertText ("[" +qtime::currenttime (). toString ("Hh:mm:ss") + "]");
Table->cellat (1,0). Firstcursorposition (). InsertText (text); Cur.moveposition (qtextcursor::endofblock,qtextcursor::keepanchor,1);
Cur.select (Qtextcursor::blockundercursor);
Cur.mergecharformat (CHFMT);
Cur.clearselection ();
Qscrollbar *bar = Ui->statustextedit->verticalscrollbar ();
Bar->setvalue (Bar->maximum ());
Number of status information +1 M_status_count + +; If the number of messages is >50, delete the oldest record if (M_status_count >50) {//Move the selection cursor to the first character cur.setposition (1,qtextcursor:
: Moveanchor);
Select the first table Cur.moveposition (qtextcursor::endofblock,qtextcursor::keepanchor,1) containing the earliest state information;
Cur.select (Qtextcursor::blockundercursor);
Delete this information cur.removeselectedtext ();
The cursor moves to the last line of Qtextedit cur.moveposition (Qtextcursor::end,qtextcursor::moveanchor);
M_status_count--; }
}