Qt5 学习之路及嵌入式开发教程18:Qt5主窗口---字体字号状态栏

Qt5 学习之路及嵌入式开发教程18:Qt5主窗口---字体字号状态栏

这次任务要完成Qt5主窗口的界面设计第五部分:字体字号状态栏功能实现

无法用控件完成,只能用代码完成。

步骤:

1、在mainwidow.h中添加代码

#include <QSpinBox>
#include <QFontComboBox>

在类定义内:

private:
    QLabel           *fLabCurFile;
    QProgressBar     *progressBar1;
    QSpinBox         *spinFontSize;
    QFontComboBox    *comboFont;
    
    void             iniUI();

添加槽函数:

private slots:
    void on_spinBoxFontSize_valueChanged(int aFontSize);
    void on_comboFont_currentIndexChanged(const QString &arg1);

2、在mainwindow.cpp中变量初始化、功能实现

变量和函数初始化:

void MainWindow::iniUI()
{
    fLabCurFile = new QLabel;
    fLabCurFile->setMinimumWidth(150);
    fLabCurFile->setText("当前文件:");
    ui->statusBar->addWidget(fLabCurFile);

    progressBar1 = new QProgressBar;
    progressBar1->setMaximumWidth(200);
    progressBar1->setMinimum(5);
    progressBar1->setMaximum(50);
    progressBar1->setValue(ui->textEdit->font().pointSize());
    ui->statusBar->addWidget(progressBar1);

    spinFontSize = new QSpinBox;
    spinFontSize->setMinimum(5);
    spinFontSize->setMaximum(50);
    spinFontSize->setValue(ui->textEdit->font().pointSize());
    spinFontSize->setMinimumWidth(50);
    ui->toolBar->addSeparator();

    QLabel   *lb_1 = new QLabel("字号 ");
    QFont ft("Microsoft YaHei", 11);
    lb_1->setFont(ft);
    ui->toolBar->addWidget(lb_1);
    ui->toolBar->addWidget(spinFontSize);

    ui->toolBar->addSeparator();

    QLabel   *lb_2 = new QLabel(" 字体  ");
    QFont ft2("Microsoft YaHei", 11);
    lb_2->setFont(ft2);
    ui->toolBar->addWidget(lb_2);
    comboFont = new QFontComboBox;
    comboFont->setMinimumWidth(100);
    ui->toolBar->addWidget(comboFont);

    setCentralWidget(ui->textEdit);
}

在ui->setupUi(this);下一行添加

iniUI();

槽函数:

connect(spinFontSize,SIGNAL(valueChanged(int)),this,SLOT(on_spinBoxFontSize_valueChanged(int)));
  

connect(comboFont,SIGNAL(currentIndexChanged(QString)),this,SLOT(on_comboFont_currentIndexChanged(QString)));

功能实现:

void MainWindow::on_spinBoxFontSize_valueChanged(int aFontSize)
{
    QTextCharFormat     fmt;
    fmt.setFontPointSize(aFontSize);
    ui->textEdit->mergeCurrentCharFormat(fmt);
    progressBar1->setValue(aFontSize);
}

void MainWindow::on_comboFont_currentIndexChanged(const QString &arg1)
{
    QTextCharFormat     fmt;
    fmt.setFontFamily(arg1);
    ui->textEdit->mergeCurrentCharFormat(fmt);
}

3、运行结果:

添加格式刷部分

定义变量及函数部分

1、在头文件中添加

    //格式刷初始化
    void initalFormatBrush();

    //重写事件过滤器,实现点击松开改格式
    bool eventFilter(QObject* obj,QEvent* e);

2、定义格式刷变量

private:

    QAction         *actionFormatBursh; //格式刷

    
    QTextCharFormat brushCharFormat;    //格式刷储存的字体

3、定义槽函数

private slots:
    //格式刷
    void showFormatBrush();

初始化及函数实现部分

1、构建函数内

    //注册ui->textEdit 事件过滤器  如果要监听QTextEdit 控件的鼠标事件,在
    //ui->textEdit->installEventFilter(this);
    ui->textEdit->viewport()->installEventFilter(this);


    //格式刷初始化
    initalFormatBrush();

2、功能部分

