Qt设置的字体加粗、下划线、斜体、字号,字体

加粗

void MainWindow::on_checkbox_Bold_stateChanged(int arg1)
{
    ui->textEdit->selectAll();
    QTextCharFormat fmt = ui->textEdit->currentCharFormat();
    if(arg1 == Qt::Checked)
    {
        fmt.setFontWeight(QFont::Bold);
    }else
    {
        fmt.setFontWeight(QFont::Normal);
    }
    ui->textEdit->mergeCurrentCharFormat(fmt);

//    QString str = ui->textEdit->toPlainText();
//    ui->textEdit->setText(str);
    ui->textEdit->append("追加文字");
}

下划线

void MainWindow::on_checkbox_Unline_stateChanged(int arg1)
{
    QTextCharFormat fmt = ui->textEdit->currentCharFormat();
    if(arg1 == Qt::Checked)
    {
        fmt.setFontUnderline(true);
    }else
    {
        fmt.setFontUnderline(false);
    }
    ui->textEdit->mergeCurrentCharFormat(fmt);

    QString str = ui->textEdit->toPlainText();
    ui->textEdit->setText(str);
    ui->textEdit->append("追加文字");
}

斜体

void MainWindow::on_checkbox_Italic_stateChanged(int arg1)
{
    QTextCharFormat fmt = ui->textEdit->currentCharFormat();
    if(arg1 == Qt::Checked)
    {
        fmt.setFontItalic(true);
    }else
    {
        fmt.setFontItalic(false);
    }
    ui->textEdit->mergeCurrentCharFormat(fmt);

    QString str = ui->textEdit->toPlainText();
    ui->textEdit->setText(str);
    ui->textEdit->append("追加文字");
}

字体大小

void MainWindow::on_spinSize_valueChanged(int arg1)
{
    QTextCharFormat fmt = ui->textEdit->currentCharFormat();
    fmt.setFontPointSize(arg1);
    ui->textEdit->mergeCurrentCharFormat(fmt);

    QString str = ui->textEdit->toPlainText();
    ui->textEdit->setText(str);
    ui->textEdit->append("追加文字");
}

字体设置

void MainWindow::on_fontComboBox_currentFontChanged(const QFont &f)
{
    QTextCharFormat fmt = ui->textEdit->currentCharFormat();
    fmt.setFont(f);
    ui->textEdit->mergeCurrentCharFormat(fmt);

    QString str = ui->textEdit->toPlainText();
    ui->textEdit->setText(str);
    ui->textEdit->append("追加文字");
}

你可能感兴趣的:(Qt基础,qt,开发语言)