0

我有问题。我正在输入字体大小为 15 的文本:“你好”。然后我正在输入一个字体大小为 20 的文本:“世界”。当我将光标的位置更改回第一行时,我的字体大小将从 20 变为 15。但我的 QComboBox 仍然显示“20”。如何与 QComboBox 和按钮(粗体、斜体、下划线)进行字体样式同步?

https://thepasteb.in/p/Lghpcmp0oGM1mUW

代码:

#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);
}
4

2 回答 2

0

Edited 2 (16.12.2017):

enter image description here

I said you on the last edit of this post what you have to change and you said it didn't work.

I took all your code and made the change I said and it works perfectly for me.

https://pastebin.com/raw/Y38eLYbK


Edited (15.12.2017):

So, I maybe understood what is going on with your code:

By setFontPointSize(), your are changing the current format size. that is represented with a QTextCharFormat.

So basically you have this: enter image description here

When you put your cursor on "Hello", it takes the QTextCharFormat associated with Hello (fontPoinzeSize that equals 15). So your top right font size has no effect on it.

What you have to do is to change the current QTextCharFormat with the size you have on the top right when you place your cursor on other position like this:

/*  In your .h */
class NotepadWindow{

    /* ... */

    private:
        int m_actualPointSize;
}


/** In your cpp */
NotepadWindow::NotepadWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::NotepadWindow)

{
    /* ... */
    connect(ui->textEdit, SIGNAL(cursorPositionChanged()), this, SLOT(changeCursorPosPointSize()));
}

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

void NotepadWindow::changeCursorPosPointSize()
{
    QTextCharFormat format;
    format.setFontPointSize(m_actualPointSize); // Put the variable that is associated with 
    ui->textEdit->mergeCurrentCharFormat(format);
}

Old post:

Maybe QTextEdit (ui->textEdit) is not updated and you maybe need to call the update function.

Try it like this:

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

If it doesn't work, can you please tell us what is the method called by this line:

connect(ui->textEdit, SIGNAL(QTextEdit::cursorPositionChanged()), this, SLOT());

because SLOT() is pointing to any method.

于 2017-12-15T12:35:11.840 回答
0

就像您在上一条评论中所说的那样,您在对齐方面遇到了同样的问题。

我认为在您的软件中要做的主要事情是“更新” UI 信息,如字体大小、对齐方式、粗体等,当它不同时。这正是 Microsoft Word 和其他作家所做的。

主要思想与之前相同,但采用更常见的方式:

  1. 触发currentCharFormatChanged(const QTextCharFormat &f) 信号以获取有关实际文本的信息(粗体、斜体 pointSize aso..)
  2. 更新有关最后信息的 UI 信息(如果文本为粗体,请检查粗体按钮...)

因为 QTextCharFormat 只提供有关字符而不是对齐的信息,所以您必须ui->textEdit->alignment()获取对齐信息

所以平行地,为了对齐,你做同样的事情:

  1. 触发 cursorPositionChanged() 信号
  2. 更新 UI 对齐按钮(如果对齐是左对齐,请检查“左对齐”按钮,如果居中,请检查居中一等..)

这是您的代码:

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

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


学习阅读文档或从更简单的事情开始

我可能错了,但就我所看到的而言,您似乎并不真正知道自己在做什么。

我的意思是,我搜索了文档以找到您需要的回复。我没有直接找到它们,我不得不尝试并了解它是如何工作的,而我唯一使用的是文档。看来您多次要求人们在论坛上帮助您,这就是让我得出这个结论的原因。

我知道你肯定是个初学者,也许你认为在文档上搜索是浪费时间或者你对它什么都不懂,但是如果你想在编程上有所进步,你必须阅读文档(即使你对它一无所知)并在开始时尝试简单的事情以了解你在做什么。

不要把它当作一种责备,而更像是一种建议:如果你知道自己在做什么,请阅读文档,如果不知道,请从更简单的事情开始。

于 2017-12-25T13:21:54.970 回答