Draw over QLabel inside QScrollarea - c++

I'm developing an image viewer, based on a QScrollArea and a QLabel that contains the image to show.
Now, I need to show text over the label, like the current mouse (x,y) position, image size, etc., in a specific point, but it can't be affected by the scrolling.
How can I do this?

The first thing I would try is:
container = new QWidget();
scrollArea = new QScrollArea(container);
pic = new QLabel();
pic->setPixmap(...);
scrollArea->setWidget(pic);
infoLabel = new QLabel("mouse is at 0, 0", container);
infoLabel->move(20, 20); // the desired non-scrolling position

Related

Is possible to have a widget that doesnt scroll in a QScrollArea?

Given this example:
Suppose A and B are QWidgets
Is it possible to keep everything starting from B static when the QScrollArea widget is scrolled?
The QScrollArea occupies the entire GUI, B is inside it to let the vertical ScrollBar closer to the right border of the GUI, or it would look like this:
What I can do in this case?
It is possible. You have to make use of the scrollbar being a widget of its own.
Here is the method:
Change your window in Qt Designer to be: spacer, QScrollArea (that will contain widget A), spacer, widget B, spacer; everything is in a QGridLayout (or QHBoxLayout but please read until the end).It is because widget B is outside the scroll area that it will not move during scrolling.
In your widget constructor, after ui->setupUi(); reparent and move the vertical scrollbar of your QScrollArea with these lines of code:
scrollArea->verticalScrollBar()->setParent(w);
layout->addWidget(sa->verticalScrollBar()); //Adds the scrollbar to the right of the layout.
Note about the margins:Obviously, you can easily push the scrollbar to the very right of your window by setting the layout right margin to 0.
If you also want it to cover the entire height of your window, while keeping some space between the other widgets and the window's border, that is where a QHBoxLayout will not suffice and you need a QGridLayout instead, set its top and bottom margin to 0 and add spacers (fixed size) to obtain the same visual result.
The C++ code for such a window would be:
QWidget* widget = new QWidget();
QGridLayout* layout = new QGridLayout(widget);
layout->setSpacing(0);
layout->setContentsMargins(9, 0, 0, 0);
widget->setLayout(layout);
QScrollArea* scrollArea = new QScrollArea(widget);
scrollArea->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOn);
QWidget* widgetB = new QLabel("Widget B", widget);
//Creates the spacer that will "simulate" the top and bottom margins
QSpacerItem* topSpacer = new QSpacerItem(0, 9, QSizePolicy::Minimum, QSizePolicy::Fixed),
* bottomSpacer = new QSpacerItem(0, 9, QSizePolicy::Minimum, QSizePolicy::Fixed);
layout->addItem(topSpacer, 0, 0);
layout->addWidget(scrollArea, 1, 0);
layout->addItem(bottomSpacer, 2, 0);
layout->addWidget(widgetB, 1, 1);
//Moves the scrollbar outside the scroll area
scrollArea->verticalScrollBar()->setParent(widget);
layout->addWidget(scrollArea->verticalScrollBar(), 0, 2, 3, 1);
QLabel* innerLabel = new QLabel("Some big label to force the scrolling");
scrollArea->setWidget(innerLabel);
innerLabel->setMinimumHeight(500);
widget->show();

Custom widgets in scroll area not respecting minimum?

I'm getting closer to getting a QScrollArea working, but it's still shrinking my custom widgets as they are added. Everything is resizing fluidly, and if the scroll area is too small, then the scroll bar appears, so it clearly has some idea of a minimum size.
At start, with two custom widgets in the scroll area, you can see some shrinking:
Here's the same window below the critical point. The text is now completely gone, but it won't shrink the QLineEdit, so it finally adds a scrollbar. (the scroll area has a blue background, the content widget is the purple)
I started in Design, but everything below the scroll area's widget is in code, as I was having trouble getting the vertical layout to work right using design.
Here's the starting point:
There's a page in a StackWidget that has two elements in a vertical layout. The scroll area has a QWidget. The constructor for MainWindow defines a vertical layout, assigning it to the member scrollWidgetLayout, and giving that layout to the scroll area's widget:
scrollWidgetLayout = new QVBoxLayout(ui->scrollAreaWidgetContents);
ui->scrollAreaWidgetContents->setLayout(scrollWidgetLayout);
The app starts on the first page of the stack widget, the user logs in, and the app runs off to fetch records from the server. Each record is turned into a widget:
RecordFolderWidget::RecordFolderWidget(Record *r, QWidget *parent) : QWidget(parent)
{
record = r;
//setSizePolicy(QSizePolicy::Expanding, QSizePolicy::MinimumExpanding);
QGridLayout *layout = new QGridLayout();
pathLineEdit = new QLineEdit();
finderButton = new QPushButton("...");
QLabel *nameLabel = new QLabel(record->name);
layout->setSpacing(5);
layout->setMargin(3);
layout->addWidget(nameLabel, 0, 0, 0, 1, Qt::AlignCenter);
layout->addWidget(pathLineEdit, 1, 0);
layout->addWidget(finderButton, 1, 1);
setLayout(layout);
//setMinimumHeight(sizeHint().height());
}
Note that there are some lines commented out, these are things I have been playing with to try to get it to work. The sizeHint, btw, appears to be correct, and does not change.
Once that Widget is created, it gets added to the scroll area's widget:
RecordFolderWidget *rf = new RecordFolderWidget(record);
rf->setParent(ui->scrollAreaWidgetContents);
scrollWidgetLayout->addWidget(rf);
I tried here to also resize the scroll areas contents, with no luck:
ui->scrollAreaWidgetContents->resize(rfSize.width(), rfSize.height() * records.count());
where rfSize was pulled from the custom widget's sizeHint after it was created, and this line was called once after the loop to create/add all of the widgets.
Other than the setMinimumHeight and resize above, I've tried changing the SizePolicy for the scrollAreaWidgetContents from preferred to expanding to minimumexpanding and did not see any difference. I'm sure I've missed something trivial, but just can't find it.
The problem that I see in your code is when adding the QLabel you are setting the rowSpan to 0, I have changed it and I can observe it correctly. In the next part I show my test code and the result:
QVBoxLayout *scrollWidgetLayout = new QVBoxLayout(ui->scrollAreaWidgetContents);
for(int i=0; i<10; i++){
QWidget *widget = new QWidget;
QGridLayout *layout = new QGridLayout(widget);
QLineEdit *pathLineEdit = new QLineEdit;
QPushButton *finderButton = new QPushButton("...");
QLabel *nameLabel = new QLabel(QString("name %1").arg(i));
layout->setSpacing(5);
layout->setMargin(3);
layout->addWidget(nameLabel, 0, 0, 1, 1, Qt::AlignCenter);
layout->addWidget(pathLineEdit, 1, 0);
layout->addWidget(finderButton, 1, 1);
scrollWidgetLayout->addWidget(widget);
}

Centering a widget inside another widget in Qt

I'm trying to center a QWidget localized inside a QTabWidget. When one resizes the inner QWidget I want it to be placed in the center always.
How can I do this?
1/ Create a layout in your QTabWidget: a QBoxLayout for example.
2/ Set your QWidget in it, with the desired alignment.
QTabWidget* tabwid = new QTabWidget(this);
QBoxLayout* layout = new QBoxLayout(QBoxLayout::LeftToRight, tabwid);
//if you want to set margins : layout->setContentsMargins(5, 5, 5, 5);
QWidget* widget = new QWidget();
layout->addWidget(widget, Qt::AlignCenter); // center alignment

QGridLayout with different size of cells

