Qt: display an image (QLabel) inside a QScrollArea - c++

I am trying to display an image inside a QScrollArea located on the QMainWindow.
I want a fixed size for the image display, and scroll bars to appear if the loaded image is bigger than the QScrollArea. My problem is that when I load an image which is bigger than the QScrollArea, the image appears cut (which is ok) but no scroll bars appear on the UI.
Taking into account various recommandations from other stackoverflow questions, here is the generated code from the Qt designer:
mImageScrollArea = new QScrollArea(centralWidget);
mImageScrollArea->setObjectName(QString::fromUtf8("mImageScrollArea"));
mImageScrollArea->setGeometry(QRect(440, 0, 400, 700));
mImageScrollArea->setWidgetResizable(false);
scrollAreaWidgetContents = new QWidget();
scrollAreaWidgetContents->setObjectName(QString::fromUtf8("scrollAreaWidgetContents"));
scrollAreaWidgetContents->setGeometry(QRect(0, 0, 398, 698));
mLabel = new QLabel(scrollAreaWidgetContents);
mLabel->setObjectName(QString::fromUtf8("mLabel"));
mLabel->setGeometry(QRect(0, 0, 400, 700));
QSizePolicy sizePolicy(QSizePolicy::Ignored, QSizePolicy::Ignored);
sizePolicy.setHorizontalStretch(0);
sizePolicy.setVerticalStretch(0);
sizePolicy.setHeightForWidth(mLabel->sizePolicy().hasHeightForWidth());
mLabel->setSizePolicy(sizePolicy);
mLabel->setScaledContents(true);
mImageScrollArea->setWidget(scrollAreaWidgetContents);
When an image is loaded, I display it in the label as follow:
QPixmap wPixmap = QPixmap::fromImage(mImage);
ui.mLabel->resize(wPixmap.size());
ui.mLabel->setPixmap(wPixmap);
ui.mLabel->show();
Why aren't any scrollbars showing if the image I load is bigger than the QScrollArea ?

It would be more helpful if you provide UI file content instead of generated C++ code. Anyway, it seems that scrollAreaWidgetContents doesn't have a layout. You need to add a grid layout to it in Qt Designer. After you do this, you will not need to resize label or scrollAreaWidgetContents manually. They will be resized automatically. Calling show on label is not required either, it will be visible by default (unless you have hid it).

Related

QLabel::setPixmap() and QScrollArea::setWidget()

I've been tracking down a bug that boils down to this - if you show an image label inside a scroll area, the label will not be resized to the image's size if QLabel::setPixmap() is called after QScrollArea::setWidget().
This example illustrates the problem, just replace /path/to/some/image.png with some real image on your computer:
QScrollArea *scrollArea = new QScrollArea;
QLabel *label = new QLabel(scrollArea);
scrollArea->setWidget(label);
label->setPixmap(QPixmap("/path/to/some/image.png"));
scrollArea->show();
If you swap the lines to call setPixmap() before setWidget(), the label will be properly resized.
Why does this happen, and how can I force the label to resize properly?
Set your scroll area's widgetResizable property to true:
scrollArea->setWidgetResizable(true);

QLabel take full size into QToolBar

I want to put QLabel into QToolBar. I do that but the QLabel changes QToolBar size, and does not fill it, see the first image below. What I need is make QLabel fill the QToolBar and resize the image, to be the same size as QLabel and QToolBar.
screenshot of what I want to happening:
I'm working with Qt 5.1, MinGw 4.8, and image type is .gif.
Are you sure you must to use QLabel? Perhaps you should set the background image using Qt style sheets.
Link to customizing QToolBar.

Adding scroll bar to widget containing a layout in QT C++

I am new to QT and I am creating a widget that has a gridlayout. The gridlayout contains a matrix of QLineEdit widgets. The window resizes to fit the layout but when layout is large it goes off screen. When I maximize the screen, the QLineEdit widgets are resized to fit the screen and for large layouts they become extremely small.
I want to be able to resize the window without resizing the QLineEdit widgets and add scroll bars to navigate.
I tried the following with no luck:
Window->resize(QSize(500,500));
QScrollArea *scrollArea = new QScrollArea;
scrollArea->setWidget(Window);
where window is the widget containing the layout. Also, the window closes when after executing "scrollArea->setWidget(Window);" and I dont why.
If someone can help me out I would really appreciate it.
Thank You!
For disabling the vertical resize on the widgets, why don't you just use the setFixedHeight() method on the widgets?
For the menu bar, why don't you take it out of the widget that is scrollable. You can have a layout for the window that contains the menu bar and then the widget that contains everything else (scrollable part). Is that what you are looking for?
I fixed my problem by creating a QMainWindow with the menu bar. Then created a widget which includes the layout, set the Scroll Area to the widget. Finally set the central widget of the main widow to the scroll area.

