用Qt写一个预览多张图片的程序,及窗口放大缩小的程序
时间: 2023-05-28 16:06:06 浏览: 119
以下是一个使用Qt编写的预览多张图片的程序和窗口放大缩小的程序示例:
预览多张图片的程序:
```cpp
#include <QApplication>
#include <QMainWindow>
#include <QScrollArea>
#include <QGridLayout>
#include <QLabel>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QMainWindow *window = new QMainWindow;
QWidget *centralWidget = new QWidget(window);
QGridLayout *gridLayout = new QGridLayout(centralWidget);
QScrollArea *scrollArea = new QScrollArea(centralWidget);
scrollArea->setWidgetResizable(true);
QWidget *scrollWidget = new QWidget(scrollArea);
QGridLayout *scrollLayout = new QGridLayout(scrollWidget);
scrollArea->setWidget(scrollWidget);
for (int i = 1; i <= 10; ++i) {
QLabel *label = new QLabel(scrollWidget);
label->setPixmap(QPixmap(QString(":/images/image%1.png").arg(i)));
scrollLayout->addWidget(label, (i - 1) / 3, (i - 1) % 3);
}
gridLayout->addWidget(scrollArea);
window->setCentralWidget(centralWidget);
window->show();
return a.exec();
}
```
窗口放大缩小的程序:
```cpp
#include <QApplication>
#include <QMainWindow>
#include <QHBoxLayout>
#include <QVBoxLayout>
#include <QPushButton>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QMainWindow *window = new QMainWindow;
QWidget *centralWidget = new QWidget(window);
QHBoxLayout *hLayout = new QHBoxLayout(centralWidget);
QVBoxLayout *vLayout = new QVBoxLayout;
QPushButton *button1 = new QPushButton("Button 1", centralWidget);
QPushButton *button2 = new QPushButton("Button 2", centralWidget);
vLayout->addWidget(button1);
vLayout->addWidget(button2);
hLayout->addLayout(vLayout);
QWidget *stretchWidget = new QWidget(centralWidget);
stretchWidget->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
hLayout->addWidget(stretchWidget);
window->setCentralWidget(centralWidget);
window->setMinimumSize(300, 200);
window->show();
return a.exec();
}
```
在这个示例中,我们创建了一个水平布局,其中包含两个垂直布局,每个垂直布局包含一个按钮。我们还添加了一个占位符小部件,它具有可扩展的大小策略,以便在调整窗口大小时保持两个按钮的大小不变。
阅读全文