26

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?

eyllanesc
  • 235,170
  • 19
  • 170
  • 241
Daniel
  • 2,576
  • 7
  • 37
  • 51
  • You'll have to write you own widget, start by reading the [docs](http://doc.qt.digia.com/4.7-snapshot/classes.html) or with a qt tutorial. –  Jan 04 '13 at 16:41
  • 2
    Well, you can use QTextEdit with setReadOnly(true). – Archie Jan 04 '13 at 16:47

1 Answers1

36

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:

#include <QCoreApplication>
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
    // optional if you want to see the changes 
    // after appendPlainText() immediately
    // useful if you use this->appendMessage() in a loop
    QCoreApplication::processEvents();

}

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.

KuhakuPixel
  • 212
  • 2
  • 4
  • 11
Fred
  • 4,894
  • 1
  • 31
  • 48
  • Wow, okay I will try it. Btw, have you guys attended any classes or you just learnt Qt simply by reading manuals? Why do you know them so well? :) – Daniel Jan 04 '13 at 17:05
  • 3
    @Daniel : In my case, it started long ago with reading manuals and tutorials. Add to that over 10 years of experience and working professionally with Qt and that gives a more in-depth knowledge of the framework. Qt is open-source (so is QtCreator), so if you ever wonder how something works, open the source code and dig in. – Fred Jan 04 '13 at 17:08
  • 3
    I started with a Qt book over 4 years ago "C++ GUI Programming with Qt4." I read that from cover to cover then I tried a good deal of the Qt bundled tutorial samples and demos. After that I got to real coding for my job which is on order of 200 thousand lines of code written since I first picked up that Qt book.. Even now I still find myself learning new things. That is one reason I look at every single Qt question here on stackoverflow. – drescherjm Jan 04 '13 at 17:26