开发者

QT4 using QMdiArea and QScrollArea strange usage trouble

开发者 https://www.devze.com 2023-04-11 05:11 出处:网络
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 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(imageLabe开发者_开发问答l);

it works perfectly.

Anyone know why this is happening?


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->.)

0

精彩评论

暂无评论...
验证码 换一张
取 消