Qt复习第二天

1、菜单栏工具栏状态栏

#include "mainwindow.h"
#include "ui_mainwindow.h"
#pragma execution_character_set("utf-8");
MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    //菜单
    QMenu *editMenu = ui->menubar->addMenu("编辑(&E)");
    QAction*action_copy = editMenu->addAction(QString("复制(&C)"));//前面还可以写个QIcon
    action_copy->setShortcut(QKeySequence("Ctrl + C"));

    //工具栏操作
    ui->toolBar->addAction(action_copy);

    //动作加入分组
    QActionGroup*group = new QActionGroup(this);
    QAction*action_L = group->addAction(("左对齐(&L)"));
    QAction*action_R = group->addAction(("左对齐(&R)"));

    action_L->setCheckable(true);
    action_R->setCheckable(true);

    editMenu->addSeparator();//添加分割线

    //工具栏添加部件
    QToolButton*toolBtn = new QToolButton(this);
    toolBtn->setText("颜色");

    //工具栏设置菜单
    QMenu *colorMenu = new QMenu(this);
    colorMenu->addAction("绿色");
    colorMenu->addAction("红色");
    toolBtn->setMenu(colorMenu);

    toolBtn->setPopupMode(QToolButton::MenuButtonPopup);//弹出的菜单

    ui->toolBar->addWidget(toolBtn);

    QSpinBox*spinBox = new QSpinBox(this);
    ui->toolBar->addWidget(spinBox);

    //状态栏显示信息
    ui->statusbar->showMessage(QString("欢迎。。。"),3000);

    //添加永久的
    QLabel*tag = new QLabel("wcoao");
    ui->statusbar->addPermanentWidget(tag);



}

MainWindow::~MainWindow()
{
    delete ui;
}


实现效果如下:
在这里插入图片描述
在这里插入图片描述

2、自定义菜单栏

1、需要继承于QWidgetAction
写一个Myaction类

#ifndef MYACTION_H
#define MYACTION_H

#include <QWidgetAction>
#include <QObject>
#include<QLineEdit>
class Myaction : public QWidgetAction
{
    Q_OBJECT
public:
    explicit Myaction(QObject *parent = nullptr);

protected:
    QWidget*createWidget(QWidget*parent);

signals:
    void getText(const QString&string);

private slots:
    void sendText();

private:
    QLineEdit*lineedit;
};

#endif // MYACTION_H


#include "myaction.h"
#include<QSplitter>
#include<QLabel>
Myaction::Myaction(QObject *parent)
    : QWidgetAction{parent}
{
    lineedit = new QLineEdit;
    connect(lineedit,&QLineEdit::returnPressed,this,&Myaction::sendText);

}

QWidget *Myaction::createWidget(QWidget *parent)
{
    //判断父部件是否继承菜单或者工具栏
    //如果创建部件的子部件并放回子部件
    if(parent->inherits("QMenu")||parent->inherits("QToolBar"))
    {
        QSplitter *splitter = new QSplitter(parent);
        QLabel*label = new QLabel("插入文本");
        splitter->addWidget(label);
        splitter->addWidget(lineedit);
        return splitter;
    }
    return 0;
}

void Myaction::sendText()
{
    emit getText(lineedit->text());
    lineedit->clear();
}

QMainWindow::
    Myaction*action = new Myaction;
    QMenu*menu = ui->menubar->addMenu("编辑(&E)");
    menu->addAction(action);
    connect(action,&Myaction::getText,this,&MainWindow::setText);


void MainWindow::setText(const QString &str)
{
    ui->textEdit->setText(str);
}


3、富文本

#include "mainwindow.h"
#include "ui_mainwindow.h"


#include<QTextFrame> //富文本
MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);

    //获取文档对象
    QTextDocument *document = ui->textEdit->document();

    //获取根框架
    QTextFrame *rootFrame = document->rootFrame();

    //文档格式框架
    QTextFrameFormat format;
    format.setBorderBrush(Qt::red);
    format.setBorder(3);

    //文档框架设置格式
    rootFrame->setFrameFormat(format);

    //设置文本边框风格
    QTextFrameFormat frameFormat;
    frameFormat.setBackground(Qt::lightGray);
    frameFormat.setMargin(10);
    frameFormat.setPadding(15);
    frameFormat.setBorder(2);
    frameFormat.setBorderStyle(QTextFrameFormat::BorderStyle_DotDash);

    QTextCursor cursor = ui->textEdit->textCursor();
    cursor.insertFrame(frameFormat);
}

