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. Help Please! Problem with QScrollArea and QLabel image inside
Forum Updated to NodeBB v4.3 + New Features

Help Please! Problem with QScrollArea and QLabel image inside

Scheduled Pinned Locked Moved General and Desktop
7 Posts 2 Posters 1.9k Views 1 Watching
  • 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.
  • R Offline
    R Offline
    rdelgado1980
    wrote on last edited by
    #1

    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.

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      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

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      0
      • R Offline
        R Offline
        rdelgado1980
        wrote on last edited by
        #3

        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.

        1 Reply Last reply
        0
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #4

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

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          1 Reply Last reply
          0
          • R Offline
            R Offline
            rdelgado1980
            wrote on last edited by
            #5

            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.

            1 Reply Last reply
            0
            • SGaistS Offline
              SGaistS Offline
              SGaist
              Lifetime Qt Champion
              wrote on last edited by
              #6

              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.

              Interested in AI ? www.idiap.ch
              Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

              1 Reply Last reply
              0
              • R Offline
                R Offline
                rdelgado1980
                wrote on last edited by
                #7

                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.

                1 Reply Last reply
                0

                • Login

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