SuperCollider GUI绘图报错:QPen使用受限错误含义及问题排查
解决SuperCollider UserView绘制图像时的Qt错误
错误原因
你遇到的Qt: Usage of QPen is not allowed at this point!错误,根源在于多余的Pen.draw调用:
Image.drawInRect已经直接完成了图像绘制操作,它不需要依赖Pen类的状态。Pen.draw是用于矢量图形(线条、形状)的绘制方法,在图像绘制的上下文里调用它会导致Qt的绘图状态冲突,触发原语调用失败。
修正方案
1. 移除错误的Pen.draw语句
修改初始化代码中的drawFunc,删掉Pen.draw;这一行:
( var path = CleffView.path; // 替换为你的本地图片路径 b = Image.new(path +/+ "B.png"); a = [b]; u = UserView().front; // 加上.front让视图显示出来 u.drawFunc_({ var width, height, left=0, top=0; a.do({|img| img.notNil.if({ width = img.width; height = img.height; img.drawInRect(Rect(left, top, width, height), nil, 'sourceOver', 1); // 移除这行错误代码:Pen.draw; left = left + width; }); }) }) )
2. 用u.refresh触发重绘
更新图像数组后,不要直接调用u.draw,而是用u.refresh来触发视图的正常重绘流程:
a = a ++ b; u.refresh; // 正确触发drawFunc执行,避免Qt状态错误
补充说明
UserView的drawFunc会在视图初始化、窗口大小变化或调用refresh时自动执行,手动调用u.draw会绕过Qt的绘图状态管理,容易引发类似错误。Image.drawInRect是直接操作Qt绘图设备的方法,不需要和Pen类的绘制流程混用。
内容的提问来源于stack exchange,提问作者les_h
相关产品推荐
相关产品推荐

