Qtテキストエディタにおける文字書式設定の実装

基本フレームワーク

Qtでリッチテキスト編集機能を実装する際、主要なクラスとしてQTextEdit、QTextDocument、QTextCursor、QTextCharFormat等を使用します。QTextEditが編集コンテナとして機能し、QTextDocumentがコンテンツを管理します。書式操作はQTextCursorを通じて行われ、QTextCharFormatが文字属性を定義します。

// ヘッダー宣言
QLabel *fontTypeLabel;
QFontComboBox *fontSelector;
QComboBox *fontSizeSelector;
QToolButton *boldButton;
QToolButton *italicButton;
QToolButton *underlineButton;
QToolButton *colorButton;
QToolBar *formatToolbar;

// スロット宣言
void updateFontSelection(const QString &fontName);
void updateFontSize(const QString &size);
void toggleBold();
void toggleItalic();
void toggleUnderline();
void changeTextColor();
void syncFormatDisplay(const QTextCharFormat ¤tFormat);

UIコンポーネントの初期化

// ツールバー設定
fontTypeLabel = new QLabel(tr("フォント:"));
fontSelector = new QFontComboBox;
fontSelector->setFontFilters(QFontComboBox::ScalableFonts);

QLabel *fontSizeLabel = new QLabel(tr("サイズ:"));
fontSizeSelector = new QComboBox;
QFontDatabase fontDB;
foreach(int size, fontDB.standardSizes()) {
    fontSizeSelector->addItem(QString::number(size));
}

boldButton = new QToolButton;
boldButton->setIcon(QIcon("bold.png"));
boldButton->setCheckable(true);

// 同様にitalicButton, underlineButton, colorButtonを初期化

// イベント接続
connect(fontSelector, &QFontComboBox::textActivated, 
        this, &TextEditor::updateFontSelection);
connect(boldButton, &QToolButton::clicked, 
        this, &TextEditor::toggleBold);

書式適用メカニズム

void TextEditor::applyFormat(const QTextCharFormat &format) {
    QTextCursor editorCursor = textArea->textCursor();
    if(!editorCursor.hasSelection()) {
        editorCursor.select(QTextCursor::WordUnderCursor);
    }
    editorCursor.mergeCharFormat(format);
    textArea->mergeCurrentCharFormat(format);
}

フォント設定

void TextEditor::updateFontSelection(const QString &fontName) {
    QTextCharFormat textFormat;
    textFormat.setFontFamily(fontName);
    applyFormat(textFormat);
}

フォントサイズ設定

void TextEditor::updateFontSize(const QString &size) {
    QTextCharFormat textFormat;
    textFormat.setFontPointSize(size.toFloat());
    textArea->mergeCurrentCharFormat(textFormat);
}

文字装飾設定

// 太字設定
void TextEditor::toggleBold() {
    QTextCharFormat textFormat;
    textFormat.setFontWeight(boldButton->isChecked() ? 
                            QFont::Bold : QFont::Normal);
    applyFormat(textFormat);
}

// 斜体設定
void TextEditor::toggleItalic() {
    QTextCharFormat textFormat;
    textFormat.setFontItalic(italicButton->isChecked());
    applyFormat(textFormat);
}

// 下線設定
void TextEditor::toggleUnderline() {
    QTextCharFormat textFormat;
    textFormat.setFontUnderline(underlineButton->isChecked());
    applyFormat(textFormat);
}

文字色設定

void TextEditor::changeTextColor() {
    QColor selectedColor = QColorDialog::getColor(Qt::black, this);
    if(selectedColor.isValid()) {
        QTextCharFormat textFormat;
        textFormat.setForeground(selectedColor);
        applyFormat(textFormat);
    }
}

書式同期処理

void TextEditor::syncFormatDisplay(const QTextCharFormat ¤tFormat) {
    fontSelector->setCurrentFont(QFont(currentFormat.fontFamily()));
    fontSizeSelector->setCurrentText(QString::number(currentFormat.fontPointSize()));
    boldButton->setChecked(currentFormat.font().bold());
    italicButton->setChecked(currentFormat.fontItalic());
    underlineButton->setChecked(currentFormat.fontUnderline());
}

タグ: Qt QTextEdit QTextCharFormat QTextCursor QFontComboBox

6月29日 21:43 投稿