私の環境は以下です。
- Qt 5.4、ソースからビルド
-platform win32-msvc2013 -opengl desktop -no-icu -skip webkit
- ビジュアル スタジオ 2013
- ウィンドウズ 7 x64
次のセットアップを検討してください (固定サイズの中央に配置されたウィジェットを含む QScrollArea):
QScrollArea scrollArea;
scrollArea.setAlignment(Qt::AlignCenter);
scrollArea.setWidgetResizable(false);
QWidget scrollAreaWidgetContents;
scrollArea.setWidget(scrollAreaWidgetContents);
ここで、次のようにプログラム内でそのウィジェットのサイズを変更したいと思います。
int w = 200, h = 200;
scrollAreaWidgetContents.setGeometry(0, 0, w, h);
ここで奇妙な動作が発生します。とが現在の幅と高さと等しい (つまり、何も変わらない)
場合、ウィジェットは左上隅にジャンプします。とが現在の幅と高さと異なる
場合、ウィジェットは中央の位置にとどまり、サイズが正しく変更されます。w
h
w
h
この問題を示す小さなデモ アプリケーションを作成しました。ボタンをクリックするpushButton_Stay
と、ウィジェットが左上隅にジャンプします。
#include <QtWidgets/QApplication>
#include <QPushButton>
#include <QVBoxLayout>
#include <QScrollArea>
QScrollArea *scrollArea;
QWidget *scrollAreaWidgetContents;
QPushButton *pushButton_Up;
QPushButton *pushButton_Down;
QPushButton *pushButton_Stay;
QVBoxLayout *layout;
int w = 200, h = 200;
void sizeUp() {
w += 10;
h += 10;
scrollAreaWidgetContents->setGeometry(0, 0, w, h);
}
void sizeDown() {
w -= 10;
h -= 10;
scrollAreaWidgetContents->setGeometry(0, 0, w, h);
}
void sizeStay() {
scrollAreaWidgetContents->setGeometry(0, 0, w, h);
}
int main(int argv, char **args)
{
QApplication app(argv, args);
// Scroll area
scrollArea = new QScrollArea;
scrollArea->setWidgetResizable(false);
scrollArea->setAlignment(Qt::AlignCenter);
scrollAreaWidgetContents = new QWidget();
scrollAreaWidgetContents->setGeometry(QRect(0, 0, w, h));
scrollAreaWidgetContents->setAutoFillBackground(true);
QPalette Pal(scrollAreaWidgetContents->palette());
Pal.setColor(QPalette::Background, Qt::black);
scrollAreaWidgetContents->setPalette(Pal);
scrollArea->setWidget(scrollAreaWidgetContents);
// Buttons
pushButton_Up = new QPushButton(scrollAreaWidgetContents);
pushButton_Up->setGeometry(QRect(85, 50, 31, 23));
pushButton_Up->setText("+");
pushButton_Down = new QPushButton(scrollAreaWidgetContents);
pushButton_Down->setGeometry(QRect(85, 110, 31, 23));
pushButton_Down->setText("-");
pushButton_Stay = new QPushButton(scrollAreaWidgetContents);
pushButton_Stay->setGeometry(QRect(85, 80, 31, 23));
pushButton_Stay->setText("0");
QObject::connect(pushButton_Up, &QPushButton::clicked, sizeUp);
QObject::connect(pushButton_Stay, &QPushButton::clicked, sizeStay);
QObject::connect(pushButton_Down, &QPushButton::clicked, sizeDown);
// Central layout
layout = new QVBoxLayout;
layout->addWidget(scrollArea);
// Main window
QWidget window;
window.setGeometry(200, 200, 400, 400);
window.setLayout(layout);
window.show();
return app.exec();
}
ボタンをクリックするpushButton_Up
:
ボタンをクリックするpushButton_Stay
:
質問:
ここで何がうまくいかないのですか?
バグですか?もしそうなら、誰でもそれを確認できますか?
バグでない場合、ウィジェットが常に中央に表示されるようにするにはどうすればよいですか?