gpt4 book ai didi

c++ - 为什么 QPainter 不绘制到我的 QWidget?

转载 作者:太空宇宙 更新时间:2023-11-04 12:32:09 26 4
gpt4 key购买 nike

我想在我的窗口上画一条线,但我无法真正画出任何东西。

我的.h

#ifndef BOARD_H
#define BOARD_H

#include <Disc.h>
#include <iostream>
#include <QApplication>
#include <QPaintEvent>

class Board
{
private:
int currentDiscs;
Disc* discArray[64];
char* first;
QWidget* window;
public:
Board(int*);
int StartDrawing(QApplication*);

};

#endif

}

我的.cpp

int Board::StartDrawing(QApplication* app) 
{
int WIDTH = 640;
int HEIGHT = 800;

QWidget* window = new QWidget();

window->setAttribute(Qt::WA_PaintOutsidePaintEvent, true);
window->setFixedSize(WIDTH, HEIGHT);
window->setWindowTitle("Title!");

this->window = window;
window->show();

QPainter* painter = new QPainter(window);
//QPixmap pixmap1("\\images\\tile_1.png");

//painter->drawPixmap(QPoint(0,0), pixmap1); // this works
painter->setPen(QPen(Qt::black, 12, Qt::DashDotLine, Qt::RoundCap));
painter->drawLine(10,10,100,500);
delete painter;

return app->exec();
}

我的猜测是我的 QPainter 遗漏了一些东西,但我没有在网上看到任何建议我需要用它调用任何其他函数的信息。

我错过了什么?

感谢您的宝贵时间。

最佳答案

read the documentation总是有用的,你的是最常犯的错误:

Each widget performs all painting operations from within its paintEvent() function. This is called whenever the widget needs to be redrawn, either as a result of some external change or when requested by the application.

您只能通过该小部件的绘制事件在该小部件上进行绘制。其他绘画设备的限制较少,您可以从任何地方绘画到另一个绘画设备,如果需要,在其绘画事件中将该缓存绘画到一个小部件。请注意,在某些平台上,QPixmap 可能不支持从主线程以外的线程绘制。

关于c++ - 为什么 QPainter 不绘制到我的 QWidget?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43025474/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com