Qt ScrollArea

We can show a image with automatic scroll bars with the use of QScrollArea.

This is a simple application with QScrollArea.

#include <QtGui/QApplication>
#include <QLabel>
#include <QScrollArea>


#include "qmlapplicationviewer.h"

Q_DECL_EXPORT int main(int argc, char *argv[])
{
    QApplication app(argc, argv);

    QLabel *imageLabel = new QLabel;

    QImage image(":/image.jpg");

    imageLabel->setPixmap(QPixmap::fromImage(image));

    QScrollArea *scrollArea = new QScrollArea;
    scrollArea->setBackgroundRole(QPalette::Dark);
    scrollArea->setWidget(imageLabel);

    scrollArea->show();

    return app.exec();
}

 

 In addation, we can inherit from QScrollArea and then use the class we inherited as a widget which is able to scroll automatically.

Like this:

#ifndef PICWINDOW_H
#define PICWINDOW_H

#include <QScrollArea>

class QLabel;
class QImage;

class PicWindow : public QScrollArea
{
    Q_OBJECT
public:
    explicit PicWindow(QWidget *parent = 0);

private:
    QLabel *imageLabel;
    QImage *image;

signals:

public slots:

};

#endif // PICWINDOW_H
原文地址:https://www.cnblogs.com/johnpher/p/2673441.html