resize problem in scroll area

Hello everyone, here is my code:
myplot *p = new myplot(gao.structpayloadgraph,
gao1.structpayloadgraph,
gao.structcol-2, "payload");
ui->scrollArea->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
ui->scrollArea->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
ui->scrollArea->setWidgetResizable(false);
p->resize(ui->scrollArea->size().width(), ui->scrollArea->size().height());
ui->scrollArea->setWidget(p);
I want p to take up the full available space of the scrollbar area and fit itself. However, the appearance looks 'squeezed' even though I called the resize function. What should I do to achieve the desired effect?
You have to treat the scroll area content widget as a normal QWidget. If you want automatic resize and you must use layouts in Qt. Try the following :
QVBoxLayout layout = new QVBoxLayout( ui->scrollAreaContent);
layout->setMargin(0);
layout->setContentsMargins(0,0,0,0);
layout->setSpacing(0);
ui->scrollAreaContent->setLayout( layout);
layout->addWidget(p);
NOTE: ui->scrollAreaContent is a guess, but I think you are using ui files and default content widget is named like that ...
Go to the top right of the QT creator designer screen (Object, Class), right click on the QScrollArea row and select the "Lay Out" menu item, choose a layout (eg vertical or horizontal layout), make sure that your QWidget has a minimum or above size policy. Your scroll widget should now resize with the layout.

How to set an initial size of a QScrollArea?

I know that this is a very specific C++ and Qt related question, but maybe someone can help me, anyway ...
See the code below: I want to display an image within a scroll area. The view port of the scroll area shall have a defined initial size. That means, if the image's size is bigger than the initial size of the view port, scroll bars 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 shall be possible to resize the whole application so that the view port will get another size than the initial one.
Unfortunatelly I was not able to find out, how to set the size of the view port. Qt's layout mechanism seems to set a default size for the view port, but up to now I was not able to change it. Setting a new size with
area->setMinimumSize( QSize( 300, 300 ) );
will actually set the demanded size, but then the scroll area looses the ability to get resized to a size smaller than 300x300.
Any ideas?
I think that you are looking at the problem the wrong way. The QScrollArea is just a widget that you put in a frame or QMainWindow. The size of the widget is controlled by the layout of the widget that contains it.
Take a look at this example from Trolltech: Image Viewer Example
You can try:
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 );
However layout and Qt is amazingly Voodoo. It is IMO its least functional part.
if that doesn't work, try calling QWidget::resize() on various widgets.
Is the scroll area the top level widget? If so, simply call
area->resize(300,300);
If it's inside a hierarchy you need to resize the toplevel appropriately (complex), or set the minimumSize of the area. You could also try to experiment with the LayoutPolicy - assuming the sizeHint is QSize(300,300) you can give it the appropriate size policy according to what's defined in https://doc.qt.io/qt-5/qsizepolicy.html#Policy-enum
I don't think you can do exactly that very easily, which is (if I'm reading correctly), size the widget so that the internal area is 300x300. You might be able to fudge it, however, since a scroll area is a type of frame, which inherits from QWidget. This means you could just call area->resize( 300 + fudge, 300 + fudge ), where your fudge values account for the extra bit taken up by the frame's drawing.
I'm not sure this would work in a dynamically resizable dialog, however. I haven't ever done anything quite like this.
If you're trying to display an image inside a scroll area, your best bet isn't going with a label.
You should try using a QGraphicsView/QGraphicsScene/QGraphicPixmapItem (instead of the Scroll Area and label). The performance is far better when displaying images. The scroll area and label will re-draw the image very poorly as you move around using the scroll bars.
For example, you have a ".ui" file with a QGraphicsView on the gui called "qgvImageView" and a QImage called "image"...
QGraphicsScene *scene = new QGraphicsScene(qgvImageView);
QPixmap pixTmp(QPixmap::fromImage(image));
QGraphicsPixmapItem * ppixItem = scene->addPixmap( pixTmp );
ppixItem->setPos(0,0);
Check out the QT Documentation. BTW: This was introduced in Qt 4.2
I'm not sure if this will specifically fix the problem, but there is a chance that the QGraphicsView will react better to what you're trying to do.
How about using
area->setGeometry(int x, int y, int w, int h);