/************格式初始化****************/
void MainWindow::initalFormatBrush()
{
    //设置分割线
    ui->toolBar_2->addSeparator();
    //格式刷
    actionFormatBursh=new QAction(QIcon(":/formatBrush.png"),"格式刷");
    ui->toolBar_2->addAction(actionFormatBursh);

    connect(actionFormatBursh,&QAction::triggered,this,&MainWindow::showFormatBrush);
}
//重写事件过滤器,实现点击松开改格式
bool MainWindow::eventFilter(QObject *obj, QEvent *e)
{
    if(obj==ui->textEdit->viewport()){
        if(actionFormatBursh->isCheckable()){
            //触发格式效果

            //鼠标点击,获取该光标处字体信息
            if(e->type()==QEvent::MouseButtonPress){
                //qDebug()<<"按下";
                QMouseEvent* ev=static_cast<QMouseEvent*>(e);
                if(ev->button()==Qt::LeftButton){
                    QTextCursor textCursor=ui->textEdit->textCursor();
                    this->brushCharFormat = textCursor.charFormat();
                    //qDebug()<<this->brushCharFormat.fontPointSize();
                }
            }
            //鼠标释放更改字体
            if(e->type()==QEvent::MouseButtonRelease){
                //qDebug()<<this->brushCharFormat.fontPointSize();
                //qDebug()<<"释放";
                QMouseEvent* ev=static_cast<QMouseEvent*>(e);
                if(ev->button()==Qt::LeftButton){
                   ui->textEdit->setCurrentCharFormat(brushCharFormat);
                   showFormatBrush();
                }
            }
        }
    }
    return QObject::eventFilter(obj,e);
}

void MainWindow::showFormatBrush()
{
    if(!actionFormatBursh->isCheckable()){
        actionFormatBursh->setCheckable(true);
        actionFormatBursh->setChecked(true);

        QCursor cursor;
        QPixmap pixmap(":/cursorBrush.png");
        QSize picSize(26,26);
        QPixmap scaledPixmap = pixmap.scaled(picSize, Qt::KeepAspectRatio);//按比例缩放
        cursor=QCursor(scaledPixmap);
        ui->textEdit->viewport()->setCursor(cursor);
    }else{
        actionFormatBursh->setCheckable(false);
        actionFormatBursh->setChecked(false);
        ui->textEdit->viewport()->setCursor(QCursor(Qt::ArrowCursor));
    }
}

功能演示:

 

  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Qt Designer是PyQt5中的一个可视化设计工具,它可以帮助Python开发者快速创建GUI应用程序的用户界面。Qt Designer提供了丰富的控件库,可以快速创建常见的GUI组件,如按钮、标签、文本框等,并且可以自定义控件的属性、布局以及信号槽等。下面是Qt Designer的所有详细功能及使用教程。 ## Qt Designer的所有详细功能 ### 1. 控件库 Qt Designer提供了丰富的控件库,包括基本控件、布局控件、对话框、菜单栏等。这些控件可以直接拖拽到界面设计器中,并进行属性设置和布局。 ### 2. 属性设置Qt Designer中,可以通过属性编辑器设置控件的属性,如大小、位置、文本、字体、颜色等。同时,还可以设置控件的样式表、信号槽等。属性编辑器界面可以通过点击控件显示出来。 ### 3. 布局管理器 Qt Designer中提供了多种布局管理器,如水平布局、垂直布局、网格布局等。通过布局管理器,可以轻松地调整控件的位置和大小,使得控件在不同分辨率的屏幕上都能适应。同时,还可以设置控件之间的间隔和对齐方式。 ### 4. 信号槽 Qt Designer中提供了信号槽编辑器,可以通过拖拽控件之间的连接线来设置信号和槽。信号是控件发出的事件,如按钮被点击、文本框内容改变等;槽是处理信号的函数,可以在代码中实现。通过信号槽机制,可以实现控件之间的交互。 ### 5. 预览和调试 在Qt Designer中,可以通过预览功能来查看设计的界面效果。同时,还可以通过连接到Python解释器来进行调试,调试过程中可以查看控件的属性和信号槽的连接情况。 ## 使用教程 ### 1. 安装pyqt5-tools 首先需要安装pyqt5-tools,可以通过pip来安装: ``` pip install pyqt5-tools ``` ### 2. 打开Qt Designer 安装完成后,在命令行中输入以下命令打开Qt Designer: ``` designer ``` ### 3. 创建新界面 打开Qt Designer后,可以选择新建一个界面或打开一个已有的界面。新建一个界面可以通过点击“File”->“New”->“Main Window”来创建。 ### 4. 添加控件 在左侧的控件库中选择需要添加的控件,然后将其拖拽到界面设计器中。控件的属性可以在右侧的属性编辑器中进行设置。 ### 5. 设置布局 通过选择相应的布局管理器,可以设置控件的布局。布局管理器可以在左侧的控件库中找到。此外,还可以通过手动调整控件的位置和大小来进行布局设置。 ### 6. 设置信号槽 在信号槽编辑器中,可以通过拖拽控件之间的连接线来设置信号和槽。信号和槽的设置可以在代码中实现。 ### 7. 预览和调试 在Qt Designer中,可以通过预览功能来查看设计的界面效果。同时,还可以通过连接到Python解释器来进行调试,调试过程中可以查看控件的属性和信号槽的连接情况。 ### 8. 保存界面 完成设计后,可以通过“File”->“Save”来保存界面文件。界面文件的后缀名为.ui,可以在代码中使用pyuic5工具将其转换为Python代码。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值