Help Please! Problem with QScrollArea and QLabel image inside



  • Hello everyone,

    I am having a lot of trouble with this, please help me.

    I am following the Image Viewer example, but I am using Designer.
    I have a MainWindow, inside a QScrollArea and inside a QLabel to show an image.
    My GUI looks like this:

    !https://dl.dropboxusercontent.com/u/56483815/Qt/Screen1.jpg(Screen1)!

    And I have a zoom action.

    My files are:

    mainwindow.h

    @#ifndef MAINWINDOW_H
    #define MAINWINDOW_H

    #include <QMainWindow>

    namespace Ui {
    class MainWindow;
    }

    class MainWindow : public QMainWindow
    {
    Q_OBJECT

    public:
    explicit MainWindow(QWidget *parent = 0);
    ~MainWindow();

    private slots:
    void zoom();

    private:
    Ui::MainWindow *ui;
    };

    #endif // MAINWINDOW_H@

    and mainwindow.cpp

    @#include "mainwindow.h"
    #include "ui_mainwindow.h"
    #include <QImage>

    MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
    {
    ui->setupUi(this);

    QImage img("C:/Qt/Programas/PruebaScrollArea/Desert.jpg");
    
    ui->label->setPixmap(QPixmap::fromImage(img));
    
    connect(ui->actionZoom, SIGNAL(triggered()), this, SLOT(zoom()));
    

    }

    MainWindow::~MainWindow()
    {
    delete ui;
    }

    void MainWindow::zoom()
    {
    ui->label->resize(2*ui->label->pixmap()->size());

    }
    @

    When I run the program, the image show fine, with the scrollbars correctly like the following image:

    !https://dl.dropboxusercontent.com/u/56483815/Qt/Screen2.jpg(Screen2)!

    When I maximize, the image shows like this, with the scrollbars correctly disabled:

    !https://dl.dropboxusercontent.com/u/56483815/Qt/Screen3.jpg(Screen3)!

    But when I zoom to double the image size, the image seems to move and nothing happens to the scrollbars:

    !https://dl.dropboxusercontent.com/u/56483815/Qt/Screen4.jpg(Screen4)!

    What is happening there? I´been struggling with this and I don´t know why it doesn´t work well.

    In scrollarea I have widgerResizable to true and in label I have the scaleContents false.

    I´ll appreciate your help.


  • Lifetime Qt Champion

    Hi and welcome to devnet,

    You should rather scale the pixmap rather than resize the label so you'd be working directly with the image data.

    Hope it helps



  • Hi,

    Thanks for your answer.

    Isn´t this line:

    ui->label->resize(2*ui->label->pixmap()->size());

    doing that?

    Shouldn´t I resize the label that contains the pixmap so the pixmap itself can fit inside?

    I mean, I don´t exactly get what you mean, but in the Image Viewer example, they resize the label.


  • Lifetime Qt Champion

    It would if you had scaledContents set to true. Have a look again at the example description just above the adjustScrollBar code block



  • Hi,

    I´ve already tried with scaleContents both true and false, both from Designer and hard coded, and still it won´t work.

    Still can´t understand why. I think is a layout problem but don´t know how to fix it yet.


  • Lifetime Qt Champion

    I can't really comment since I can see any layout from your images.

    However, since there's not much code involved. Copy the code from the example and run it. Once you are sure it works properly, modify it to suite your need. That might be quicker to get to your goal.



  • Hi,

    Yes I might try that. The idea was to use Designer to do things faster, but I guess I´ll have to write the code for the GUI by hand.

    Thanks.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.