简体   繁体   中英

Qt Scrollbar doesn't appear on custom widget

I tried to modify this example code ( http://doc.qt.digia.com/4.6/widgets-scribble.html ) so the image can be scrolled when the size is too big.

Basically this is a code to create a mspaint-like program (my code is almost identical to the tutorial, except i changed the class name):

class ImageDisplay : public QWidget
{
    Q_OBJECT

public:
    ImageDisplay(QWidget *parent = 0);
    ~ImageDisplay();
    void LoadImage(QString img);    

protected:
    void mousePressEvent(QMouseEvent *event);
    void mouseMoveEvent(QMouseEvent *event);
    void mouseReleaseEvent(QMouseEvent *event);
    void paintEvent(QPaintEvent *event);
    void resizeEvent(QResizeEvent *event);

private:
    bool modified;
    bool scribbling;
    int myPenWidth;
    QColor myPenColor;
    QImage svgImage;
    QPoint lastPoint;

private:
    void ResizeImage(QImage *image, const QSize &newSize);
    void DrawLineTo(const QPoint &endPoint);
};

Code attaching custom widget to scroll area

...
scrollArea = new QScrollArea(centralWidget);
scrollArea->setWidgetResizable(true);
myImageDisplay = new ImageDisplay();
myImageDisplay->setGeometry(QRect(0, 0, 780, 559));
QSizePolicy sizePolicy(QSizePolicy::Preferred, QSizePolicy::Preferred);
myImageDisplay->setSizePolicy(sizePolicy);
myImageDisplay->setMinimumSize(QSize(0, 0));
scrollArea->setWidget(myImageDisplay);

gridLayout->addWidget(scrollArea, 0, 0, 1, 1);
...

I put my custom widget inside scrollarea, but the scrollbar never appears. When i debug it, the size of the widget cannot be larger than the srollarea.

I've read somewhere the size of a widget can;t be expanded larger than the container size, or sort of, i don't quite understand.

i found a inelegant solution which seems to be "hack" where i set the minimum size of the widget to be the size of the image, i put the code inside load image

void ImageDisplay::LoadImage(QString img)
{
    QImage loadedImage;

    loadedImage.load(img);

    QSize newSize = loadedImage.size().expandedTo(size());
    ResizeImage(&loadedImage, newSize);
    svgImage = loadedImage;
    modified = false;
    update();

    // "hack" code
    this->setMinimumHeight(newSize.height());
    this->setMinimumWidth(newSize.width());
}

This isn't really a "hack." This is how it is supposed to be. If you want the widget to never be smaller than the image it displays, then that's what you do; you tell it that it should never make itself smaller than the image. But in your code, you've specified that it can be smaller than its contents:

QSizePolicy sizePolicy(QSizePolicy::Preferred, QSizePolicy::Preferred);

What this means is that the widget will prefer to have a size that fits inside the visible area of its container, even if it means that its contents are getting cut off.

The more elegant way to do this is though, is using this instead:

QSizePolicy sizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);

This will tell the layout to never try and resize the widget at all. You set a size when you load the image, and it will keep that size unless you manually resize it yourself.

With that being said, maybe you should be looking at using a QLabel instead of a custom QWidget class. QLabel is already set up to display images, and I believe it knows on its own which size it should be.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM