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

窗口设置

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)); //设置背景

  • 29
    点赞
  • 28
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
下面是一个简单的示例代码,演示了如何使用线程来优化QT5 QGraphicsView中大量QGraphicsItem的添加操作: ```cpp // MyThread.h #ifndef MYTHREAD_H #define MYTHREAD_H #include <QThread> #include <QGraphicsScene> #include <QGraphicsRectItem> class MyThread : public QThread { Q_OBJECT public: explicit MyThread(QGraphicsScene* scene, QObject *parent = nullptr); void run() override; signals: void itemAdded(QGraphicsItem* item); private: QGraphicsScene* m_scene; }; #endif // MYTHREAD_H // MyThread.cpp #include "MyThread.h" MyThread::MyThread(QGraphicsScene* scene, QObject *parent) : QThread(parent), m_scene(scene) { } void MyThread::run() { for (int i = 0; i < 10000; i++) { QGraphicsRectItem* item = new QGraphicsRectItem(QRectF(0, 0, 100, 100)); item->setPos(i * 100, 0); emit itemAdded(item); } } // MainWindow.h #ifndef MAINWINDOW_H #define MAINWINDOW_H #include <QMainWindow> #include <QGraphicsScene> #include <QGraphicsView> #include "MyThread.h" class MainWindow : public QMainWindow { Q_OBJECT public: MainWindow(QWidget *parent = nullptr); ~MainWindow(); private slots: void onItemAdded(QGraphicsItem* item); private: Ui::MainWindow *ui; QGraphicsScene* m_scene; QGraphicsView* m_view; MyThread* m_thread; }; #endif // MAINWINDOW_H // MainWindow.cpp #include "MainWindow.h" #include "ui_MainWindow.h" MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent) , ui(new Ui::MainWindow) { ui->setupUi(this); m_scene = new QGraphicsScene(this); m_view = new QGraphicsView(m_scene, this); setCentralWidget(m_view); m_thread = new MyThread(m_scene, this); connect(m_thread, &MyThread::itemAdded, this, &MainWindow::onItemAdded); m_thread->start(); } MainWindow::~MainWindow() { delete ui; } void MainWindow::onItemAdded(QGraphicsItem* item) { m_scene->addItem(item); m_view->update(); } ``` 该示例代码中,我们创建了一个MyThread类,用于在子线程中创建QGraphicsItem。MyThread类中定义了一个itemAdded信号,用于在子线程中将创建好的QGraphicsItem添加到主线程中的QGraphicsScene中。 在MainWindow中,我们创建了一个QGraphicsScene和一个QGraphicsView,将QGraphicsScene设置为QGraphicsView的场景。然后,我们创建了一个MyThread对象,并将其和QGraphicsItem的创建和添加操作相关联。 在MyThread的run()函数中,我们创建了10000个QGraphicsRectItem,并通过emit itemAdded(item)信号将其添加到主线程中的QGraphicsScene中。 在MainWindow的onItemAdded()函数中,我们将创建好的QGraphicsItem添加到QGraphicsScene中,并调用m_view->update()函数更新界面。 运行该示例代码,可以看到在添加QGraphicsItem的过程中,界面仍然可以响应其他的操作,不会出现卡顿的情况。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值