QT4 QgraphicsView 不在“for 循环”中重新绘制显示许多页面
我写了一个程序来试验 poppler pdf 库。 我可以使用以下方法将 pdf 页面显示到graphicsView中:
void MainWindow::setPage(int newpage)
{
pdfPage = document->page(newpage);
if (pdfPage == 0) {
// ... error message ...
return;
}
// Generate a QImage of the rendered page
image = pdfPage->renderToImage(100.0,100.0,0,0,pdfPage->pageSize().width(),pdfPage->pageSize().height());
if (image.isNull()) {
// ... error message ...
return;
}
pixmap=QPixmap::fromImage(image);
scene->clear();
scene->addPixmap(pixmap);
this->ui->graphicsView->setScene(scene);
this->ui->graphicsView->repaint(); //the same with show(), invalidate scene()
// after the usage, the page must be deleted
delete pdfPage;
}
通过单个调用或在控件的插槽中 但如果我写一个像这样的循环 for (i=0;i<200;i++) { setPage(i);} 在循环结束之前不会显示任何内容,然后只显示最后一页。 怎么了? 我尝试在自定义线程类中使用 msleep(500),并尝试为graphicsView 调用paintEvent。 可以帮忙吗?
I wrote a program to experiment with poppler pdf library.
I am able to display pdf pages into a graphicsView with a method:
void MainWindow::setPage(int newpage)
{
pdfPage = document->page(newpage);
if (pdfPage == 0) {
// ... error message ...
return;
}
// Generate a QImage of the rendered page
image = pdfPage->renderToImage(100.0,100.0,0,0,pdfPage->pageSize().width(),pdfPage->pageSize().height());
if (image.isNull()) {
// ... error message ...
return;
}
pixmap=QPixmap::fromImage(image);
scene->clear();
scene->addPixmap(pixmap);
this->ui->graphicsView->setScene(scene);
this->ui->graphicsView->repaint(); //the same with show(), invalidate scene()
// after the usage, the page must be deleted
delete pdfPage;
}
with single call or in a slot for a control
but if I write a cycle like
for (i=0;i<200;i++) { setPage(i);}
nothing is displayed until the cycle gets to its end, and then just the last page is visible.
What's wrong?
I tried with msleep(500) in a custom thread class, and tried also calling paintEvent for graphicsView.
Can help?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您不能使小部件在循环中重绘自身。为了让小部件(或图形项)能够自行绘制,每次小部件的外观发生变化时,控制都必须返回到事件循环。请参阅此问题及其答案更多信息。
You cannot cause a widget to redraw itself in a loop. In order for a widget (or graphics item) to draw itself, control must return to the event loop every time the appearance of the widget changes. Please see this question and its answers for more information.