0
votes

I want to display a lot of icons in a label but as the number of icons is really high, I will need to be able to scroll horizontally and vertically in this label.

What I did : I created a ScrollArea with a horizontal and vertical scrollbar and also a label. However, it doesn't seem to work. I wrote a long sentence in the label but the scroll bars does not move what is inside the label. Actually, I think the scroll bars only relate to the scroll area but not to the label.

To summarize : I need a label of fixed size on my form where I would be able to scroll horizontally and vertically.

enter image description here enter image description here

1
I understood correctly, scrolling does not work??Vahagn Avagyan
@Programmer_ARM indeed. Actually, my guess is that the scroll bars would be useful if there were many things in the ScrollArea. Here there is only a label which fits perfectly the Scroll Area so I would rather need to be able to scroll inside the labelMysteryGuy
You can consider QTextBrowser for this task.Vahagn Avagyan
I am now showing an example of how to do this.Vahagn Avagyan

1 Answers

0
votes

It will help you to put a lot of pictures in one scrolling, and will be stabilized by their size.

.h file

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
class QLabel;
class QScrollArea;
class FlowLayout;

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    MainWindow(QWidget *parent = 0);
    ~MainWindow();
    void setImagePaths(const QStringList& paths);
private:
    QScrollArea* m_area;
    QVector <QLabel*> m_imagesLab;
    FlowLayout *flowLayout;

};

#endif // MAINWINDOW_H

.cpp file

#include "mainwindow.h"
#include "flowlayout.h"

#include <QScrollArea>
#include <QLabel>
#include <QVBoxLayout>

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
{
    m_area = new QScrollArea(this);
    m_area->setWidgetResizable(true);

    flowLayout = new FlowLayout;

    QWidget* widget = new QWidget(this);
    widget->setLayout(flowLayout);

    m_area->setWidget(widget);
    setCentralWidget(m_area);

}

void MainWindow::setImagePaths(const QStringList &paths)
{
    foreach (QString path, paths) {
        QLabel* lab = new QLabel(this);
        lab->setPixmap(path);
        flowLayout->addWidget(lab);
        m_imagesLab.push_back(lab);
    }
}
MainWindow::~MainWindow()
{

}

flowlayout.h and flowlayout.cpp you can find in QT example,

Flow Layout Example Shows how to arrange widgets for different window sizes.

Flow Layout implements a layout that handles different window sizes. The widget placement changes depending on the width of the application window.