The QStatusBar has just one line each time and I cannot track the history or save the history in a log file.
So I want to have a dock widget in my mainwindow that is able to display the messages I need in a multi-line way and auto-scrolling way and then automatically saved to a log file.
My question is how to do this in Qt?
Qt Widgets provide means for customization via style sheets, but Qt Quick is a better performing choice for user interfaces that do not aim to look native. Qt Widgets do not scale well for animations. Qt Quick offers a convenient and natural way to implement animations in a declarative manner.
With Qt widgets, stylesheets can be made by the designer, and the developer does native C++ coding. QML is processed at run-time. Within the framework everything can run together, the differences just add flexibility to software architect's decision making.
QStackedWidget can be used to create a user interface similar to the one provided by QTabWidget. It is a convenience layout widget built on top of the QStackedLayout class. Like QStackedLayout, QStackedWidget can be constructed and populated with a number of child widgets ("pages"):
If what you are looking for is something similar to the "Application Output" pane of QtCreator, then a simple QPlainTextEdit
can do the job. You can call QPlainTextEdit::setReadOnly(true)
if you don't want the user to be able to edit its content (i.e. only your application can write to it).
If you want auto-scrolling and automatically saving to a log file, you will need to subclass it though. Here's a little something to get you started:
class MyLogWindow : public QPlainTextEdit { Q_OBJECT /* snip */ public: void appendMessage(const QString& text); private: QFile m_logFile; }; void MyLogWindow::appendMessage(const QString& text) { this->appendPlainText(text); // Adds the message to the widget this->verticalScrollBar()->setValue(this->verticalScrollBar()->maximum()); // Scrolls to the bottom m_logFile.write(text); // Logs to file }
I leave it to you to deal with the boilerplate (opening and closing the file, dealing with newlines, etc.).
Now, simply put an instance of MyLogWindow
in a dock in your QMainWindow
, and call MyLogWindow::appendMessage()
every time you need to log something to have it displayed, scrolled and saved automatically.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With