我有问题。我正在输入字体大小为 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);
}