QT GUI代码大全(MainWindow, QFile, QPainter, QGraphicsItem/Scene/View)

本文主要是介绍QT GUI代码大全(MainWindow, QFile, QPainter, QGraphicsItem/Scene/View),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

文章目录

  • 窗口设置
    • QMainWindow类
  • 按钮和菜单
    • QMenuBar类
    • QMenu类
    • QAction类
  • 文件交互
    • QFileDialog类
    • QFileInfo类
    • QFile类
    • QTextStream
  • 绘图
    • QPixmap类
    • QPainter类
    • QBrush类
    • QPen类
    • QPainterPath类
  • 游戏场景
    • QGraphicsItem类
    • QGraphicsScene类
    • QGraphicsView类

窗口设置

QMainWindow类

  • QMainWindow(QWidget *parent = nullptr, Qt::WindowFlags flags = Qt::WindowFlags())

  • void setCentralWidget(QWidget *widget); //set the given widget to the main window’s central widget

  • void setFixedSize(int w, int h); //set the size of the widget

  • void setWindowIcon(QIcon(QString filepath));

按钮和菜单

QMenuBar类

  • QMenuBar *QMainWindow::menuBar() const
    返回MainWindow的menu bar
    //creates and returns an empty menu bar if the menu bar does not exist.

  • QMenuBar::addMenu(QMenu *menu)

  • QMenuBar::addMenu(const QString& title)

QMenu类

  • addAction(QAction *action)
  • addSeparator()

QAction类

可以看成是一个动作,连接到槽

  • QAction(const QString &text, QObject *parent = nullptr)

  • 设置快捷键

void QAction::setShortcuts(const QList<QKeySequence> &shortcuts)
  • ->setStatusTip(tr("Start a new game")); 设置说明
  • ->setEnabled(false) 设置按钮激活状态
  • connect(aboutAction, &QAction::triggered, this, &MainWindow::about);

文件交互

QFileDialog类

用于打开文件选择窗口

QString fileName = QFileDialog::getOpenFileName(this, tr("Open File"),                                       "/home",  //文件夹目录
tr("Images (*.png *.xpm *.jpg)"));

QFileInfo类

用于获取文件的相关信息,比如后缀名等等

QString extension = fileInfo.suffix().toLower();  // 获取小写的文件后缀名

QFile类

  • QFile(QString filename, QObject *parent*);
  • .setFileName(filePath);

QTextStream

文本流,用于读取数据
QTextStream::QTextStream(FILE *fileHandle, QIODevice::OpenMode openMode = QIODevice::ReadWrite)

QTextStream in(&file);
  • 逐行读取
while(!in.atEnd()){QString line = in.readLine(); // 读取一行... // 逐行处理}file.close();

绘图

QPixmap类

贴图,纹理

  • QPixmap::QPixmap(int width, int height)
    //注释:上面尚未fill with color

  • QPixmap::QPixmap(const QString &fileName, const char *format = nullptr, Qt::ImageConversionFlags flags = Qt::AutoColor)

  • void QPixmap::fill(const QColor &color = Qt::white)

QPainter类

  • QPainter(QPaintDevice *device)
    例如
//QPixmap bg(TILE_SIZE, TILE_SIZE);
QPainter p(&bg);
QPainter p(this);
  • setBrush(const QBrush)
  • setPen(QPen)
  • drawRect(int x, int y, int width, int height);
  • drawLine
  • drawPath(const QPainterPath &path) //current pen
  • void QPainter::drawPolygon(const QPolygonF &points, Qt::FillRule fillRule = Qt::OddEvenFill)

void QPainter::drawEllipse(const QRectF &rectangle)

  • save 保存当前painter状态

  • restore 恢复

  • setRenderHint(QPainter::RenderHint hint, bool on = true)
    设置绘画风格
    比如

painter->setRenderHint(QPainter::Antialiasing);
//带有边缘

void QPainter::fillRect(const QRectF]&rectangle, const QBrush&brush)
Fills the given rectangle with the brush specified.

Alternatively, you can specify a QColor instead of a QBrush; the QBrush constructor (taking a QColor argument) will automatically create a solid pattern brush.

