- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我想实现一个自定义小部件。通过双击将 Node
对象添加到拥有的 Graph
小部件时,Node::paintEvent
正确发生,但是 的区域code>QPaintEvent
是常量并且太小,无论我在哪里添加它。指示的边界重绘框始终位于 (0,0),宽度/高度为 (100,30)。
知道为什么会这样吗?
代码
#include <QApplication>
#include <QMainWindow>
#include <QPainter>
#include <QMouseEvent>
#include <iostream>
#include <vector>
#define DEBUG(lvl, x) \
std::clog << "L" << __LINE__ << ": " << x << "\n";
class Node final : public QWidget
{
protected:
void paintEvent (QPaintEvent * event) override {
DEBUG(0, "Node::paintEvent");
QPainter painter(this);
painter.setBrush(QColor(127,127,127));
painter.drawRect(posX, posY, width, height);
auto x = event->rect();
DEBUG(0, "PaintBox:" << x.x() << "::" << x.y() << "::" << x.width() << "::" << x.height());
}
public:
explicit Node (QWidget * parent = 0): QWidget(parent) {}
int posX{0}, posY{0}, width{60}, height{60};
};
class GraphView final : public QWidget
{
public:
explicit GraphView (QWidget * parent = 0): QWidget(parent) {}
protected:
void paintEvent (QPaintEvent *) override {
DEBUG(0, "GraphView::paintEvent");
}
void mouseDoubleClickEvent (QMouseEvent * event) override {
DEBUG(0, "mouseDoubleClickEvent");
auto ptr = new Node(this);
ptr->posX = event->x();
ptr->posY = event->y();
nodes.push_back(ptr);
ptr->show();
}
void mousePressEvent (QMouseEvent * event) override {
DEBUG(0, "mousePressEvent");
auto ptr = static_cast<Node*>(childAt(event->pos()));
if (ptr) {
DEBUG(0, "FOUND X");
}
}
std::vector<Node*> nodes;
};
int main (int argc, char *argv[])
{
QApplication a(argc, argv);
auto* gv = new GraphView{};
QMainWindow w;
w.setCentralWidget(gv);
w.resize(640, 480);
w.show();
return a.exec();
}
双击窗口区域的任何地方返回:
L34: GraphView::paintEvent
L48: mousePressEvent
L38: mouseDoubleClickEvent
L34: GraphView::paintEvent
L16: Node::paintEvent
L21: PaintBox:0::0::100::30
如果双击 0,0 和 100,30 之间的区域,节点将按应有的方式显示。
最佳答案
请注意,Qt 已经内置了一个非常好的图形场景小部件。看看 Graphics View Framework .它针对数千个项目进行了优化,支持单个场景的多个 View 、缩放、剪切、旋转等。
但是如果你想自己处理:
绘画事件中的坐标总是相对于小部件的根。所以 (0,0)
是小部件的左上角,不管它放在哪里(参见 coordinate systems )。
当您将子小部件(作为您的节点)直接添加到小部件(而不是使用布局)时,它被放置在左上角。它的大小由 sizeHint
决定。
所以现在,当您点击例如在 (200,200)
您将添加一个新的小部件,并相应地设置其位置成员。这导致在 (0,0)
的 GraphView
小部件中有一个 Node
小部件,大小为 (100,30)
。然后在绘制事件中,您在 (200,200)
处绘制了一个矩形,它在小部件边界之外!
您应该设置几何图形,以便将子部件放置在 Qt 的坐标系中:
void GraphView::mouseDoubleClickEvent (QMouseEvent * event) {
auto ptr = new Node(this);
ptr->setGeometry(event->x(), event->y(), ptr->width, ptr->height);
nodes.push_back(ptr);
ptr->show();
}
然后根据 (0,0)
绘制:
void Node::paintEvent (QPaintEvent * event) {
QPainter painter(this);
painter.setBrush(QColor(127,127,127));
painter.drawRect(0, 0, width, height);
}
关于c++ - Qt5 paintEvent() 区域太小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54752184/
我有一个用 C# 编写的 Windows 窗体应用程序,带有绘图面板和一个按钮 - 用于绘制一条线。 当您点击按钮时,您可以为 2 个随机点画一条线。 Pen p = new Pen(Color.Bl
我有一个用 C# 编写的 Windows 窗体应用程序,带有绘图面板和一个按钮 - 用于绘制一条线。 当您点击按钮时,您可以为 2 个随机点画一条线。 Pen p = new Pen(Color.Bl
我有一个 QMainWindow 的 widget 类子类,它有一个中央 widget(QWidget),在覆盖的 paintEvent 函数中,我可以在这个中央 widget 上创建一个 QPain
我想在Qwidget 中用随机颜色绘制一些菱形。只有在调整窗口大小时才应重新绘制小部件。问题是当小部件被遮盖并且现在已被揭开时,它会被重新绘制。在这种情况下如何避免调用 paintEvent()?提前
我正在尝试向QDialog添加圆角。我正在定义自己的 paintEvent 方法来创建圆角。它正在工作,但它为所有内容添加了圆形边框。甚至光标也有了边框。有什么办法可以禁用这种行为吗? 示例代码: f
我的应用程序使用两个类,因此有两个类"file",第一类显示按钮等。第二类绘制矩形,并且在按键时应该改变某些矩形的颜色(通过重绘它们)。第二类包含 paintEvent 方法。该程序编译正常,但矩形不
我想实现一个自定义小部件。通过双击将 Node 对象添加到拥有的 Graph 小部件时,Node::paintEvent 正确发生,但是 的区域code>QPaintEvent 是常量并且太小,无论我
我在自定义小部件中子类化了 QProgressBar,并用以下代码覆盖了 paintEvent 方法: void myProg::paintEvent(QPaintEvent *pe) { Q
我在 Qt 中遇到裁剪问题。我有一个小部件,我在其中绘制了一大堆矩形。由于我只是不时更改几个矩形,所以我只想绘制小部件的一小部分(仅这些矩形)并将绘制区域裁剪到这些部分。 如果矩形接收到自上次绘制以来
我是 Qt 初学者。 所以我的 MainWindow 有一个 QSlider 和一个 QPixmap。我重新定义了 paintEvent( QPaintEvent* event ) 和 connect
我正在为应用程序创建自定义消息框。我的对象派生自 QMessageBox,但我重写了 paintEvent() 方法以更改其外观。奇怪的是,虽然我没有在派生方法中调用基本的 paintEvent 方法
每当调用 repaint() 时,我都试图重绘一个小部件,但由于某种原因,它没有触发 paintevent()。例如,当我做这样的事情时: mLCD = new LCDui(); mLCD->draw
我的表单中只有一个图片框,我想在这个图片框上使用一种方法绘制圆圈,但我做不到而且不起作用。方法是: private Bitmap Circle() { Bitmap bmp;
在我的一个 Qt 应用程序中更新 QWidget 时,我发现了一个非常奇怪的情况。当 widget->update() 方法在没有任何警告或命令的情况下被调用时,widget 的 paintEvent
正如标题所说,我想将paintEvent连接到一个插槽,以便它可以被定时器触发,我该怎么做?我可以提供你们需要的任何更多信息,不过我对 QT 还很陌生,所以请记住这一点。 编辑:所以我在一个小测试程序
我正在尝试在我的小部件上绘制简单的板。 当我尝试将其自动化时,我的 paintEvent 崩溃了。我认为这是由内部的for循环引起的,对吗?怎么用其他方式画呢? void Widget::paintE
我正在使用 PyQt4,我想根据用户对显示为图像标签的现有图像的点击画一条线。图像正确显示,单击工具栏中的图标后,用户将在图像上画一条线。 我已经覆盖了 mousePressEvent() 和 mou
过去几天我一直在为这个问题苦苦挣扎。当用户调整窗口大小时,我希望能够扩大和缩小 QLabel 中分配的任何 Pixmap。问题是保持纵横比和图像质量。这里的另一位用户建议我重新实现标签的绘画事件 -
QGLWidget 的paintEvent 方法似乎是在initializeGL 之前调用的,那么我应该把我的openGL 初始化代码放在哪里呢? 我像这样将它放入 paintEvent 方法中: v
据我所知,paintEvent() 是在 QApplication 对象的“主循环”中执行的,并且可以为其内部系统任务花费时间,从而延迟执行排队槽或其他事件。 但是,如果我需要播放非常流畅的动画并且我
我是一名优秀的程序员,十分优秀!