为什么 QPainter 不绘制到我的 QWidget?
Why is QPainter not drawing to my QWidget?
我想在我的 window 上画一条线,但我无法真正画出任何东西。
我的.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
可能不支持从主线程以外的线程绘制。
所以之前 my research 教我如果我实现 setAttribute 函数:
QWidget::setAttribute(Qt::WA_PaintOutsidePaintEvent, true);
我的 QPainter 可以在 QWidget::paintEvent(QPaintEvent*)
函数调用之外进行打印。
Dtech 指出它是遗留功能,Qt 5 中不再存在。当我在 Ubuntu 上用 Qt 4.8.7 进行编译时,我认为该功能仍然存在并设法找到 documentation (ctrl+f PaintOutsidePaintEvent).
WA_PaintOutsidePaintEvent:
Makes it possible to use QPainter to paint on the widget outside paintEvent(). This flag is not supported on Windows, Mac OS X or Embedded Linux. We recommend that you use it only when porting Qt 3 code to Qt 4.
因此必须假定此属性设置仅在将 Qt 3 代码移植到 Qt 4 时才能正常运行。
因此,错误是在尝试实现我的 QWidget 的 overriding the paintEvent 函数的 QWidget::setAttribute 函数时出错。
我想在我的 window 上画一条线,但我无法真正画出任何东西。
我的.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
可能不支持从主线程以外的线程绘制。
所以之前 my research 教我如果我实现 setAttribute 函数:
QWidget::setAttribute(Qt::WA_PaintOutsidePaintEvent, true);
我的 QPainter 可以在 QWidget::paintEvent(QPaintEvent*)
函数调用之外进行打印。
Dtech 指出它是遗留功能,Qt 5 中不再存在。当我在 Ubuntu 上用 Qt 4.8.7 进行编译时,我认为该功能仍然存在并设法找到 documentation (ctrl+f PaintOutsidePaintEvent).
WA_PaintOutsidePaintEvent:
Makes it possible to use QPainter to paint on the widget outside paintEvent(). This flag is not supported on Windows, Mac OS X or Embedded Linux. We recommend that you use it only when porting Qt 3 code to Qt 4.
因此必须假定此属性设置仅在将 Qt 3 代码移植到 Qt 4 时才能正常运行。
因此,错误是在尝试实现我的 QWidget 的 overriding the paintEvent 函数的 QWidget::setAttribute 函数时出错。