- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
问题:我无法调用 GraphicsView 的 paintEvent,或者换句话说,我不知道如何让 QPainter 一起工作。
class Draw
)进行了子类化,覆盖了 paintEvent
并在主类中保存了这个子类(Draw draw
)的一个实例窗口(我想把复杂的图移到别处)新的 QGraphicsScene
并将其分配给 QGraphicsView 的
(ui->graphicsView
和 draw
)所以当我在 Draw
中绘制时,它也会在 ui->graphicsview
中明显生效(我希望如此)。scene
对象进行绘制时,我会得到一个可见的结果(但是,我不想使用 scene
对象进行绘制但是 QPainter
。原因是为了另一个问题,所以我希望它不是必需的)因此我试图子类化 QGraphicsView
并覆盖 paintEvent 所以我得到 QPainter p(this)
很容易。Window.cpp
中调用 MyRepaint()
,我试图在我的 上调用 paintEvent()
code>Draw
对象 - 但它不工作。主窗口“Window.cpp”:
Window::Window(QWidget* parent) :
QMainWindow(parent),
ui(new Ui::Window),
draw(*this) //my own QGraphicsView instance (see class below)
{
ui->setupUi(this);
//assigning a new scene to draw and to window's graphicsView
this->scene = new QGraphicsScene(this);
this->draw.setScene(scene);
this->ui->graphicsView->setScene(scene);
}
void Window::MyRepaint()
{
qInfo() << "Repaint - start" << this->draw.scene();
this->draw.scene()->update(this->draw.sceneRect());
this->draw.repaint();
this->draw.viewport()->update();
/*only the following line made the paintEvent executed eventually but without a visible result and with an error in the output*/
this->draw.paintEvent(NULL);
qInfo() << "Repaint - end";
}
子类化 QGraphicsView,文件:Draw.h:
class Window;
class Draw : public QGraphicsView{
private:
Window& parent;
public:
Draw(Window &parent);
void paintEvent(QPaintEvent*e) override;
};
绘图.cpp
void Draw::paintEvent(QPaintEvent *e)
{
qInfo() << "trying to draw";
QPainter p(this);
p.setPen(QPen(Qt::black, 12, Qt::DashDotLine, Qt::RoundCap));
p.drawLine(0, 0, 200, 200);
}
输出:
Repaint - start QGraphicsScene(0x15c7eca8)
trying to draw
QWidget::paintEngine: Should no longer be called
QPainter::begin: Paint device returned engine == 0, type: 1
QPainter::setPen: Painter not active
QPainter::viewport: Painter not active
QPainter::end: Painter not active, aborted
Repaint - end
也许我选择了一个完全错误的方法。
最佳答案
QGraphicsView 旨在与 QGraphicsScene 一起使用。如果你只想画线,那么从 QWidget 派生并覆盖它的 paintEvent。在设计器中将 QWidget 提升到您的派生类。
另外,Qt 有很好的文档。我建议你访问this页面。
关于c++ - 使用 QGraphicsView 子类用 QPainter 画一条线,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37981512/
我想以 headless 模式(屏幕上根本没有 GUI)将 JPanel 绘制到 BufferedImage 中。 final JPanel panel = createPanel(); panel.
我是 Canvas 的新手,正在尝试创建看起来逼真的 float 粒子动画。 目前,我正在创建 400 个随机散布在 400x400 Canvas 上的粒子。 然后,在每个 requestAnimat
有没有办法在悬停时停止悬 float 画? :hover 这是一个显示动画的链接: https://codepen.io/youbiteme/pen/RprPrN 最佳答案 只需为您的 svg 悬停添
我想在谷歌地图上绘制覆盖图,其中除了特定点周围 1.5 公里半径以外的所有内容都被遮蔽了。为此,我尝试使用带有大量边框的圆圈,所以我会在边框中放置透明中心和覆盖颜色来实现这一点,但它无法渲染。
我正在尝试通过扩展类 UIView 来创建自定义 View ,该类可以在自定义 View 的中心显示一个圆圈。为了添加自定义绘图,我重写了 draw(_ rect: CGRect) 方法,如下所示。
我是一名优秀的程序员,十分优秀!