单击按钮时在 QFrame 中绘制。
假设有一个名为“Draw”的 QPushButton、一个 QLineEdit 和一个 QFrame。单击按钮时,我想从 QLineEdit 中获取一个数字并在 QFrame 中绘制一个圆圈。我该怎么做?请向我提供代码。
PS问题是QPainter的draw方法应该在drawEvent方法中调用。
Say there is a QPushButton named "Draw", a QLineEdit and a QFrame. On clicking the button I want to take a number from QLineEdit and draw a circle in a QFrame. How can I do this? Please provide me with the code.
P.S. The problem is that draw methods of the QPainter should be called in drawEvent method.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果@Kaleb Pederson的答案是对您来说还不够,那么这里有一个完整的解决方案,用于与您所描述的内容相匹配的简单设置。在 Linux 上使用 Qt 4.5.2 进行测试。我有一些空闲时间...;)
main.cpp:
window.h
window.cpp:
frame.h:
frame.cpp:
If @Kaleb Pederson's answer is not enough for you then here's a complete solution for a simple set-up matching what you describe. Tested with Qt 4.5.2 on Linux. I had some spare time... ;)
main.cpp:
window.h
window.cpp:
frame.h:
frame.cpp:
如果您希望框架进行绘图,那么它需要一种方法来知道它应该绘制某些内容,因此创建一个将接收通知的插槽:
然后,您的框架子类需要重写
paintEvent()
绘制圆圈:只要响应按钮的
clicked()
信号的插槽发出一个信号,使用正确的参数调用drawCircle
插槽,一切都应该正常工作。If you want your frame to do the drawing, then it needs a way to know that it should draw something, so create a slot that will receive notification:
Then, your frame subclass you need to override
paintEvent()
to draw the circle:As long as the slot responding to the button's
clicked()
signal emits a signal that calls thedrawCircle
slot with the correct arguments everything should work correctly.您不会直接在框架上绘图。
从这里开始 graphicsview,乍一看很复杂 - 但 GUI 程序是一个很大的飞跃你第一次遇到它
在大多数 GUI(Qt、OpenGL 等)中,你会建立一个你想要在程序中绘制的元素列表并以某种方式存储它们 - 然后有一个 draw() 函数,当计算机需要绘制你的元素时,它会被调用图片 - 例如,当它被移动或另一个窗口被移动到它前面时。然后调用 OnDraw 或 OnRepaint 等函数,您必须绘制对象列表。
另一种方法是将它们全部绘制到图像(QOimage 或 QPixmap),然后在 OnDraw 或 OnRepaint 中将其复制到屏幕 - 例如,您可以对图形包执行此操作。
You don't draw diectly onto a frame.
Start here graphicsview, it looks complicated at first - but GUI program is a big leap when you first encounter it
In most GUIs (Qt, OpenGL etc) you build up a list of elements you want to draw in your program and store them somehow - then there is a draw() function that gets called when the computer needs to draw your picture - eg when it is moved or another window is moved in front of it. The OnDraw or OnRepaint etc function then gets called and you have to draw the list of objects.
Another way to do this is to draw them all to an image (QOimage or QPixmap) and copy that to the screen in OnDraw or OnRepaint - you might do this for a graphics package for example.