我编写了以下代码,以便在两个单独的滚动区域中显示一系列图像并同时移动它们。例如,当我相应地移动第一个滚动区域时,第二个滚动区域必须移动并显示相同的图片。不幸的是,代码无法正常工作。我该如何解决这个问题?
MainWindow.cpp
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
ui->statusBar->addPermanentWidget(new QLabel(), 1);
ui->statusBar->addPermanentWidget(statusSelectionRect, 1);
ui->statusBar->addPermanentWidget(statusMousePos, 1);
//two connections doesn't work properly
connect(firstScrollbar, SIGNAL(valueChanged(int)), secondScrollbar, SLOT(setValue(int)));
connect(secondScrollbar, SIGNAL(valueChanged(int)), firstScrollbar, SLOT(setValue(int)));
/*************/
layout = ui->verticalLayout_5;
layout_2 = ui->verticalLayout_thumbnail;
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::on_actionOpen_triggered()
{
QString directory = QFileDialog::getExistingDirectory(this,
tr("Open Directory"),
QDir::homePath(),
QFileDialog::ShowDirsOnly| QFileDialog::DontResolveSymlinks);
thumbnail(directory);
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()){
QListWidgetItem(QIcon(finfo.absoluteFilePath()), finfo.fileName());
int column = scroll->columnCount();
for(int row=0; row < nrows; row++){
QPixmap pic(finfo.absoluteFilePath());
QImage image(finfo.absoluteFilePath());
QLabel *label = new QLabel();
label->setFrameShape(QFrame::WinPanel);
label->setFrameShadow(QFrame::Raised);
label->setAlignment(Qt::AlignCenter);
QScreen *screen = QGuiApplication::primaryScreen();
QRect screenGeometry = screen->geometry();
int height = screenGeometry.height();
int width = screenGeometry.width();
label->setPixmap(pic.scaled(width-100,height,Qt::KeepAspectRatio));
scroll->addWidget(label, row, column);
}
}
}
void MainWindow::thumbnail(QString directory)
{
int nrows = 1;
int ncols = 1;
HorizontalScrollArea *scroll_2 = new HorizontalScrollArea(nrows, ncols);
layout_2->addWidget(scroll_2);
QDir dir(directory);
dir.setNameFilters({"*.png", "*.jpg"});
for(const QFileInfo & finfo: dir.entryInfoList()){
QListWidgetItem(QIcon(finfo.absoluteFilePath()), finfo.fileName());
int column = scroll_2->columnCount();
for(int row=0; row < nrows; row++){
QPixmap pic(finfo.absoluteFilePath());
QLabel *label_2 = new QLabel(QString("label: %1 %2")
.arg(row)
.arg(column));
label_2->setAlignment(Qt::AlignCenter);
label_2->setPixmap(pic.scaled(ui->frame->maximumWidth(),ui->frame->maximumWidth(),Qt::KeepAspectRatio));
scroll_2->addWidget(label_2, row, column);
}
}
}
水平滚动区域.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);
//setHorizontalScrollBarPolicy( 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();
}
};