How to set the initial size of QScrollArea?

I know this is a very specific question related to C ++ and Qt, but maybe someone can help me, anyway ...

See the code below: I want to display the image in the scroll pane. The viewport view port must have a specific initial size. This means that if the image size is larger than the initial viewport size, scrollbars will be visible, otherwise not.

// create label for displaying an image
QImage image( ":/test.png" );
QLabel *label = new QLabel( this );
label->setPixmap( image.toPixmap() );

// put label into scroll area
QScollArea *area = new QScrollArea( this );
area->setWidget( label );

// set the initial size of the view port
// NOTE: This is what I'd like to do, but this method does not exist :(
area->setViewPortSize( QSize( 300, 300 ) );

It should be possible to resize the entire application so that the view port gets a different size than the original one.

, , . Qt, -, , .

area->setMinimumSize( QSize( 300, 300 ) );

, 300x300.

?

+3
6

, . QScrollArea - , QMainWindow. , .

Trolltech:

+2

? ,

area->resize(300,300);

, () . LayoutPolicy - , sizeHint QSize (300 300), , https://doc.qt.io/qt-5/qsizepolicy.html#Policy. -enum

+2

:

class MyScrollArea : public QScrollArea
{
    virtual QSize sizeHint() const { return QSize( 300, 300 ); }
};

// create label for displaying an image
QImage image( ":/test.png" );
Label *label = new QLabel;
label->setPixmap( image.toPixmap() );

// put label into scroll area
QScollArea *area = new MyScrollArea( this );
area->setWidget( label );

Qt Voodoo. - .

, QWidget:: resize() .

+1

, , ( ), , 300x300. , , , QWidget. , area->resize( 300 + fudge, 300 + fudge ), fudge , .

, . .

0

, .

QGraphicsView/QGraphicsScene/QGraphicPixmapItem ( ). . .

, ".ui" QGraphicsView gui, "qgvImageView" QImage, ""...

QGraphicsScene *scene = new QGraphicsScene(qgvImageView);
QPixmap pixTmp(QPixmap::fromImage(image));
QGraphicsPixmapItem * ppixItem = scene->addPixmap( pixTmp );
ppixItem->setPos(0,0);

QT. BTW: Qt 4.2

, , , QGraphicsView , .

0

area->setGeometry(int x, int y, int w, int h);
0

Source: https://habr.com/ru/post/1698766/


All Articles