MainWindow::~MainWindow()
{
    delete ui;
}


效果:
在这里插入图片描述

4、文本框块字符格式

#include "mainwindow.h"
#include "ui_mainwindow.h"
#include<QAction>
#include<QTextDocument>
#include<QTextFrame>
#include<QDebug>
#include<QTextBlockFormat>
#pragma execution_character_set("utf-8");
MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    QAction *action_textFrame = new QAction("框架",this);
    connect(action_textFrame,&QAction::triggered,this,&MainWindow::showtextFrame);

    QAction*action_textBlock = new QAction("文本块",this);
    connect(action_textBlock,&QAction::triggered,this,&MainWindow::showTextBlock);
    QAction*action_setText = new QAction("字体",this);
    connect(action_setText, SIGNAL(triggered(bool)), this, SLOT(setTextFont(bool)));

    ui->toolBar->addAction(action_setText);
    ui->toolBar->addAction(action_textBlock);
    ui->toolBar->addAction(action_textFrame);
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::showtextFrame() //文本框架
{
    QTextDocument* document = ui->textEdit->document();
    QTextFrame* frame = document->rootFrame();

    QTextFrame::iterator it;
    for (it = frame->begin(); !it.atEnd(); ++it)
    {
        //获取当前框架的指针
        QTextFrame* childframe = it.currentFrame();

        QTextBlock childBlock = it.currentBlock();
        if (childframe)
        {
            qDebug() << "frame";
        }
        else if (childBlock.isValid())
        {
            qDebug() << "block " << childBlock.text();
        }
    }
}


void MainWindow::setTextFont(bool checked)
{
    if(!checked)
    {
        QTextCursor cursor= ui->textEdit->textCursor();
        //文本块格式
        QTextBlockFormat blockFormat;
        //居中对齐
        blockFormat.setAlignment(Qt::AlignCenter);
        cursor.insertBlock(blockFormat);

        //字符格式
        QTextCharFormat charFormat;
        //字符背景色
        charFormat.setBackground(Qt::lightGray);
        //前景色
        charFormat.setForeground(Qt::blue);

        //字体
        charFormat.setFont(QFont(QString("宋体"),12,QFont::Bold,true));

        //下划线
        charFormat.setFontUnderline(true);
        //设置字符格式
        cursor.setCharFormat(charFormat);
        cursor.insertText("哈哈哈");
    }
    qDebug()<<checked;
}

void MainWindow::showTextBlock()
{
    QTextDocument *document = ui->textEdit->document();
    QTextBlock block = document->firstBlock();

    for(int i = 0;i<document->blockCount();i++)
    {
        qDebug()<<QString("文本块:%1,文本首行行号为: %2,长度为%3,内容为%4").arg(i).arg(block.firstLineNumber()
).arg(block.length()).arg(block.text());
        block = block.next();
    }
}


效果如下:
在这里插入图片描述

5、文档插入表格列表图片

#include "mainwindow.h"
#include "ui_mainwindow.h"
#include<QAction>
#include<QTextTableFormat>
#include<QTextCursor>
#include<QTextListFormat>
#include<QFileDialog>
#include<QImageReader>
#include<QTextImageFormat>
#pragma execution_character_set("utf-8");
MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    QAction*action_table = new QAction("表格",this);
    connect(action_table,SIGNAL(triggered(bool)),this,SLOT(insertTable()));

    QAction*action_list = new QAction("列表",this);
    connect(action_list,SIGNAL(triggered(bool)),this,SLOT(insertList()));

    QAction*action_pic = new QAction("表格",this);
    connect(action_pic,SIGNAL(triggered(bool)),this,SLOT(insertImage()));

    ui->toolBar->addAction(action_list);
    ui->toolBar->addAction(action_table);
    ui->toolBar->addAction(action_pic);
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::insertTable() //插入图
{
    QTextCursor cursor = ui->textEdit->textCursor();
    QTextTableFormat format;//表格格式
    format.setCellSpacing(2);
    format.setCellPadding(10);
    cursor.insertTable(3,3,format);
}

void MainWindow::insertList() //插入列表
{
    QTextListFormat format;//列表格式
    format.setStyle(QTextListFormat::ListDecimal);//数字编号
    ui->textEdit->textCursor().insertList(format);
}

void MainWindow::insertImage()
{
    QString filepath = QFileDialog::getOpenFileName(this,"选择图片",".",
"JPEG(*.jpg *.jpeg);;""GIF(*.gif);;""PNG(*.png)");

    QUrl url(QString("file://%1").arg(filepath));
    QImage image = QImageReader(filepath).read();

    QTextDocument*document = ui->textEdit->document();
    //文档添加图片资源
    document->addResource(QTextDocument::ImageResource,url,QVariant(image));

    QTextCursor cursor = ui->textEdit->textCursor();
    QTextImageFormat imgFormat;
    imgFormat.setWidth(image.width());
    imgFormat.setHeight(image.height());
    imgFormat.setName(url.toString());
    cursor.insertImage(imgFormat);

}


效果展示:
在这里插入图片描述

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mfbz.cn/a/611214.html

如若内容造成侵权/违法违规/事实不符,请联系我们进行投诉反馈qq邮箱809451989@qq.com,一经查实,立即删除!

相关文章

粤嵌—2024/4/26—跳跃游戏 ||

代码实现&#xff1a; 方法一&#xff1a;回溯 历史答案剪枝优化——超时 int *dis;void dfs(int k, int startindex, int *nums, int numsSize) {if (dis[startindex] < k) {return;}dis[startindex] k;for (int i 0; i < nums[startindex]; i) {if (startindex i &…

嫁接打印的技术要点

所谓嫁接打印&#xff0c;是一种增减材混合制造的方式。它将已成形的模具零件当作基座&#xff0c;在此基础上“生长”出打印的零件。其中基座通常采用传统加工方式制造&#xff0c;而打印部分则使用专用的金属粉末&#xff0c;通过 3D 打印技术成型。 嫁接打印之所以备受欢迎&…

4.nginx.pid打开失败以及失效的解决方案

一. nginx.pid打开失败以及失效的解决方案 1.错误图片&#xff1a; 2.解决方法 步骤1&#xff1a;进入这个目录 /var/run/nginx,提示没有文件或目录&#xff0c;则使用mkdir创建这个目录。 步骤2&#xff1a;然后 ./nginx -s reload 运行,是一个无效的PID 步骤3&#xff1a;使…

SMI接口

目录 SMI 接口帧格式读时序写时序 IP 设计IP 例化界面IP 接口IP 验证 SMI 接口 SMI&#xff08;Serial Management Interface&#xff09;串行管理接口&#xff0c;也被称作 MII 管理接口&#xff08;MII Management Interface&#xff09;&#xff0c;包括 MDC 和 MDIO 两条信…

【字符串】Leetcode 二进制求和

题目讲解 67. 二进制求和 算法讲解 为了方便计算&#xff0c;我们将两个字符串的长度弄成一样的&#xff0c;在短的字符串前面添加字符0&#xff1b;我们从后往前计算&#xff0c;当遇到当前计算出来的字符是> 2’的&#xff0c;那么就需要往前面进位和求余 注意&#xf…

《QT实用小工具·六十二》基于QT实现贝塞尔曲线画炫酷的波浪动画

1、概述 源码放在文章末尾 该项目实现了通过贝塞尔曲线画波浪动画&#xff0c;可控制 颜色密度速度加速度 安装与运行环境 语言&#xff1a;C 框架&#xff1a;Qt 11.3 平台&#xff1a;Windows 将屏幕水平平均分为10块&#xff0c;在一定范围内随机高度的12个点&#xff08;…

OAuth 2.0 和 OAuth 2.1

OAuth 2.0 和 OAuth 2.1比较&#xff1a; OAuth 2.0 和 OAuth 2.1 是授权框架的不同版本&#xff0c;它们用于允许应用程序安全地访问用户在另一个服务上的数据。以下是它们之间的一些主要区别&#xff1a; 安全性增强&#xff1a;OAuth 2.1 旨在提高安全性&#xff0c;它整合…

C语言/数据结构——每日一题(移除链表元素)

一.前言 今天在leetcode刷到了一道关于单链表的题。想着和大家分享一下。废话不多说&#xff0c;让我们开始今天的知识分享吧。 二.正文 1.1题目要求 1.2思路剖析 我们可以创建一个新的单链表&#xff0c;然后通过对原单链表的遍历&#xff0c;将数据不等于val的节点移到新…

MySQL索引(聚簇索引、非聚簇索引)

了解MySQL索引详细&#xff0c;本文只做整理归纳&#xff1a;https://blog.csdn.net/wangfeijiu/article/details/113409719 概念 索引是对数据库表中一列或多列的值进行排序的一种结构&#xff0c;使用索引可快速访问数据库表中的特定信息。 索引分类 主键索引&#xff1a…

微信群发用什么软件最安全?微信群发软件哪个好?微信群发助手软件在哪里?

