我正在PyQT5中开发一个应用程序,它的两边有两个dock,中间有一个OCC 3D查看器和一个TextEdit。OCC查看器的.ExportToImage()方法允许对查看器进行截图。但是由于应用程序有一个响应式的设计,查看器的大小被调整为薄型(在某些显示分辨率上),因此截图也是薄型的。
我试着将窗口调整到一个特定的大小,然后隐藏除了3D浏览器以外的所有内容。这样就可以放大查看器,从而节省了被裁剪的截图。但是当我隐藏和调整大小,然后再进行截图时,截图还是很薄。这是代码。
def take_screenshot(self):
Ww=self.frameGeometry().width()
Wh=self.frameGeometry().height()
self.resize(700,500)
self.outputDock.hide() #Dock on the right
self.inputDock.hide() #Dock on the left
self.textEdit.hide() #TextEdit on the Bottom-Middle
self.display.ExportToImage(fName) #Display is the 3d Viewer's display on the Top-Middle
self.resize(Ww,Wh)
self.outputDock.show()
self.inputDock.show()
self.textEdit.show()
我想这是因为PyQt5的.show(), .hide(), .resize()方法是多线程的,我一运行它们,它们就不是连续运行,而是并行运行。因此,在其他进程完成之前,屏幕截图就已经被截取了。
有什么办法可以解决这个问题吗?或者有更好的方法吗?
没有多线程.事件是在循环中处理的,所以叫事件循环。
试试这个。
def take_screenshot(self):
Ww=self.frameGeometry().width()
Wh=self.frameGeometry().height()
self.resize(700,500)
self.outputDock.hide() #Dock on the right
self.inputDock.hide() #Dock on the left
self.textEdit.hide() #TextEdit on the Bottom-Middle
QTimer.singleShot(0, self._capture)
def _capture(self):
# app.processEvents() # open this line if it still doesn't work, I don't know why.
self.display.ExportToImage(fName) #Display is the 3d Viewer's display on the Top-Middle
self.resize(Ww,Wh)
self.outputDock.show()
self.inputDock.show()
self.textEdit.show()