Qt5 学习之路及嵌入式开发教程16:Qt5主窗口---格式菜单栏

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、运行

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值