qscrollarea图片立即消失

Qscrollarea images disappeard immidiately

我写了下面的代码,以便在滚动区域显示存在于特殊目录中的图像。但是当我触发 actionOpen 时,图像会立即消失。如何使图像稳定?

mainwindow.cpp

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

layout = ui->verticalLayout;
}

MainWindow::~MainWindow()
{
delete ui;
}

void MainWindow::on_actionOpen_triggered()
{


QString directory = QFileDialog::getExistingDirectory(this,
                                                      tr("OpenDirectory"),
                                                      QDir::homePath(),
                                                      QFileDialog::ShowDirsOnly| QFileDialog::DontResolveSymlinks);

int nrows = 1;
int ncols = 1;
HorizontalScrollArea scroll(nrows, ncols);
layout->addWidget(&scroll);

    QDir dir(directory);
    dir.setNameFilters({"*.png", "*.jpg"});
    for(const QFileInfo & finfo: dir.entryInfoList()){

        QMessageBox::information(this,"log",finfo.absolutePath());
        int column = scroll.columnCount();
        for(int row=0; row < nrows; row++){
            QPixmap pic(finfo.absoluteFilePath());
            QLabel *label = new QLabel(QString("label: %1 %2")
                                       .arg(row)
                                       .arg(column));
            label->setFrameShape(QFrame::Box);
            label->setAlignment(Qt::AlignCenter);
            label->setPixmap(pic.scaled(500,600,Qt::KeepAspectRatio));
            scroll.addWidget(label, row, column);
        }
    }
  }

horizontasScrollarea.h

class HorizontalScrollArea : public QScrollArea
{
QWidget *contentWidget;
QGridLayout *grid;
int nRows;
int nColumns;
public:
HorizontalScrollArea(int rows, int cols, QWidget *parent = Q_NULLPTR)
    :QScrollArea(parent), nRows(rows), nColumns(cols)
{
    setWidgetResizable(true);
    contentWidget = new QWidget(this);
    setWidget(contentWidget);
    grid = new QGridLayout(contentWidget);
    setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
}

void addWidget(QWidget *w, int row, int col){
    grid->addWidget(w, row, col);
    adaptSize();
}

int columnCount() const{
    if(grid->count() == 0){
        return 0;
    }
    return grid->columnCount();
}

private:
void adaptSize(){
    if(columnCount() >= nColumns ){
        int w = 1.0*(width() - grid->horizontalSpacing()*(nColumns+1.6))/nColumns;
        int wCorrected = w*columnCount() + grid->horizontalSpacing()*(columnCount()+2);
        contentWidget->setFixedWidth(wCorrected);
    }
    contentWidget->setFixedHeight(viewport()->height());
}
protected:
void resizeEvent(QResizeEvent *event){
    QScrollArea::resizeEvent(event);
    adaptSize();
}
};

为了查看图片,我放置了一个信息按钮,只要出现图片就会执行该按钮。

在方法内创建的变量在方法执行完成时被消除,你的情况是滚动,解决方案是使用指针。

QString directory = QFileDialog::getExistingDirectory(this,
                                                      tr("OpenDirectory"),
                                                      QDir::homePath(),
                                                      QFileDialog::ShowDirsOnly| QFileDialog::DontResolveSymlinks);

int nrows = 1;
int ncols = 1;
HorizontalScrollArea *scroll = new HorizontalScrollArea(nrows, ncols);
layout->addWidget(scroll);

QDir dir(directory);
dir.setNameFilters({"*.png", "*.jpg"});
for(const QFileInfo & finfo: dir.entryInfoList()){

    QMessageBox::information(this,"log",finfo.absolutePath());
    int column = scroll->columnCount();
    for(int row=0; row < nrows; row++){
        QPixmap pic(finfo.absoluteFilePath());
        QLabel *label = new QLabel(QString("label: %1 %2")
                                   .arg(row)
                                   .arg(column));
        label->setFrameShape(QFrame::Box);
        label->setAlignment(Qt::AlignCenter);
        label->setPixmap(pic.scaled(500,600,Qt::KeepAspectRatio));
        scroll->addWidget(label, row, column);
    }
}