一、文件 QFile
QT提供了QFile类用于对文件进行读写操作,也提供了其他的两个类:文本流(QTextSream)和数据流(QDataStream)
1、对文件进行操作流程:
如何找到文件?--------使用文件对话框(QFileDialog),也可以直接使用QFile类去找到文件
2、打开文件
bool open(FILE *fh, QIODevice::OpenMode mode, QFileDevice::FileHandleFlags handleFlags = DontCloseHandle)
参数:FILE*fh文件指针
参数:QIODevice::OpenMode mode //打开文件模式
参数:QFileDevice::FileHandleFlags handleFlags = DontCloseHandle //文件处理标志
---------------------------------------------------------------
bool open(int fd, QIODevice::OpenMode mode, QFileDevice::FileHandleFlags handleFlags = DontCloseHandle)
参数:int fd :文件ID
参数:QIODevice::OpenMode mode //打开文件模式
参数:QFileDevice::FileHandleFlags handleFlags = DontCloseHandle //文件处理标志
---------------------------------------------------------------
virtual bool open(QIODevice::OpenMode mode) override
参数:QIODevice::OpenMode mode //打开文件模式
3、读/写
virtual qint64 readData(char *data, qint64 len) override
返回值:qint64=long long int
参数:char *data里读取数据
参数:qint64 读取数据的长度
-----------------------------
//按行读取
virtual qint64 readLineData(char *data, qint64 maxlen) override
返回值:qint64=long long int
参数:char *data里读取数据
参数:qint64 读取数据的最大长度
-----------------------------
QByteArray read(qint64 maxSize)
返回值:QByteArray:字节数组 //文本数据、网络相关的数据等等
参数:qint64 maxSize;最大能读取数据长度
-----------------------------
//读取文件里的所有数据
QByteArray readAll()
返回值:QByteArray:字节数组 //文本数据、网络相关的数据等等
-----------------------------
//按行读取
QByteArray readLine(qint64 maxSize = 0)
返回值:QByteArray:字节数组 //文本数据、网络相关的数据等等
参数:qint64 maxSize = 0 //读取一行数据
virtual qint64 writeData(const char *data, qint64 len) override
返回值:qint64,返回写入的字节个数
参数const char *data:待写入的字符串
参数qint64 len:待写入的字符串的长度
-----------------------------
qint64 write(const char *data, qint64 maxSize)
返回值:qint64,返回写入的字节个数
参数const char *data:待写入的字符串
参数qint64 len:待写入的字符串最大的长度
-----------------------------
qint64 write(const char *data)
返回值:qint64,返回写入的字节个数
参数const char *data:待写入的字符串
-----------------------------
qint64 write(const QByteArray &byteArray)
返回值:qint64,返回写入的字节个数
参数const QByteArray &byteArray:待写入的字节数组
4、关闭文件
virtual void close() override
用于查看文件的基本信息的类
QDir absoluteDir() const //获取文件的绝对目录
QString absoluteFilePath() const //获取文件的绝对路径(包含文件)
QString absolutePath() const //获取文件的绝对路径(不包含文件)
QString baseName() const // 获取文件名称(不包含后缀)
QDateTime birthTime() const //获取文件的创建时间
QString completeSuffix() const //获取文件的后缀
QDir dir() const //获取文件的目录
bool exists() const //判断文件是否存在,true:存在 false:不存在
QString fileName() const //获取文件名(包好后缀)
QString filePath() const //获取文件的相对路径
QDateTime fileTime(QFile::FileTime time) const //返回文件被修改的时间
bool isHidden() const //判断文件是否被隐藏 true:隐藏 false:不隐藏
bool isReadable() const //判断文件是否是只读 ture:只读 false:不只读
bool permission(QFile::Permissions permissions) const //设置文件的许可证(权限)true:设置成功 false:设置失败
QDateTime lastRead() const //获取最后一次读取文件的时间
用于管理目录
QString absoluteFilePath(const QString &fileName) const //获取文件的路径
QString absolutePath() const
QString canonicalPath() const
bool cd(const QString &dirName) //切换文件路径
bool cdUp() //切换到上一个路径:cd ../
bool mkdir(const QString &dirName) const //创建目录或文件
bool mkpath(const QString &dirPath) const //创建路径
bool rename(const QString &oldName, const QString &newName) //更新文件名称
bool rmdir(const QString &dirName) const //移除目录,注意:目录或路径下所有文件都删掉(慎用)
bool rmpath(const QString &dirPath) const //移除路径
QDir current()
QString currentPath()
二、事件及图形
QT事件的处理流程:
virtual void actionEvent(QActionEvent *event) //当窗口状态行为被改变时,会执行该事件
virtual void changeEvent(QEvent *event)
virtual void closeEvent(QCloseEvent *event) //当窗口被关闭时会执行该事件,
virtual void contextMenuEvent(QContextMenuEvent *event)
void create(WId window = 0, bool initializeWindow = true, bool destroyOldWindow = true)
void destroy(bool destroyWindow = true, bool destroySubWindows = true)
virtual void dragEnterEvent(QDragEnterEvent *event)
virtual void dragLeaveEvent(QDragLeaveEvent *event)
virtual void dragMoveEvent(QDragMoveEvent *event)
virtual void dropEvent(QDropEvent *event)
virtual void enterEvent(QEvent *event)
virtual void focusInEvent(QFocusEvent *event)
bool focusNextChild()
virtual bool focusNextPrevChild(bool next)
virtual void focusOutEvent(QFocusEvent *event)
bool focusPreviousChild()
virtual void hideEvent(QHideEvent *event) //当窗口被隐藏时会执行该事件
virtual void inputMethodEvent(QInputMethodEvent *event)
virtual void keyPressEvent(QKeyEvent *event) //当键盘按下的时候,会执行该事件
virtual void keyReleaseEvent(QKeyEvent *event) //当键盘释放的时候,会执行该事件
virtual void leaveEvent(QEvent *event)
virtual void mouseDoubleClickEvent(QMouseEvent *event)
virtual void mouseMoveEvent(QMouseEvent *event)
virtual void mousePressEvent(QMouseEvent *event)
virtual void mouseReleaseEvent(QMouseEvent *event)
virtual void moveEvent(QMoveEvent *event)
virtual bool nativeEvent(const QByteArray &eventType, void *message, long *result)
virtual void paintEvent(QPaintEvent *event)
virtual void resizeEvent(QResizeEvent *event)
virtual void showEvent(QShowEvent *event)
virtual void tabletEvent(QTabletEvent *event)
virtual void wheelEvent(QWheelEvent *event)
bool QObject::eventFilter(QObject *watched, QEvent *event) // QObject类中的时间过滤器
返回值:bool类型 当事件过滤成功,则返回true, 否则,返回false
参数1:QObject *watched 子窗口部件对象
参数2:QEvent *event 在子窗口部件对象上产生的事件的类型
-------------------------------------------------------------------
//使用:首先需要向指定的子窗口部件注册事件过滤器,之后即可使用事件过滤器
//注册事件过滤器:
void installEventFilter(QObject *filterObj) //注册事件过滤器
事件分发器原型:
bool QObject::event(QEvent *e) //事件分发器
返回值:bool 分发事件的返回值
参数:QEvent *e //分发事件的类型
QT里定时器的实现两种方式:
(1)实例化QTimer类
(2)重写定时器事件
void QObject::timerEvent(QTimerEvent *event) //定时器事件
(1)实例化QTimer类
void start(std::chrono::milliseconds msec) //开启定时器
void start(int msec) //开启定时器并设置定时时间,单位:毫秒
void start() //开启定时器
void stop() //停止定时器
---------------------------------------------------------
信号:
void timeout() //当定时器开启之后,到达设置的时间,触发该信号
(2)重写定时器事件
void QObject::timerEvent(QTimerEvent *event) //定时器事件
int startTimer(int interval, Qt::TimerType timerType = Qt::CoarseTimer) //开启定时器
int startTimer(std::chrono::milliseconds time, Qt::TimerType timerType = Qt::CoarseTimer) //开启定时器
返回值:int 返回定时器的ID号
virtual void paintEvent(QPaintEvent *event) //绘画事件
当应用程序界面刷新一次,就会自动调用paintEvent事件处理函数
调用update()函数,也可以实现调用paintEvent事件处理函数
QPainter类:
一支不带彩色的画笔,默认的颜色为黑色
void drawArc(const QRectF &rectangle, int startAngle, int spanAngle)
void drawArc(const QRect &rectangle, int startAngle, int spanAngle)
void drawArc(int x, int y, int width, int height, int startAngle, int spanAngle)
void drawChord(const QRectF &rectangle, int startAngle, int spanAngle)
void drawChord(int x, int y, int width, int height, int startAngle, int spanAngle)
void drawChord(const QRect &rectangle, int startAngle, int spanAngle)
void drawConvexPolygon(const QPointF *points, int pointCount)
void drawConvexPolygon(const QPolygonF &polygon)
void drawConvexPolygon(const QPoint *points, int pointCount)
void drawConvexPolygon(const QPolygon &polygon)
void drawEllipse(const QRectF &rectangle)
void drawEllipse(const QRect &rectangle)
void drawEllipse(int x, int y, int width, int height)
void drawEllipse(const QPointF ¢er, qreal rx, qreal ry)
void drawEllipse(const QPoint ¢er, int rx, int ry)
void drawGlyphRun(const QPointF &position, const QGlyphRun &glyphs)
void drawImage(const QRectF &target, const QImage &image, const QRectF &source, Qt::ImageConversionFlags flags = Qt::AutoColor)
void drawImage(const QRect &target, const QImage &image, const QRect &source, Qt::ImageConversionFlags flags = Qt::AutoColor)
void drawImage(const QPointF &point, const QImage &image, const QRectF &source, Qt::ImageConversionFlags flags = ...)
void drawImage(const QPoint &point, const QImage &image, const QRect &source, Qt::ImageConversionFlags flags = ...)
void drawImage(const QRectF &rectangle, const QImage &image)
void drawImage(const QRect &rectangle, const QImage &image)
void drawImage(const QPointF &point, const QImage &image)
void drawImage(const QPoint &point, const QImage &image)
void drawImage(int x, int y, const QImage &image, int sx = 0, int sy = 0, int sw = -1, int sh = -1, Qt::ImageConversionFlags flags = Qt::AutoColor)
void drawLine(const QLineF &line)
void drawLine(const QLine &line)
void drawLine(int x1, int y1, int x2, int y2)
void drawLine(const QPoint &p1, const QPoint &p2)
void drawLine(const QPointF &p1, const QPointF &p2)
void drawLines(const QLineF *lines, int lineCount)
void drawLines(const QVector<QLineF> &lines)
void drawLines(const QPointF *pointPairs, int lineCount)
void drawLines(const QVector<QPointF> &pointPairs)
void drawLines(const QLine *lines, int lineCount)
void drawLines(const QVector<QLine> &lines)
void drawLines(const QPoint *pointPairs, int lineCount)
void drawLines(const QVector<QPoint> &pointPairs)
void drawPath(const QPainterPath &path)
void drawPicture(const QPointF &point, const QPicture &picture)
void drawPicture(int x, int y, const QPicture &picture)
void drawPicture(const QPoint &point, const QPicture &picture)
void drawPie(const QRectF &rectangle, int startAngle, int spanAngle)
void drawPie(int x, int y, int width, int height, int startAngle, int spanAngle)
void drawPie(const QRect &rectangle, int startAngle, int spanAngle)
void drawPixmap(const QRectF &target, const QPixmap &pixmap, const QRectF &source)
void drawPixmap(const QRect &target, const QPixmap &pixmap, const QRect &source)
void drawPixmap(int x, int y, int w, int h, const QPixmap &pixmap, int sx, int sy, int sw, int sh)
void drawPixmap(int x, int y, const QPixmap &pixmap, int sx, int sy, int sw, int sh)
void drawPixmap(const QPointF &point, const QPixmap &pixmap, const QRectF &source)
void drawPixmap(const QPoint &point, const QPixmap &pixmap, const QRect &source)
void drawPixmap(const QPointF &point, const QPixmap &pixmap)
void drawPixmap(const QPoint &point, const QPixmap &pixmap)
void drawPixmap(int x, int y, const QPixmap &pixmap)
void drawPixmap(const QRect &rectangle, const QPixmap &pixmap)
void drawPixmap(int x, int y, int width, int height, const QPixmap &pixmap)
void drawPixmapFragments(const QPainter::PixmapFragment *fragments, int fragmentCount, const QPixmap &pixmap, QPainter::PixmapFragmentHints hints = ...)
void drawPoint(const QPointF &position)
void drawPoint(const QPoint &position)
void drawPoint(int x, int y)
void drawPoints(const QPointF *points, int pointCount)
void drawPoints(const QPolygonF &points)
void drawPoints(const QPoint *points, int pointCount)
void drawPoints(const QPolygon &points)
void drawPolygon(const QPointF *points, int pointCount, Qt::FillRule fillRule = Qt::OddEvenFill)
void drawPolygon(const QPolygonF &points, Qt::FillRule fillRule = Qt::OddEvenFill)
void drawPolygon(const QPoint *points, int pointCount, Qt::FillRule fillRule = Qt::OddEvenFill)
void drawPolygon(const QPolygon &points, Qt::FillRule fillRule = Qt::OddEvenFill)
void drawPolyline(const QPointF *points, int pointCount)
void drawPolyline(const QPolygonF &points)
void drawPolyline(const QPoint *points, int pointCount)
void drawPolyline(const QPolygon &points)
void drawRect(const QRectF &rectangle)
void drawRect(int x, int y, int width, int height)
void drawRect(const QRect &rectangle)
void drawRects(const QRectF *rectangles, int rectCount)
void drawRects(const QVector<QRectF> &rectangles)
void drawRects(const QRect *rectangles, int rectCount)
void drawRects(const QVector<QRect> &rectangles)
void drawRoundedRect(const QRectF &rect, qreal xRadius, qreal yRadius, Qt::SizeMode mode = Qt::AbsoluteSize)
void drawRoundedRect(int x, int y, int w, int h, qreal xRadius, qreal yRadius, Qt::SizeMode mode = ...)
void drawRoundedRect(const QRect &rect, qreal xRadius, qreal yRadius, Qt::SizeMode mode = ...)
void drawStaticText(const QPointF &topLeftPosition, const QStaticText &staticText)
void drawStaticText(const QPoint &topLeftPosition, const QStaticText &staticText)
void drawStaticText(int left, int top, const QStaticText &staticText)
void drawText(const QPointF &position, const QString &text)
void drawText(const QPoint &position, const QString &text)
void drawText(int x, int y, const QString &text)
void drawText(const QRectF &rectangle, int flags, const QString &text, QRectF *boundingRect = nullptr)
void drawText(const QRect &rectangle, int flags, const QString &text, QRect *boundingRect = nullptr)
void drawText(int x, int y, int width, int height, int flags, const QString &text, QRect *boundingRect = nullptr)
void drawText(const QRectF &rectangle, const QString &text, const QTextOption &option = QTextOption())
void drawTiledPixmap(const QRectF &rectangle, const QPixmap &pixmap, const QPointF &position = QPointF())
void drawTiledPixmap(int x, int y, int width, int height, const QPixmap &pixmap, int sx = 0, int sy = 0)
void drawTiledPixmap(const QRect &rectangle, const QPixmap &pixmap, const QPoint &position = ...)
画笔类,可以设置画笔颜色、画笔宽度等等
//1、可以设置画笔的颜色,线宽等信息
QPen pen;
pen.setColor(QColor(255,0,0));//设置画笔的颜色
//设置线宽
pen.setWidth(6);
//设置画笔的样式
pen.setStyle(Qt::DotLine);
p.setPen(pen);
//2、可以填充颜色等信息
QBrush brush;//填充类
//设置填充的颜色
brush.setColor(QColor(255,0,0));
//设置填充样式
brush.setStyle(Qt::SolidPattern);
p.setBrush(brush);
p.drawRect(50,50,50,50);
p.drawLine(50,50,80,80);
三、多线程
为什么QT引入多线程概念?QT搭建界面的工具,使用多线程可以解决界面卡顿,响应不及时的情况
QT中新建的界面,实质上是一个主线程
如何实现(搭建)线程: