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. Custom Widget put in ScrollArea with no scrollbar
Forum Updated to NodeBB v4.3 + New Features

Custom Widget put in ScrollArea with no scrollbar

Scheduled Pinned Locked Moved Solved General and Desktop
8 Posts 3 Posters 126 Views 2 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.
  • F Offline
    F Offline
    fsign
    wrote last edited by
    #1

    I'm defining my own Widget with overwriting paintEvent() method. In paintEvent() method, I paint image and hope to display it in QScrollArea, the image displayed and bigger than window, but the scrollbar does not show. I'm curious about the reason, when call qDebug(), my custom widget's size almost always equals to scroll area size, I guess it's the reason. Am I right? How to solve it? Thanks a lot.

    MainWindow::MainWindow(QWidget* parent)
        : QMainWindow(parent)
        , ui(new Ui::MainWindow)
    {
        ui->setupUi(this);
    
        ui->scrollArea->setWidgetResizable(true);
        
        QVBoxLayout* layout = new QVBoxLayout(ui->scrollArea->widget());
        ImageWidget* imgWidget = new ImageWidget(this);
    
        layout->addWidget(imgWidget);
    }
    
    
    ImageWidget::ImageWidget(QWidget* parent)
        : QWidget { parent }
    {
        // setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
    
        m_pixmap.load("image_path");
    
        update();
    }
    
    void ImageWidget::paintEvent(QPaintEvent* event)
    {
        QPainter painter { this };
    
        qDebug("image width and height: %d*%d", m_pixmap.width(), m_pixmap.height());
        qDebug() << "current widget width and height: " << this->size();
    
        painter.drawPixmap(0, 0, m_pixmap);
    }
    
    QSize ImageWidget::sizeHint()
    {
        return m_pixmap.size();
    }
    
    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote last edited by
      #2

      Hi and welcome to devnet

      Which version of Qt ?
      On which platform ?
      What if you use a QLabel like shown in the Qt documentation of QScrollArea ?
      Did you check the scroll bar policies ?

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

      F 2 Replies Last reply
      0
      • SGaistS SGaist

        Hi and welcome to devnet

        Which version of Qt ?
        On which platform ?
        What if you use a QLabel like shown in the Qt documentation of QScrollArea ?
        Did you check the scroll bar policies ?

        F Offline
        F Offline
        fsign
        wrote last edited by
        #3

        @SGaist
        Thank you for watching, my Qt version is Qt6, on Macbook, and it works normally by QLabel. Before your answer, the scroll bar policy is default, after your answer, I tried "AlwaysOn", it did not work. This is my trial.

        ui->scrollArea->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOn);
        ui->scrollArea->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOn);
        
        1 Reply Last reply
        0
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote last edited by
          #4

          One of the issue you likely have is that you only return the size hint, which is "only" a hint. You should also adjust the widget geometry to match your image size.

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

          F 1 Reply Last reply
          0
          • SGaistS SGaist

            Hi and welcome to devnet

            Which version of Qt ?
            On which platform ?
            What if you use a QLabel like shown in the Qt documentation of QScrollArea ?
            Did you check the scroll bar policies ?

            F Offline
            F Offline
            fsign
            wrote last edited by
            #5

            @SGaist
            Add something, when using QLabel, if use setPixmap(), it works normally, but if subclass QLabel and overwrite paintEvent(), it does not work either.

            1 Reply Last reply
            0
            • Z Offline
              Z Offline
              Zbigniew-Sch
              wrote last edited by Zbigniew-Sch
              #6

              Hi,

              1. Create and insert "QScrollArea" object in your "QWidget" -> "m_pcoMyScrollArea"

              2. Create your own "QYourLabel" class derived from "QLabel",
                the parent is "m_pcoMyScrollArea"

              3. In the "QYourLabel" class do the following:

                 m_dScaleFactor = 1.0; // double - 1.0 or other value 
                 m_pcoImage = new QImage(); 
                 m_pcoImage->load("YourFilePath"); 
                 setPixmap(QPixmap::fromImage(*m_pcoImage)); 
                
                 QSize coImageSize(int(pixmap().width() * m_dScaleFactor), int(pixmap().height() * m_dScaleFactor)); 
                 resize(coImageSize);
                
                 // View and set scroll values
                 m_pcoScrollArea->horizontalScrollBar()->setValue(int(m_dScaleFactor * m_pcoScrollArea->horizontalScrollBar()->value()) + 
                      int(((m_dScaleFactor - 1.0) * m_pcoScrollArea->horizontalScrollBar()->pageStep()/2)));
                
                 m_pcoScrollArea->verticalScrollBar()->setValue(int(m_dScaleFactor * m_pcoScrollArea->verticalScrollBar()->value()) + 
                      int(((m_dScaleFactor - 1.0) * m_pcoScrollArea->verticalScrollBar()->pageStep()/2)));
                
              F 1 Reply Last reply
              0
              • SGaistS SGaist

                One of the issue you likely have is that you only return the size hint, which is "only" a hint. You should also adjust the widget geometry to match your image size.

                F Offline
                F Offline
                fsign
                wrote last edited by
                #7

                @SGaist
                Thanks a lot, I tried setGeometry() and it works fine.

                1 Reply Last reply
                0
                • Z Zbigniew-Sch

                  Hi,

                  1. Create and insert "QScrollArea" object in your "QWidget" -> "m_pcoMyScrollArea"

                  2. Create your own "QYourLabel" class derived from "QLabel",
                    the parent is "m_pcoMyScrollArea"

                  3. In the "QYourLabel" class do the following:

                     m_dScaleFactor = 1.0; // double - 1.0 or other value 
                     m_pcoImage = new QImage(); 
                     m_pcoImage->load("YourFilePath"); 
                     setPixmap(QPixmap::fromImage(*m_pcoImage)); 
                    
                     QSize coImageSize(int(pixmap().width() * m_dScaleFactor), int(pixmap().height() * m_dScaleFactor)); 
                     resize(coImageSize);
                    
                     // View and set scroll values
                     m_pcoScrollArea->horizontalScrollBar()->setValue(int(m_dScaleFactor * m_pcoScrollArea->horizontalScrollBar()->value()) + 
                          int(((m_dScaleFactor - 1.0) * m_pcoScrollArea->horizontalScrollBar()->pageStep()/2)));
                    
                     m_pcoScrollArea->verticalScrollBar()->setValue(int(m_dScaleFactor * m_pcoScrollArea->verticalScrollBar()->value()) + 
                          int(((m_dScaleFactor - 1.0) * m_pcoScrollArea->verticalScrollBar()->pageStep()/2)));
                    
                  F Offline
                  F Offline
                  fsign
                  wrote last edited by
                  #8

                  @Zbigniew-Sch
                  Thank you for your answer, I have solved my problem. I created custom MyWidget derived from QWidget and overwrited paintEvent method. Then called setGeometry in constructor method and called resize() when scaled. It works as I want.

                  1 Reply Last reply
                  0
                  • F fsign has marked this topic as solved

                  • Login

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