今天给大家推荐一款我们目前在使用的电脑群发工具掘金小蜜&#xff0c;不仅可以无限多开&#xff0c;方便你同时管理多个账号&#xff0c;群发功能更是十分强大&#xff0c;轻松释放你的双手。 掘金小蜜&#xff08;只支持Win7及以上操作系统&#xff0c;没有推Mac版和手机客户…

【算法入门赛】B. 自助店评分(C++、STL、推荐学习)题解与代码

比赛地址&#xff1a;https://www.starrycoding.com/contest/8 题目描述 在上一场的入门教育赛中&#xff0c;牢 e e e找到了所有自助店的位置&#xff0c;但是他想发现一些“高分好店”&#xff0c;于是他利用爬虫技术从“小众点评APP”中爬取了武汉所有自助店的评分。 评分…

[笔试训练](十八)

目录 052:字符串压缩 053:chika和蜜柑 054:01背包 052:字符串压缩 压缩字符串(一)_牛客题霸_牛客网 (nowcoder.com) 题目&#xff1a; 题解&#xff1a; 双指针模拟 class Solution { public:string compressString(string param) {int nparam.size();string ret;int num…

【线性代数】英语版听课笔记

线性代数 - 北京航天航空大学&#xff08;英文版&#xff09;_哔哩哔哩_bilibili 39.concept of vector space in this lecture we will studyvector space&#xff0c; the concept of basis dimension and coordinates 向量空间的维数&#xff1a;向量空间的基底所含向量的…

wandb: - 0.000 MB of 0.011 MB uploaded持续出现的解决方案

大家好,我是爱编程的喵喵。双985硕士毕业,现担任全栈工程师一职,热衷于将数据思维应用到工作与生活中。从事机器学习以及相关的前后端开发工作。曾在阿里云、科大讯飞、CCF等比赛获得多次Top名次。现为CSDN博客专家、人工智能领域优质创作者。喜欢通过博客创作的方式对所学的…

界面组件DevExpress Blazor UI v23.2新版亮点:图表组件全新升级

DevExpress Blazor UI组件使用了C#为Blazor Server和Blazor WebAssembly创建高影响力的用户体验&#xff0c;这个UI自建库提供了一套全面的原生Blazor UI组件&#xff08;包括Pivot Grid、调度程序、图表、数据编辑器和报表等&#xff09;。 DevExpress Blazor控件目前已经升级…

四边形不等式优化dp,超详细,概念定理详解证明,OJ练习

零、前言 四边形不等式最早是 D.E.Knuth 在优化传统O(n3)区间dp求解最优二叉检索树为O(n2)所采用的方法,可惜原论文的证明跳跃性过强,阅读门槛较高. 不过随着算竞的发展,四边形不等式已经有了较为完备的可参考资料,本文进行介绍. 一、再看[石子合并] 石子合并是区间dp的经典入…

【vue3-pbstar-big-screen】一款基于vue3、vite、ts的大屏可视化项目

vue3-pbstar-big-screen是一款基于vue3、vite、ts的大屏可视化项目&#xff0c;项目已内置axios、sass&#xff0c;如element、echarts等需要自行安装。 屏幕适配方案 本项目主要通过transform: scale()缩放核心区域实现屏幕适配效果 //html <div class"container-wr…

IDEA无法下载远程仓库jar包问题

问题描述&#xff1a; idea无法下载远程仓库jar包&#xff0c;最奇怪的是idea有多个项目&#xff0c;有些项目可以下载&#xff0c;有些项目不行。报错如下&#xff1a; 一开始&#xff1a; unable to find valid certification path to requested target Try run Maven impo…

Adobe Premiere Pro安装

一、安装包下载 链接&#xff1a;https://pan.baidu.com/s/1aYqTSQQutDguKYZE-yNHiw?pwd72l8 提取码&#xff1a;72l8 二、安装步骤 1.鼠标右击【Pr2024(64bit)】压缩包&#xff08;win11及以上系统需先点击“显示更多选项”&#xff09;【解压到 Pr2024(64bit)】。 2.打开…

ICode国际青少年编程竞赛- Python-4级训练场-太阳能板1

ICode国际青少年编程竞赛- Python-4级训练场-太阳能板1 1、 Dev.step(3) Dev.turnRight() Dev.step(2) while Dev.energy < 60:wait() Dev.step(-6)2、 Dev.step(7) while Dev.energy < 90:wait() Dev.step(-1) Dev.turnRight() Dev.step(7)3、 Dev.step(4) Dev.turn…