[Solved] Center Text in QTextEdit with HTML



  • When using HTML to center a portion of text in a QTextEdit (e.g., a title), the initial display looks great. After writing a number of lines to the text edit, the text scrolls so that the title is no longer visible. If a clear() is issued and the title rewritten, it is no longer centered. If the text has not yet scrolled off the widget, then a clear() and rewriting the title works fine.

    In the example below, a text edit is created and a title and date are centered on the widget. Every 1/2 second, a message is written to the log widget, left-justified. The File->Clear action will clear the textedit and re-display the title with a new time, both centered. If clear is issued prior to the text getting to the end, then the title and time will be centered. If the vertical scrollbar appears prior to issuing clear, then the title and time are left-justified instead of centered. Even if the time between messages is long (e.g. 5 seconds), the results are the same.

    Any ideas on how to center the first two lines of the text edit and left-justify the remaining text?
    Thanks for your time and help.

    Sample code: main.cpp
    @
    #include <QtGui>
    #include <QApplication>
    #include "texteditsample.h"

    int main(int argc, char *argv[])
    {
    QApplication a(argc, argv);
    TextEditSample w;

    w.setWindowState(w.windowState() ^ Qt::WindowFullScreen);
    w.show();
    
    return a.exec&#40;&#41;;
    

    }
    @
    texteditsample.h:
    @
    #ifndef TEXTEDITSAMPLE_H
    #define TEXTEDITSAMPLE_H

    #include <QtGui>
    #include <QString>

    class TextEditSample : public QMainWindow
    {
    Q_OBJECT

    public:
    static const QString EVENT_LOG_TEXT_INITIAL_FORMAT;
    TextEditSample(QWidget *parent = 0);
    virtual ~TextEditSample(){};

    QWidget *centralwidget;
    QScrollArea *eventLogScrollArea;
    QWidget *eventLogScrollAreaWidgetContents;
    QTextEdit *eventLogTextEdit;
    QMenuBar *menubar;
    QMenu *menuFile;
    QAction *actionExit;
    QAction *actionClearTextEdit;
    

    public slots:
    void updateEventLog();
    void actionExit_triggered();
    void actionClearTextEdit_triggered();

    };
    #endif
    @
    texteditsample.cpp;
    @
    #include <QString>
    #include <QTimer>
    #include "texteditsample.h"

    const QString TextEditSample::EVENT_LOG_TEXT_INITIAL_FORMAT =
    "<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0//EN" "http://www.w3.org/TR/REC-html40/strict.dtd">\n<html><head><meta name="qrichtext" content="1" /></head><body style=" font-family:'Sans'; font-size:10pt; font-weight:400; font-style:normal;"><p align="center" style=" font-size:18pt; margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;">Main App Event Log</p></body></html>";

    TextEditSample::TextEditSample(QWidget *parent) : QMainWindow(parent)
    {
    menubar = new QMenuBar(this);
    menubar->setGeometry(QRect(0, 0, 1000, 25));
    menuFile = menubar->addMenu(tr("&File"));
    actionClearTextEdit = new QAction(tr("&Clear"), this);
    menuFile->addAction(actionClearTextEdit);
    actionExit = new QAction(tr("&Exit"), this);
    menuFile->addAction(actionExit);
    setMenuBar(menubar);

    connect (actionClearTextEdit, SIGNAL(triggered()), this, SLOT(actionClearTextEdit_triggered()));
    connect (actionExit, SIGNAL(triggered()), this, SLOT(actionExit_triggered()));
    
    centralwidget = new QWidget(this);
    centralwidget->setGeometry(QRect(30, 30, 441, 321));
    

    eventLogScrollArea = new QScrollArea(centralwidget);

    eventLogScrollArea->setGeometry(QRect(30, 30, 441, 321));
    eventLogScrollArea->setMinimumSize(441, 321);
    eventLogScrollArea->setFrameShadow(QFrame::Raised);
    eventLogScrollArea->setLineWidth(2);
    eventLogScrollArea->setWidgetResizable(true);
    eventLogScrollAreaWidgetContents = new QWidget();
    eventLogTextEdit = new QTextEdit(eventLogScrollAreaWidgetContents);
    eventLogTextEdit->setEnabled(true);
    eventLogTextEdit->setMinimumSize(410, 290);
    eventLogTextEdit->setLineWrapMode(QTextEdit::NoWrap);
    eventLogTextEdit->setReadOnly(true);
    eventLogTextEdit->setTextInteractionFlags(Qt::TextSelectableByKeyboard|Qt::TextSelectableByMouse);
    eventLogScrollArea->setWidget(eventLogScrollAreaWidgetContents);
    
    eventLogTextEdit->setText(EVENT_LOG_TEXT_INITIAL_FORMAT);
    eventLogTextEdit->append(
          QDateTime::currentDateTime().toString(
           tr("ddd MM/dd/yyyy hh:mm:ss "))
           );
    
    QTimer* timeUpdateSignal = new QTimer(this);
    connect(timeUpdateSignal, SIGNAL(timeout()), this, SLOT(updateEventLog()));
    timeUpdateSignal->start(500);
    

    }

    void TextEditSample::actionExit_triggered()
    {
    close();
    }

    void TextEditSample::actionClearTextEdit_triggered()
    {
    eventLogTextEdit->clear();
    eventLogTextEdit->setText(EVENT_LOG_TEXT_INITIAL_FORMAT);
    eventLogTextEdit->append(
    QDateTime::currentDateTime().toString(
    tr("ddd MM/dd/yyyy hh:mm:ss "))
    );
    }

    void TextEditSample::updateEventLog()
    {
    QString textToAppend("10:23:45.593 Long text message with status and/or error information to display on text edit.");

    eventLogTextEdit->append(textToAppend);
    }
    @

    [Edit: code tags; mlong]


  • Moderators

    Please add @ tags around your code when you post. Thanks!



  • If you call clear(), the complete contents of the editor are wiped out, including all the formatting. You will have to switch to centered display manually then. Either use the API or set a simple, empty HTML instead of calling clear().



  • Super! Thanks.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.