如何以编程方式滚动一个QPrintPreviewWidget?

时间:2022-08-24 23:48:46

I've got a QPrintPreviewWidget, I update its content programmatically based on user input. The problem when doing so is that the scrollbar stays where it was, while I need it to go back to the top.

我有一个QPrintPreviewWidget,我根据用户输入以编程方式更新它的内容。这样做的问题是滚动条保持在原来的位置,而我需要它回到顶部。

I tried using the scroll function with a very big number but that didn't do anything. Any suggestion?

我试着用一个很大的数字来使用滚动函数,但它什么也没做。任何建议吗?

Edit:

编辑:

Here is the structure of the dialog in Qt Creator:

以下是Qt创建者对话框的结构:

如何以编程方式滚动一个QPrintPreviewWidget?

Then I add the QPrintPreviewWidget to the previewWidgetContainter layout using this code:

然后,我使用以下代码将QPrintPreviewWidget添加到previewWidgetContainter布局中:

previewWidget_ = new QPrintPreviewWidget(printer_, this);
previewWidget_->fitToWidth();
ui->previewWidgetContainter->addWidget(previewWidget_);

Edit:

编辑:

I followed @tmpearce 's suggestion, but that didn't work. On the showEvent I set the current page but it has no effect whatsoever. My code is something like this:

我遵从了@tmpearce的建议,但那没用。在showEvent上,我设置了当前页面,但它没有任何效果。我的代码是这样的:

qDebug() << "Before" << previewWidget.currentPage();
previewWidget.setCurrentPage(0);
qDebug() << "After" << previewWidget.currentPage();

And the output of this is always something like that:

它的输出总是这样的

Before 15
After 15

I also tried setting the page in various other places but the result is always the same. That looks like a bug in QPrintPreviewWidget so I'm going to give up for now. Thanks anyway for the suggestions.

我还尝试在其他地方设置页面,但结果总是一样的。这看起来像QPrintPreviewWidget中的一个bug,所以我现在要放弃了。谢谢你的建议。

1 个解决方案

#1


0  

Without knowing more about the specifics of your code, my suggestion would be to explicitly call this slot: QPrintPreviewWidget::setCurrentPage(int)

如果不了解更多代码的细节,我的建议将是显式地调用这个slot: QPrintPreviewWidget::setCurrentPage(int)

after changing the contents of the widget.

在更改小部件的内容之后。

#1


0  

Without knowing more about the specifics of your code, my suggestion would be to explicitly call this slot: QPrintPreviewWidget::setCurrentPage(int)

如果不了解更多代码的细节,我的建议将是显式地调用这个slot: QPrintPreviewWidget::setCurrentPage(int)

after changing the contents of the widget.

在更改小部件的内容之后。