Qt5 学习之路及嵌入式开发教程16:Qt5主窗口---格式菜单栏
这次任务要完成Qt5主窗口的界面设计第三部分:格式及子菜单:字体、颜色、粗体、斜体、下划线及快捷方式功能实现
1、属性及图标:
字体:
颜色:
粗体:
斜体:
下划线:
快捷方式直接拖曳到工具栏就行了。
2、各部分功能实现
在mainwindow.h中添加槽函数
void ShowBold(); //粗体
void ShowItalic(); //斜体
void ShowUnderLine(); //下划线
void ShowFont(); //设置字体
void ShowColor(); //设置颜色
在mainwindow.cpp中添加代码
头文件:
#include <QTextCharFormat>
#include <QFontDialog>
#include <QColorDialog>
槽函数:
connect(ui->fontBoldAction, SIGNAL(triggered(bool)),this,SLOT(ShowBold()) );
connect(ui->fontItalicAction,SIGNAL(triggered(bool)),this,SLOT(ShowItalic()) );
connect(ui->fontUnderAction, SIGNAL(triggered(bool)),this,SLOT(ShowUnderLine()));
connect(ui->fontSetAction, SIGNAL(triggered(bool)),this,SLOT(ShowFont()));
connect(ui->colorSetAction,SIGNAL(triggered(bool)),this,SLOT(ShowColor()));
功能实现函数:
//粗体
void MainWindow::ShowBold()
{
QTextCharFormat fmt;
fmt.setFontWeight(ui->fontBoldAction->isChecked()?QFont::Bold:QFont::Normal);
ui->textEdit->mergeCurrentCharFormat(fmt);
}
//斜体
void MainWindow::ShowItalic()
{
QTextCharFormat fmt;
fmt.setFontItalic(ui->fontItalicAction->isChecked());
ui->textEdit->mergeCurrentCharFormat(fmt);
}
//下划线
void MainWindow::ShowUnderLine()
{
QTextCharFormat fmt;
fmt.setFontUnderline(ui->fontUnderAction->isChecked());
ui->textEdit->mergeCurrentCharFormat(fmt);
}
//设置字体
void MainWindow::ShowFont()
{
bool ok;
QFont f = QFontDialog::getFont(&ok);
if (ok)
{
ui->textEdit->setFont(f);
}
}
//设置颜色
void MainWindow::ShowColor()
{
QPalette pal = ui->textEdit->palette();
const QColor & color = QColorDialog::getColor(pal.color(QPalette::Base),this);
if (color.isValid())
{
pal.setColor(QPalette::Text,color);
ui->textEdit->setPalette(pal);
}
}
3、运行