c++qtfontsqtexteditqtextcursor

QTextEdit::cursorPositionChanged() -> synchronization font style with QComboBox and buttons


I got a problem. I'm typing a text with Font Size 15: "Hello". Then I'm typing a text with Font Size 20: "World". When I change position of the cursor back to first line, my font size will change from 20 to 15. But my QComboBox is still shows "20". How to do font style synchronization with QComboBox and buttons (Bold, Italics, Underline)?

https://thepasteb.in/p/Lghpcmp0oGM1mUW

Code:

#include "notepadwindow.h"
#include "ui_notepadwindow.h"
#include <QFileDialog>
#include <QComboBox>

NotepadWindow::NotepadWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::NotepadWindow)

{
    ui->setupUi(this);
    ui->actionBold->setCheckable(true);
    ui->actionItalics->setCheckable(true);
    ui->actionUnderline->setCheckable(true);
    ui->actionLeft->setCheckable(true);
    ui->actionRight->setCheckable(true);
    ui->actionCenter->setCheckable(true);
    ui->actionJustify->setCheckable(true);
    this->setCentralWidget(ui->textEdit); // Wyśrodkuj Pole tekstowe
    QComboBox* myComboBox = new QComboBox;
    ui->mainToolBar->addWidget(myComboBox);
    for (int i = 1; i < 102; i += 2) {
      myComboBox->addItem(QString::number(i));
    }
    myComboBox->setCurrentText("11");
    ui->textEdit->setFontPointSize(11);
    ui->actionLeft->setChecked(true);
    ui->actionRight->setChecked(false);
    ui->actionCenter->setChecked(false);
    ui->actionJustify->setChecked(false);
    QObject::connect(myComboBox, SIGNAL(currentTextChanged(QString)), this, SLOT(onFontSizeChanged(QString)));
    connect(ui->textEdit, SIGNAL(QTextEdit::cursorPositionChanged()), this, SLOT());


}


NotepadWindow::~NotepadWindow()
{
    delete ui;
}



void NotepadWindow::openfile(QString textfile)
{
        QFile sFile(textfile);
        if(sFile.open(QFile::ReadOnly | QFile::Text)) // Jeśli plik jest otwarty
        {
            mFilename = textfile;
            QTextStream in(&sFile);
            in.setCodec("UTF-8"); // Ustaw kodowania znaków na UTF-8
            QString text = in.readAll();
            sFile.close();
            ui->textEdit->setHtml(text);
        }
}

void NotepadWindow::onFontSizeChanged(QString selected)
{
    ui->textEdit->setFontPointSize(selected.toInt());
}

void NotepadWindow::on_actionUndo_triggered()
{
    ui->textEdit->undo();
}

void NotepadWindow::on_actionRedo_triggered()
{
    ui->textEdit->redo();
}

void NotepadWindow::on_actionCut_triggered()
{
    ui->textEdit->cut();
}

void NotepadWindow::on_actionCopy_triggered()
{
    ui->textEdit->copy();
}

void NotepadWindow::on_actionPaste_triggered()
{
    ui->textEdit->paste();
}

void NotepadWindow::on_actionNew_triggered()
{
    mFilename = "";
    ui->textEdit->setPlainText("");
}

void NotepadWindow::on_actionOpen_triggered()
{
    QString file = QFileDialog::getOpenFileName(this, "open"); // Otwórz okienko wyboru plików
    if(!file.isEmpty())        // Jeśli plik nie jest pusty
    {
        QFile sFile(file);
        if(sFile.open(QFile::ReadOnly | QFile::Text)) // Jeśli plik jest otwarty
        {
            mFilename = file;
            QTextStream in(&sFile);
            in.setCodec("UTF-8"); // Ustaw kodowania znaków na UTF-8
            QString text = in.readAll();
            sFile.close();
            ui->textEdit->setHtml(text);
        }
    }
}

void NotepadWindow::on_actionSave_triggered()
{
    QFile sFile(mFilename);
    if(sFile.open(QFile::WriteOnly | QFile::Text)) // Jeśli plik jest otwarty
    {
        QTextStream out(&sFile);
        out << ui->textEdit->toHtml();
        out.setCodec("UTF-8"); // Ustaw kodowania znaków na UTF-8
        sFile.flush();
        sFile.close();

    }
    else if(!sFile.open(QFile::WriteOnly | QFile::Text)) on_actionSave_as_triggered();
}

