QT4 using QMdiArea and QScrollArea strange usage trouble

342 Views Asked by At

Here is what I am doing: mainwindow with MdiArea, and I add a scrollarea widget (which contains a image label) to MdiArea as a subwindow. It doesn't work (the picture doesn't show).

Here is my code:

MainWindow::MainWindow(QWidget *parent) :
  QMainWindow(parent),
  ui(new Ui::MainWindow)
{
  ui->setupUi(this);

  QScrollArea sa;
  QPixmap *image = new QPixmap("2.jpg");
  QLabel* imageLabel = new QLabel();
  imageLabel->setPixmap(*image);
  sa.setWidget(imageLabel);
  sa.show();
  ui->mdiArea->addSubWindow(&sa);
}

But when I use a QLabel as subwindow directly, i.e. replace the last line with:

ui->mdiArea->addSubWindow(imageLabel);

it works perfectly.

Anyone know why this is happening?

1

There are 1 best solutions below

0
On
QScrollArea sa;

This declares a QScrollArea on the stack. It gets destroyed immediately after the constructor finishes. Allocate it with new like you do for the other widgets and it should start working.

QScollArea *sa = new QScrollArea;
...
ui->mdiArea->addSubWindow(sa);

(And change the sa. to sa->.)