相应有 fillPath等等

QBrush类

刷子,可以是纹理/颜色
style设置绘制的方式

QPen类

  pen.setStyle(Qt::DashDotLine);pen.setWidth(3);pen.setBrush(Qt::green);pen.setCapStyle(Qt::RoundCap);pen.setJoinStyle(Qt::RoundJoin);painter.setPen(pen)

QPainterPath类

  • addRect等等

  • clear

  • boundingRect

  • capacity vs length

  • connectPath(&path)

  • contains(QPoint/QRect/QPainterPath)

  • 对应的intersect(相交),而contain是包含(区域)

  • lineTo

  • cubicTo 画线

0 moveTo
Moves the current position to (x, y) and starts a new subpath, implicitly closing the previous path.

游戏场景

QGraphicsItem类

  • setPos(x, y);

  • setData(int key, const QVariant &value);
    使用:serData(GD_Type, GO_Food);

  • QRectF QGraphicsItem::boundingRect() const;
    自定义,原虚函数
    返回Item的边界
    例子:return QRectF(-TILE_SIZE, -TILE_SIZE, TILE_SIZE * 2, TILE_SIZE * 2 );

  • (pure virtual) void QGraphicsItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
    原虚函数,被QGraphicsView调用
    例子:

painter->save();
painter->setRenderHint(QPainter::Antialiasing);
painter->fillPath(shape(), Qt::red);
painter->restore();
  • (virtual) QPainterPath shape()const
    例子:
QPainterPath p;
p.addEllipse(QPointF(TILE_SIZE / 2, TILE_SIZE / 2), FOOD_RADIUS, FOOD_RADIUS);return p;
  • QPointF mapFromScene(const QPointF &point) const
    将Scene坐标系中的坐标映射到本Item坐标系中的点坐标

  • void QGraphicsItem::advance(int phase)
    phase = 0 预更新
    phase = 1 更新
    用于更新Item相关逻辑

void Snake::advance(int step)
{if (!step) {return;}if (tickCounter++ % speed != 0) {return;}if (moveDirection == NoMove) {return;}if (growing > 0) {QPointF tailPoint = head;tail << tailPoint;growing -= 1;} else {tail.removeFirst();tail << head;}switch (moveDirection) {case MoveLeft:moveLeft();break;case MoveRight:moveRight();break;case MoveUp:moveUp();break;case MoveDown:moveDown();break;}setPos(head);handleCollisions();
}
  • void QGraphicsItem::setPos(const QPointF &pos)
    Sets the position of the item to pos, which is in parent coordinates.

  • 碰撞检测

QList<QGraphicsItem*> QGraphicsItem::collidingItems(Qt::ItemSelectionMode mode = Qt::IntersectsItemShape) const

可用之前设置的data判断与哪个物体的碰撞

QGraphicsScene类

//用于图形存放

  • QGraphicsScene(QObject* parent = nullptr);
    构造函数可用:
    …scene(new QGraphicsScene(this));

  • setSceneRect(x, y, w, h);//设置scene的位置
    //使用实例:scene->setSceneRect(-100, -100, 200, 200);

  • void addItem(QGraphicsItem *item);
    void removeItem(QGraphicsItem *item);

  • void QObject::installEventFilter(QObject *filterObj);
    //设置事件过滤器, filterObj会拦截并处理this的实践
    例子: scene.installEventFilter(this);

  • (virtual)
    bool QObject::eventFilter(QObject *object, QEvent *event)
    实现拦截处理函数
    例子

