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. [SOLVED] How to get Visible Rect in QScrallArea's viewport() ?
Forum Updated to NodeBB v4.3 + New Features

[SOLVED] How to get Visible Rect in QScrallArea's viewport() ?

Scheduled Pinned Locked Moved General and Desktop
2 Posts 1 Posters 4.0k 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.
  • J Offline
    J Offline
    joonhwan
    wrote on last edited by
    #1

    Is there any easy way to get "visible" QRect for QScrollArea's viewport() ? I tried to search forum but fails.
    Currently I got following codes to get this.

    @
    class MyLabel : public QLabel
    {
    public:
    MyLabel(QWidget* widget=0)
    : QLabel(widget)
    {
    image = new GigBmpImage(this);
    image->Open("c:/TestBigImage.bmp");
    }
    virtual ~MyLabel()
    {}
    virtual QSize sizeHint(void) const
    {
    return image->Size();
    }
    void SetScrollArea(QScrollArea* s)
    {
    scrollArea = s;
    }
    protected:
    virtual void paintEvent(QPaintEvent* e)
    {
    QPainter painter(this);
    QScrollBar* hb = scrollArea->horizontalScrollBar();
    QScrollBar* vb = scrollArea->verticalScrollBar();

    QPoint viewableTopLeft(hb->value(), vb->value());
    QSize viewableSize = scrollArea->size() -
    QSize(vb->sizeHint().width(),
    hb->sizeHint().height()) -
    QSize(2scrollArea->frameWidth(),
    2
    scrollArea->frameWidth());
    QRect viewableRect(viewableTopLeft, viewableSize);

    viewableRect.translate(hb->value(),
    vb->value());

    qDebug() << "drawing Rect : " << e->rect()
    << ", viewable rect : " << viewableRect
    << ", viewport: " << painter.viewport()
    << ", window: " << painter.window();

    QRect rect = viewableRect;// e->rect());
    // rect.adjust(-1,-1,1,1);
    if(rect.x()<0) {
    rect.setX(0);
    }
    if(rect.y()<0) {
    rect.setY(0);
    }
    //image->Draw(&painter, rect, rect);

    __super::paintEvent(e);
    }
    QScrollArea* scrollArea;
    GigBmpImage* image;
    };

    QGigImageViewer::QGigImageViewer(QWidget parent, Qt::WFlags flags)
    : QScrollArea(parent)
    {
    MyLabel
    label = new MyLabel;
    label->SetScrollArea(this);

    this->setBackgroundRole(QPalette::Dark);
    this->setWidget(label);
    this->setWindowFlags(flags);
    }
    @

    When widget shows up, viewable rect is exactly what I want but, it differs from the correct value after I moving scrollbar handle around. Might be that I do not understand well about scrollbar / scrollarea / viewport relationship.

    joonhwan at gmail dot com

    1 Reply Last reply
    0
    • J Offline
      J Offline
      joonhwan
      wrote on last edited by
      #2

      Oh, after removing 35~36 lines of above, it looks like working just the way what i want.
      But, I'm still afraid this kind of implementation is another make my hand dirty kinda thing.
      Is there another kinda 'official' way to get this value out of it?

      joonhwan at gmail dot com

      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