Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. how to select an image in a scroll area?
QtWS25 Last Chance

how to select an image in a scroll area?

Scheduled Pinned Locked Moved Unsolved General and Desktop
2 Posts 2 Posters 535 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    milad-sh
    wrote on last edited by
    #1

    I have written the code below in order to load images in a scroll area. now on I want to select an image an apply zoom in and zoom out actions on the selected image. how can I do that?
    mainwindow.cpp

    MainWindow::MainWindow(QWidget *parent) :
        QMainWindow(parent),
        ui(new Ui::MainWindow)
    {
        ui->setupUi(this);
    
        layout = ui->verticalLayout_5;
    }
    
    MainWindow::~MainWindow()
    {
        delete ui;
    }
    
    void MainWindow::on_actionOpen_triggered()
    {
    
    
        QString directory = QFileDialog::getExistingDirectory(this,
                                                              tr("Open Directory"),
                                                              QDir::homePath(),
                                                              QFileDialog::ShowDirsOnly| QFileDialog::DontResolveSymlinks);
        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()){
            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);
    
                label->setPixmap(pic.scaled(600,500,Qt::KeepAspectRatio));
                scroll->addWidget(label, row, column);
            }
        }
    }
    

    horizontalscrollarea.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);
        }
    
        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();
        }
    };
    
    1 Reply Last reply
    0
    • mrjjM Offline
      mrjjM Offline
      mrjj
      Lifetime Qt Champion
      wrote on last edited by mrjj
      #2

      Hi and welcome to the forums
      You could use something like
      https://wiki.qt.io/Clickable_QLabel
      to make it able to detect mousePress.

      • zoom in and zoom out actions ?
        You mean zooming as in image size shown or
        as in hovering effect when mouse is over the image or leaving it ?
        I assume hover and for that you need to implement
        enterEvent and leaveEvent
        Like shown here
        https://stackoverflow.com/questions/40729040/how-to-create-qt-label-hover-effect

      It just set text color so you need to tell what type of effect you had in mind ?
      One option is to also override paintEvent and paint the effect using QPainter.
      for more advanced stuff, there is also
      http://doc.qt.io/qt-5/qgraphicseffect.html#details

      1 Reply Last reply
      1

      • Login

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • Users
      • Groups
      • Search
      • Get Qt Extensions
      • Unsolved