Qt5文字操作

1、字体字号设置

void ImgProcessor::ShowFontComboBox(QString comboStr)	//设置字体
{
    QTextCharFormat fmt;		 //创建一个QTextCharFormat对象
    fmt.setFontFamily(comboStr); //选择的字体名称设置给QTextCharFormat对象
    mergeFormat(fmt);     		 //将新的格式应用到光标选区内的字符
}

void ImgProcessor::mergeFormat(QTextCharFormat format)
{
    QTextCursor cursor =showWidget->text->textCursor();
    //获得编辑框中的光标
    if(!cursor.hasSelection())							//(a)
        cursor.select(QTextCursor::WordUnderCursor);
    cursor.mergeCharFormat(format);						//(b)
    showWidget->text->mergeCurrentCharFormat(format);	//(c)
}

void ImgProcessor::ShowSizeSpinBox(QString spinValue)	//设置字号
{
   QTextCharFormat fmt;
   fmt.setFontPointSize(spinValue.toFloat());
   showWidget->text->mergeCurrentCharFormat(fmt);
}

2、文字下划线、加粗、颜色等设置

void ImgProcessor::ShowBoldBtn()                        //设置文字显示加粗
{
    QTextCharFormat fmt;
    fmt.setFontWeight(boldBtn->isChecked()?QFont::Bold:QFont::Normal);
    showWidget->text->mergeCurrentCharFormat(fmt);
}

void ImgProcessor::ShowItalicBtn()                      //设置文字显示斜体
{
    QTextCharFormat fmt;
    fmt.setFontItalic(italicBtn->isChecked());
    showWidget->text->mergeCurrentCharFormat(fmt);
}

void ImgProcessor::ShowUnderlineBtn()                   //设置文字加下画线
{
    QTextCharFormat fmt;
    fmt.setFontUnderline(underlineBtn->isChecked());
    showWidget->text->mergeCurrentCharFormat(fmt);
}

void ImgProcessor::ShowColorBtn()						//设置文字颜色
{
    QColor color=QColorDialog::getColor(Qt::red,this);	//(a)
    if(color.isValid())
    {
        QTextCharFormat fmt;
            fmt.setForeground(color);
            showWidget->text->mergeCurrentCharFormat(fmt);
    }
}

3、文字排序

void ImgProcessor::ShowAlignment(QAction *act)
{
    if(act==leftAction)
        showWidget->text->setAlignment(Qt::AlignLeft);
    if(act==rightAction)
        showWidget->text->setAlignment(Qt::AlignRight);
    if(act==centerAction)
        showWidget->text->setAlignment(Qt::AlignCenter);
    if(act==justifyAction)
        showWidget->text->setAlignment(Qt::AlignJustify);
}

4、控件更新

void ImgProcessor::ShowCurrentFormatChanged(const QTextCharFormat &fmt)
{
    fontComboBox->setCurrentIndex(fontComboBox->findText(fmt.fontFamily()));
    sizeComboBox->setCurrentIndex(sizeComboBox->findText(QString::number(fmt.fontPointSize())));
    boldBtn->setChecked(fmt.font().bold());
    italicBtn->setChecked(fmt.fontItalic());
    underlineBtn->setChecked(fmt.fontUnderline());
}

 

5、文字缩进

Qt5文字操作_第1张图片

Qt5文字操作_第2张图片

void ImgProcessor::ShowList(int index)
{
    //获得编辑框的QTextCursor对象指针
    QTextCursor cursor=showWidget->text->textCursor();
    if(index!=0)
    {
        QTextListFormat::Style style=QTextListFormat::ListDisc;//(a)
        switch(index)                					//设置style属性值
        {
            default:
            case 1:
                style=QTextListFormat::ListDisc; break;
            case 2:
                style=QTextListFormat::ListCircle; break;
            case 3:
                style=QTextListFormat::ListSquare; break;
            case 4:
                style=QTextListFormat::ListDecimal; break;
            case 5:
                style=QTextListFormat::ListLowerAlpha; break;
            case 6:
                style=QTextListFormat::ListUpperAlpha; break;
            case 7:
                style=QTextListFormat::ListLowerRoman; break;
            case 8:
                style=QTextListFormat::ListUpperRoman; break;
        }
        /* 设置缩进值 */								//(b)
        cursor.beginEditBlock();
        QTextBlockFormat blockFmt=cursor.blockFormat();
        QTextListFormat listFmt;
        if(cursor.currentList())
        {
            listFmt= cursor.currentList()->format();
        }
        else
        {
            listFmt.setIndent(blockFmt.indent()+1);
            blockFmt.setIndent(0);
            cursor.setBlockFormat(blockFmt);
        }
        listFmt.setStyle(style);
        cursor.createList(listFmt);
        cursor.endEditBlock();
    }
    else
    {
        QTextBlockFormat bfmt;
        bfmt.setObjectIndex(-1);
        cursor.mergeBlockFormat(bfmt);
    }
}

 

你可能感兴趣的:(Qt)