mainwindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <QImage>
#include <QLabel>
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
private slots:
void on_pushButton_2_clicked();
void on_pushButton_clicked();
private:
Ui::MainWindow *ui;
QLabel *label;
};
#endif // MAINWINDOW_H
mainwindow.cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
QImage image;
image.load("lena.jpg");
label = new QLabel();
label->setPixmap(QPixmap::fromImage(image));
label->resize(QSize(image.width(),image.height()));
ui->scrollArea->setWidget(label);
// ui->labelimage->setPixmap(QPixmap::fromImage(myImage).scaled(ui->labelimage->size()));
}
MainWindow::~MainWindow()
{
delete ui;
}
ui文件中添加控件ScrollArea,使用默认名字scrollArea。与代码中ui调用所对应。