I'm trying to set a QGridLayout with four widget as in the image below:
however what I've managed with QGridLayout as of now is:
I don't see how I can set the size of the row different for column 0 and 1. Maybe QGridLayout is not the right way of doing it but I don't know of any other widget that would do the trick.
Does anyone have any idea how to achieve this?
I would use vertical and horizontal layouts instead of the grid layout. So you need two vertical layouts and horizontal one:
// Left side
QLabel *lbl1 = new QLabel(this);
QTableWidget *t = new QTableWidget(this);
QVBoxLayout *vl1 = new QVBoxLayout;
vl1->addWidget(lbl1);
vl1->addWidget(t);
// Right side
// QImage is not a widget, so it should be a label with image
QLabel *lbl2 = new QLabel(this);
QCustomPlot *pl = new QCustomPlot(this);
QVBoxLayout *vl2 = new QVBoxLayout;
vl2->addWidget(lbl2);
vl2->addWidget(pl);
// Create and set the main layout
QHBoxLayout mainLayout = new QHBoxLayout(this);
mainLayout->addLayout(vl1);
mainLayout->addLayout(vl2);
I don't think grids are the way to go here indeed...
You could try making a horizontal layout of 2 QFrames, in which you set a vertical layout each with the two widgets of that "column"

C++ Qt QGraphicsView: Remembering the position of scrollbars after reload

I have the following problem with scrollbars in graphics view. My application takes a PDF file and creates (in some way) a QImage out of it. The QImage is then converted to QPixmap, which is used to create a QGraphicsScene and from the QGraphicsScene I create a QGraphicsView. The QGraphicsView is added to the central widget and displayed.
The code looks approximately like this
QImage image;
image = loadImage(path);
QPixmap pixmap;
pixmap.convertFromImage(image);
scene = new QGraphicsScene(this);
scene->addPixmap(pixmap);
view = new QGraphicsView(scene);
textEdit = new QTextEdit(this)
QVBoxLayout *layout = new QVBoxLayout;
layout->addWidget(view);
layout->addWidget(textEdit);
QWidget *widget = new QWidget;
widget->setLayout(layout);
setCentralWidget(widget);
In the application, the view gets updated every time the PDF file changes. Now the problem is that the thing, which is in the PDF file, can also change in size and the scrollbars get messed up. I want the scrollbars after update to be in a position, such that I will see the same part of the PDF file as I saw before the update.
Can you give me some advice on how to accomplish this? I've searched for this issue, but nothing has worked in my case so far (I could have also be doing something wrong).
Thank you for your answers!
Before changing the view's contents remember scrollbar positions using view->horizontalScrollBar()->value() and view->verticalScrollBar()->value().
After changing reset previous values using setValue(int).
It's bad that you didn't provide any code that you have tried to use to accomplish this so I can't tell you what were you doing wrong.
Here is an example:
int pos_x = view->horizontalScrollBar()->value();
int pos_y = view->verticalScrollBar()->value();
pixmap_item->setPixmap(QPixmap::fromImage(new_image));
view->horizontalScrollBar()->setValue(pos_x);
view->verticalScrollBar()->setValue(pos_y);
Here pixmap_item is the stored result of scene->addPixmap(pixmap). It has QGraphicsPixmapItem* type.
I've changed the code, but it behaves strangely.
QImage image;
image = loadImage(path);
QPixmap pixmap;
pixmap.convertFromImage(image);
scene = new QGraphicsScene(this);
scene->addPixmap(pixmap);
int hsbv = -1;
int vsbv = -1;
if (view != NULL) {
hsbv = view->horizontalScrollBar()->value();
vsbv = view->verticalScrollBar()->value();
}
view = new QGraphicsView(scene);
textEdit = new QTextEdit(this)
QVBoxLayout *layout = new QVBoxLayout;
layout->addWidget(view);
layout->addWidget(textEdit);
QWidget *widget = new QWidget;
widget->setLayout(layout);
setCentralWidget(widget);
view->horizontalScrollBar()->setVealue(hsbv);
view->verticalScrollBar()->setValue(vsbv);
When I print out the view->horizontalScrollBar()->value() at the end, I always get 83 and I get 479 for the view->verticalScrollBar()->value(), which is very wierd, but when I print hsbv and vsbv I get reasonable numbers.