Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Which qt widget should I use for message display?

Tags:

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?

like image 888
Daniel Avatar asked Jan 04 '13 16:01

Daniel


People also ask

Should I use Qt Quick or Qt widgets?

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.

What is the difference between Qt widgets and QML?

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.

What is Qt stacked widget?

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"):


1 Answers

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.

like image 186
Fred Avatar answered Oct 05 '22 06:10

Fred