Qt 实现对文本文字的编辑(字体、加粗、倾斜、下划线、颜色、字号)。
1、创建工程并进行界面设计。创建基于QWidget基类的Qt界面应用程序,并在主界面添加相关控件如下:
界面控件依次为:QLabel、QLineEdit、QPushButton、QComboBox、QToolButton、QToolButton、QToolButton、QToolButton、QComboBox、QTextEdit。
2、创建创建槽函数及类私有函数
.h头文件创建槽函数及类私有函数:
private:
void ManipulateString();
void mergeCurrentCharFormat(QTextCharFormat fmt);
private slots:
void slotReadTxt();
void slotFontComboBox(QString strFont);
void slotSizeSpinBox(QString strSize);
void slotBold();
void slotItalic();
void slotUnderline();
void slotFontColor();
void slotFormatChanged(const QTextCharFormat &fmt);
.cpp构造函数进行类初始化及槽函数绑定
ui->fontComboBox->setFontFilters(QFontComboBox::ScalableFonts);
QFontDatabase fontDb;
foreach (int size, fontDb.standardSizes()) {
ui->sizeComboBox->addItem(QString::number(size));
}
ui->boldBtn->setCheckable(true);
ui->italicBtn->setCheckable(true);
ui->underlineBtn->setCheckable(true);
connect(ui->pushButton,SIGNAL(clicked(bool)),this,SLOT(slotReadTxt()));
connect(ui-fontComboBox,SIGNAL(activated(QString)),this,SLOT(slotFontComboBox(QString)));
connect(ui->sizeComboBox,SIGNAL(activated(QString)),this,SLOT(slotSizeSpinBox(QString)));
connect(ui->boldBtn,SIGNAL(clicked(bool)),this,SLOT(slotBold()));
connect(ui->italicBtn,SIGNAL(clicked(bool)),this,SLOT(slotItalic()));
connect(ui->underlineBtn,SIGNAL(clicked(bool)),this,SLOT(slotUnderline()));
connect(ui->colorButton,SIGNAL(clicked(bool)),this,SLOT(slotFontColor()));
connect(ui->textEdit,SIGNAL(currentCharFormatChanged(QTextCharFormat)),this,SLOT(slotFormatChanged(QTextCharFormat)));
3、槽函数及类私有函数实现
void Widget::slotFontComboBox(QString strFont)
{
QTextCharFormat fmt;
fmt.setFontFamily(strFont);
mergeCurrentCharFormat(fmt);
}
void Widget::slotBold()
{
QTextCharFormat fmt;
fmt.setFontWeight(ui->boldBtn->isChecked()?QFont::Bold:QFont::Normal);
mergeCurrentCharFormat(fmt);
}
void Widget::slotItalic()
{
QTextCharFormat fmt;
fmt.setFontItalic(ui->italicBtn->isChecked()?true:false);
mergeCurrentCharFormat(fmt);
}
void Widget::slotUnderline()
{
QTextCharFormat fmt;
fmt.setFontUnderline(ui->underlineBtn->isChecked()?true:false);
mergeCurrentCharFormat(fmt);
}
void Widget::slotSizeSpinBox(QString strSize)
{
QTextCharFormat fmt;
fmt.setFontPointSize(strSize.toFloat());
mergeCurrentCharFormat(fmt);
}
void Widget::slotFontColor()
{
QColor color = QColorDialog::getColor(Qt::red,this);
if(color.isValid())
{
QTextCharFormat fmt;
fmt.setForeground(color);
mergeCurrentCharFormat(fmt);
QPalette palette;
palette.setColor(QPalette::ButtonText,color);
ui->colorButton->setPalette(palette);
}
}
void Widget::slotFormatChanged(const QTextCharFormat &fmt)
{
ui->fontComboBox->setCurrentText(fmt.fontFamily());
//ui->fontComboBox->setCurrentIndex(ui->fontComboBox->findText(fmt.fontFamily()));
ui->sizeComboBox->setCurrentIndex(ui->sizeComboBox->findText(QString::number(fmt.fontPointSize())));
ui->boldBtn->setChecked(fmt.font().bold());
ui->italicBtn->setChecked(fmt.font().italic());
ui->underlineBtn->setChecked(fmt.font().underline());
QPalette palette;
QColor color = fmt.foreground().color();
palette.setColor(QPalette::ButtonText,color);
ui->colorButton->setPalette(palette);
}
void Widget::mergeCurrentCharFormat(QTextCharFormat fmt)
{
QTextCursor cursor = ui->textEdit->textCursor();
if(!cursor.hasSelection())
cursor.select(QTextCursor::WordUnderCursor);
cursor.mergeCharFormat(fmt);
ui->textEdit->mergeCurrentCharFormat(fmt);
}
4、点击【运行】或【Ctrl+R】运行程序,并点击按钮选取txt文本文件,进行内容读取并展示在文本框中。编辑文本文字:更改字体、加粗、倾斜、下划线、字体颜色、字号等。
后续公众号会发布系列教程,更多内容请关注公众号:程序猿学习日记