Qt World Summit: Submit your Presentation

Saving margins of QTextEdit

  • Hello everyone,

    i want to save the pagemargins of QTextEdit in each Document without making a new one with this margin-information.

    Here's my code:

    void MainWindow::SeitenraenderOK() {
        if (rbutton7->isChecked())
            w->setPageMargins(QMarginsF(25, 25, 25, 10));
        else if(rbutton->isChecked())
            w->setPageMargins(QMarginsF(15, 15, 15, 15));
        else if(rbutton2->isChecked())
            w->setPageMargins(QMarginsF(19.1, 25.4, 19.1, 25.4));
        else if(rbutton3->isChecked())
            w->setPageMargins(QMarginsF(50.8, 25.4, 50.8, 25.4));

    Here the PageMargins will be set, if the user check one radiobutton.

    And if the document is saved and the Margin for e.g. is this one:

      w->setPageMargins(QMarginsF(50.8, 25.4, 50.8, 25.4));

    and then reopen the document, the margin is this:

        w->setPageMargins(QMarginsF(25, 25, 25, 10));

    because when the application is initializing, this code executes...

    So, is there a possibility to save the Margin as part of the document?


  • Lifetime Qt Champion


    Do you mean mean save it in the same file as the text data ? How are you doing it currently ?

  • Yes, you are right...

    I am actually saving the Document inside an .html File

  • Lifetime Qt Champion

    Then you could use a custom tag that you will parse in your application to contain that information.

  • And how can i do that?

    Can give me a shrt example, please?

  • Lifetime Qt Champion

    Well, can you show how you are currently saving your document ?

  • void PagesTextEdit::saveUnderDocument()
    if (fileName.isEmpty()) {
    fileName = QFileDialog::getSaveFileName(this, tr("Speichern unter..."),
    QString("Unbenannt.htm"), tr("TextPad 1.0 (*.htm);;"));
    QFile file(fileName, this);
    if (!file.open(QIODevice::WriteOnly | QIODevice::Text))
       qDebug() << "Fehler beim Speichern der Datei";
    QProgressDialog progress("Speichern - Fortschritt", "Abbrechen", 0, 10);
    for(int i = 0; i < 10; i++) {
        QTime saveTime = QTime::currentTime().addMSecs(40);
        while(QTime::currentTime() < saveTime)
            QCoreApplication::processEvents(QEventLoop::AllEvents, 100);
    QTextStream out(&file);
    out << encodeHtml( toHtml() );

  • Lifetime Qt Champion

    Prepend a line which contains the information to what is returned by toHtml.

    And when you load the file, remove that first line before setting the rest of the content on the QTextEdit and with the content of that line, update the margin.

  • @SGaist Ok, and what can the line contains? I don't have any imaginations for that...

  • Lifetime Qt Champion

    If you put your buttons in a QButtonGroup you could simply store the button id so you can programmatically check it when loading your document.