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



  • 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.



  • 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?


Log in to reply
 

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