if (event->type() == QEvent::KeyPress) {handleKeyPressed((QKeyEvent *)event); //自定义的按键处理函数return true;//返回已处理
} else {return QObject::eventFilter(object, event);//不处理}
  • connect(&timer, SIGNAL(timeout()), &scene, SLOT(advance()));
    用于定时刷新界面

QGraphicsView类

  • QGraphicsView(scene, this);

  • void fitInView(QRect, Qt::AspectRatioMode aspectRatioMode = Qt::IgnoreAspectRatio);
    缩放视图矩阵并滚动滚动条,以确保场景矩形(rect)适应视口内

  • setBackgroundBrush(QBrush(QPixmap)); //设置背景

这篇关于QT GUI代码大全(MainWindow, QFile, QPainter, QGraphicsItem/Scene/View)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!


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

相关文章

IDEA实现回退提交的git代码(四种常见场景)

《IDEA实现回退提交的git代码(四种常见场景)》:本文主要介绍IDEA实现回退提交的git代码(四种常见场景),具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录1.已提交commit,还未push到远端(Undo Commit)2.已提交commit并push到

Kotlin Compose Button 实现长按监听并实现动画效果(完整代码)

《KotlinComposeButton实现长按监听并实现动画效果(完整代码)》想要实现长按按钮开始录音,松开发送的功能,因此为了实现这些功能就需要自己写一个Button来解决问题,下面小编给大... 目录Button 实现原理1. Surface 的作用(关键)2. InteractionSource3.

使用Java实现Navicat密码的加密与解密的代码解析

《使用Java实现Navicat密码的加密与解密的代码解析》:本文主要介绍使用Java实现Navicat密码的加密与解密,通过本文,我们了解了如何利用Java语言实现对Navicat保存的数据库密... 目录一、背景介绍二、环境准备三、代码解析四、核心代码展示五、总结在日常开发过程中,我们有时需要处理各种软

Java List排序实例代码详解

《JavaList排序实例代码详解》:本文主要介绍JavaList排序的相关资料,Java排序方法包括自然排序、自定义排序、Lambda简化及多条件排序,实现灵活且代码简洁,文中通过代码介绍的... 目录一、自然排序二、自定义排序规则三、使用 Lambda 表达式简化 Comparator四、多条件排序五、

Java 压缩包解压实现代码

《Java压缩包解压实现代码》Java标准库(JavaSE)提供了对ZIP格式的原生支持,通过java.util.zip包中的类来实现压缩和解压功能,本文将重点介绍如何使用Java来解压ZIP或RA... 目录一、解压压缩包1.zip解压代码实现:2.rar解压代码实现:3.调用解压方法:二、注意事项三、总

Linux实现简易版Shell的代码详解

《Linux实现简易版Shell的代码详解》本篇文章,我们将一起踏上一段有趣的旅程,仿照CentOS–Bash的工作流程,实现一个功能虽然简单,但足以让你深刻理解Shell工作原理的迷你Sh... 目录一、程序流程分析二、代码实现1. 打印命令行提示符2. 获取用户输入的命令行3. 命令行解析4. 执行命令

SQL Server身份验证模式步骤和示例代码

《SQLServer身份验证模式步骤和示例代码》SQLServer是一个广泛使用的关系数据库管理系统,通常使用两种身份验证模式:Windows身份验证和SQLServer身份验证,本文将详细介绍身份... 目录身份验证方式的概念更改身份验证方式的步骤方法一:使用SQL Server Management S

Qt之QMessageBox的具体使用

《Qt之QMessageBox的具体使用》本文介绍Qt中QMessageBox类的使用,用于弹出提示、警告、错误等模态对话框,具有一定的参考价值,感兴趣的可以了解一下... 目录1.引言2.简单介绍3.常见函数4.按钮类型(QMessage::StandardButton)5.分步骤实现弹窗6.总结1.引言

Qt中Qfile类的使用

《Qt中Qfile类的使用》很多应用程序都具备操作文件的能力,包括对文件进行写入和读取,创建和删除文件,本文主要介绍了Qt中Qfile类的使用,具有一定的参考价值,感兴趣的可以了解一下... 目录1.引言2.QFile文件操作3.演示示例3.1实验一3.2实验二【演示 QFile 读写二进制文件的过程】4.

uniapp小程序中实现无缝衔接滚动效果代码示例

《uniapp小程序中实现无缝衔接滚动效果代码示例》:本文主要介绍uniapp小程序中实现无缝衔接滚动效果的相关资料,该方法可以实现滚动内容中字的不同的颜色更改,并且可以根据需要进行艺术化更改和自... 组件滚动通知只能实现简单的滚动效果,不能实现滚动内容中的字进行不同颜色的更改,下面实现一个无缝衔接的滚动