- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我想在我的窗口上画一条线,但我无法真正画出任何东西。
我的.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/
我在使用 QPainter 绘制不同颜色的线条和文本时遇到问题。我正在使用以下代码来实现此目的,但它不起作用。线条和文本都是使用绘制文本的颜色集绘制的。 void MyWidget::drawHorL
我正在尝试使用 QPainter(QT5.8,win64)绘制一个带有多个孔的多边形。我正在使用以下代码: QPainter pm(&image); QPen p(Qt::gray, 2); p.se
我要做什么 –对Qt编写的共享库进行JNI调用。在C++方面,我正在使用QPainter和QSvgRender来处理svg图像并返回一个简单的QImage。 QImage im; QPainter p
我有一些需要大量绘画的饼形小部件。所以我想在 QPainter 在 PaintEvent 中绘制后对其进行缓存。并稍后重用它。有没有直接或黑客的方法来做到这一点? 最佳答案 使用QPixmap作为缓存
是否有机会找出QPainter的大小?我正在使用 QPainter 来绘制移动应用程序的整个图形界面。问题是某些应用程序在窗口上覆盖了每个设备大小不同的菜单,从而使事情几乎不可能!是否有可能在不知道
上下文 我有一个自定义小部件,它应该制作点移动的动画,以便制作一种加载小部件。为了实现这个目标,我开始使用 QPainter 和 QVariantAnimation 对象,这似乎是完成这项工作的不错的
我有一个自定义的 QQuickPaintedItem,它可以绘制用户用鼠标在其上绘制的任何内容。到目前为止,实现非常简单,只需绘制整个图像,即使在放大时也是如此。我注意到放大和平移图像时 FPS 真的
我正在尝试在具有白色背景的 QFrame 上绘制两个具有相同颜色和透明度的矩形。这些矩形应该重叠并且它们的透明度不应该改变(也在重叠区域)。所以像这样: 这是我目前的代码: class Canvas
我有下面的代码通过QPainter显示一些文本 QPainter painter(this); painter.setRenderHint(QPainter::Antialiasing); paint
我使用下面的代码在qt中绘制一个图的轴: ... static const std::array horizontalAxis = { QPointF(0.f,0.f), QPointF(1.
我想在整个圆圈周围绘制那些黑点,彼此之间有规则的间距(下图的粗略图)。那些黑点应该直接坐在圆圈上。有没有一种简单的方法可以用 painter.drawArc() 函数或类似的东西来做这件事? 最佳答案
我正在学习 Qt。我没有意识到 the exercise of chapter 11 of Qt tutorial ,上面写着“当炮弹在空中时改变大炮的颜色”。我选择在 paintCannon 函数中
我创建了一个名为 painter 的类,并在将 QMainWindow 作为参数传递的构造函数中创建了 QPainter p。 所以在 mainwindow.h 中我添加了: protected:
我是Qt初学者,想用QPainter。 我的流程是这样的:我从串口接收数据坐标(x,y),比如(1,1), (2,3),等。我想在每次收到数据时在窗口中绘制这些点。 我看到 QPainter 在事件中
我有一个 QWidget,我在其中使用 QPainter 对象绘制一些点,但是当调用 update() 方法时,QWidget 的绘制被完全清除。有什么方法可以保存实际状态并只添加点,或者我必须保存每
这是我第一次使用 Qt,我必须制作一个与 Qt 等效的 MSPaint。然而,我在画线时遇到了麻烦。我目前可以通过单击屏幕上的某处并在其他地方释放来绘制一条线,但是当我绘制第二条线时,前一条线将被删除
以下代码会导致一堆错误: void MainWindow::displayBoard() { QPixmap pix(0,0); pix.fill(Qt::white); QP
我对书中示例和我的版本之间的差异感到困惑。Qt 版本 5.12.0 如示例所示: 从我的输出中可以看出: 首先,目标和源 In/Atop 模式的图片不同。而且,另一件值得注意的事情是,我们可以将矩形视
问题很简单!我想要这样的东西。要么使用 QPainter类或使用 Qt 图形框架 : 最佳答案 有几种方法可以使用 QPainterPath 来做到这一点。指定 here . 这是该页面的第二个示例:
默认情况下,QPainter 的原点位于左上角。我想绘制形状,但我拥有的所有坐标都在笛卡尔系统中。所以我的问题 - 如何将原点更改为左下角?当然,我可以 scale(1,-1) 但 drawText(
我是一名优秀的程序员,十分优秀!