嗯,我没有答案,同样的问题。但是,我可以提供一个小的复制示例:
#include <QtGui>
class TestWidget : public QWidget
{
Q_OBJECT
public:
TestWidget(QWidget * parent = 0);
private slots:
void addButton();
private:
QScrollArea * scrollArea;
QWidget * contents;
QVBoxLayout * contentsLayout;
};
TestWidget::TestWidget(QWidget * parent)
: QWidget(parent)
{
QVBoxLayout * layout = new QVBoxLayout(this);
scrollArea = new QScrollArea(this);
layout->addWidget(scrollArea);
contents = new QWidget(this);
scrollArea->setWidget(contents);
contents->resize(300,400);
scrollArea->setWidgetResizable(true);
contentsLayout = new QVBoxLayout(contents);
for (int i = 0; i < 10; ++i)
{
QPushButton * button = new QPushButton(QString("button %1").arg(i), this);
connect(button, SIGNAL(clicked()), this, SLOT(addButton()));
contentsLayout->addWidget(button);
}
}
void TestWidget::addButton()
{
QPushButton * button = new QPushButton("button", this);
connect(button, SIGNAL(clicked()), this, SLOT(addButton()));
contentsLayout->addWidget(button);
QScrollBar * scrollBar = scrollArea->verticalScrollBar();
scrollBar->setValue(scrollBar->maximum());
}
int main(int argc, char * argv[])
{
QApplication app(argc, argv);
TestWidget widget;
widget.show();
app.setQuitOnLastWindowClosed(true);
return app.exec();
}
#include "main.moc"
Qt 4.8 和 5.2 都会出现此问题(需要对示例进行少量修改)。