void NotepadWindow::on_actionSave_as_triggered()
{
    QString file = QFileDialog::getSaveFileName(this,  tr("Text File"), "", tr("Text files (*.txt)"));
    if(!file.isEmpty())
    {
        mFilename = file;
        on_actionSave_triggered();
    }
}




void NotepadWindow::on_actionBold_triggered(bool checked)
{
    if(checked)
    {
        ui->textEdit->setFontWeight(QFont::Bold);
    }
    if(!checked)
    {
        ui->textEdit->setFontWeight(QFont::Normal);
    }

}

void NotepadWindow::on_actionItalics_triggered(bool checked)
{
    if(checked)
    {
        ui->textEdit->setFontItalic(1);
    }
    if(!checked)
    {
        ui->textEdit->setFontItalic(0);
    }

}

void NotepadWindow::on_actionUnderline_triggered(bool checked)
{
    if(checked)
    {
        ui->textEdit->setFontUnderline(1);
    }
    if(!checked)
    {
        ui->textEdit->setFontUnderline(0);
    }

}

void NotepadWindow::on_actionLeft_triggered()
{
    ui->textEdit->setAlignment(Qt::AlignLeft);
    ui->actionLeft->setChecked(true);
    ui->actionRight->setChecked(false);
    ui->actionCenter->setChecked(false);
    ui->actionJustify->setChecked(false);
}

void NotepadWindow::on_actionCenter_triggered()
{
    ui->textEdit->setAlignment(Qt::AlignCenter);
    ui->actionLeft->setChecked(false);
    ui->actionRight->setChecked(false);
    ui->actionCenter->setChecked(true);
    ui->actionJustify->setChecked(false);
}

void NotepadWindow::on_actionRight_triggered()
{
    ui->textEdit->setAlignment(Qt::AlignRight);
    ui->actionLeft->setChecked(false);
    ui->actionRight->setChecked(true);
    ui->actionCenter->setChecked(false);
    ui->actionJustify->setChecked(false);
}

void NotepadWindow::on_actionJustify_triggered()
{
    ui->textEdit->setAlignment(Qt::AlignJustify);
    ui->actionLeft->setChecked(false);
    ui->actionRight->setChecked(false);
    ui->actionCenter->setChecked(false);
    ui->actionJustify->setChecked(true);
}

Solution

  • Like said on your last comment, you have the same problem with alignement.

    I think the main thing to do in your software is to "update" UI informations like font-size, alignement, bold and so one when it is different. That's exactly what Microsoft Word and others writer do.

    The main idea is the same that before but in the more common way:

    1. Trigger the currentCharFormatChanged(const QTextCharFormat &f) signal to get informations about the actual text (bold, italic pointSize aso..)
    2. Update UI informations about last informations (If text it is bold, check the bold button aso..)

    Because QTextCharFormat gives only informations about characters and not alignment, you have to get ui->textEdit->alignment() to get alignment informations

    So in parallel, for alignment you do the same:

    1. Trigger the cursorPositionChanged() signal
    2. Update UI alignment buttons (If alignment is left, check the "left alignment" button, if center, check the center one aso..)

    Here is your code:

    notepadwindow.h : https://pastebin.com/8b5UqnZF

    notepadwindow.cpp: https://pastebin.com/ufJPM2Yj


    Learn to read the Doc or begin with easier things

    I am maybe wrong but with what I saw, it seems that you don't really know what you are doing.

    I mean, I searched into the doc to find the responses you need. I didn't find them directly, I had to try and to understand how it worked and the only thing I used was the doc. It appears that you asked multiple times people to help you on forums and that is what bring me to this conclusion.

    I know that you are certainly a beginner, and that maybe you think that searching on the doc is a waste of time or that you don't understand anything on it, but if you want to progress in programming, you have to read documentations (even if you don't understand anything on it) and try easy things at the beginning to understand what you are doing.

    Don't take it like a reproach but more like an advice: Read the documentation if you know what you do, if not, begin with easier things.