qTableView select row -> display image - c++

What I want to achieve:
Whenever I select a row in my qTableView I want to display an image in sort of a "popup" way.
I'm not sure how to go about this. I mean I can display an image in an already present qLabel, but how can I show a qLabel in it's own "window" and destroy this window immediately if no row/another row is selected?
What I have so far:
QLabel *test = new QLabel(this);
test->setPixmap(QPixmap::fromImage(photo));
test->setBaseSize(photo.width(), photo.height());
test->show();
But this only creates a very small label in the left upper corner of the window. How can I get this to be a given size and at position with an offset of the current window?

You set parent to your label, so label appears on parent. Remove parent and label will be shown as separate window.
QLabel *test = new QLabel;
But of course in this case there is memory leak risk. So set attribute to label.
test->setAttribute(Qt::WA_DeleteOnClose);
With this line, when you close your label, it will be immediately destroyed. But I think you should not recreate label to show images. You can show or hide and set new pixmap to one label. If label visible (isVisible() method) set new image, if it is not visible, set new image too and show it. Use clicked() signal of QTableView to catch selection of row/columns.
Use move(), resize() or setGeometry to set different parameters of label.

You should implemented like:
Declared in header :QPixmap *pixH and QLabel * header;
header = new QLabel(this);
pixH = QPixmap(":/images/header.png");
header->setPixmap(pixH);
header->setGeometry(0,0,320,30);
header->setAlignment(Qt::AlignHCenter);

Related

QT How to click on the screen and make a Label appear in the same position?

I'm writing code to display labels on the screen and then drag and drop those labels. But when I use show() for those appear on the screen, I create multiple windows, each one of then with one label.
I already tried to use layouts and add a widget in this every time that i click with the mouse on the screen, but i cant place then in a custom position, only side a side or one in the top of the other.
else if(event->button()==Qt::RightButton)
{
QLabel *child = new QLabel;
child->setPixmap(QPixmap("C:/Users/ILHA4/Desktop/boat.png"));
child->move(event->x(),event->y());
child->show();
}
This is from the mousePressEvent() function, and I don't know how to put all these labels that I create when I click on the screen in the same window.

In QT "ui->scrollArea->horizontalScrollBar()->setValue(0)" has no effect

So in my UI designer I have a ScrollArea Widget, I then in my MainWindow.cpp, I create a QGraphicScene and a QGraphics View. I create a new widget and give that widget a QVBoxLayout so that it will auto-size(which is correct to my understanding).
I then use ui->scrollArea->setWidget(widget); to make this new widget the child of my scrollView.
All of this seems correct because I have scroll bars that I can navigate my scene with. However; using the line ui->scrollArea->horizontalScrollBar()->setValue(0); still has no effect on the scroll bar values.
scene = new QGraphicsScene();
scene->setSceneRect(0,0,2500,2500);
view = new QGraphicsView(scene);
QWidget *widget = new QWidget;
view->setBackgroundBrush(Qt::white);
QVBoxLayout* bLayout = new QVBoxLayout(widget);
ui->scrollArea->setWidget(widget);
bLayout->addWidget(view);
widget->show();
ui->scrollArea->horizontalScrollBar()->setValue(0);
ui->scrollArea->verticalScrollBar()->setValue(0);
I just had the this problem. Then, after debugging with ui->scrollArea->verticalScrollBar()->value() I realized that the scrolling area has no size before the component is show on the screen, i.e., it does not change the scrolling because it is not visible yet.
This is a sample Python code, but the is the same for C++, except the Language Syntax.
from PyQt5.QtWidgets import QDialog
...
dialog = QDialog()
...
verticalScrollBar = dialog.QPlainTextEdit.verticalScrollBar()
horizontalScrollBar = dialog.QPlainTextEdit.horizontalScrollBar()
# Has no effect, if you print the values, you will see always 0
verticalScrollBar.setValue( horizontalScrollBar.maximum() )
horizontalScrollBar.setValue( horizontalScrollBar.minimum() )
dialog.show()
# Now it has effect
verticalScrollBar.setValue( horizontalScrollBar.maximum() )
horizontalScrollBar.setValue( horizontalScrollBar.minimum() )
Autoscroll PyQT QTextWidget
If you are sure to address the correct scrollbar (as pointed in the comments by thuga), maybe check if your scroll area is modified after that init. I mean I'm not sure of the bahaviour, but if you modified some text in your widget for example, I think the scrollbar will be impacted.
You may need to catch some of your widget's event to reapply those:
ui->scrollArea->horizontalScrollBar()->setValue(0);
ui->scrollArea->verticalScrollBar()->setValue(0);
If it doesn't help, you should try to debug by tracking scrollbar value with
ui->scrollArea->verticalScrollBar()->value()
and see if your set is well applied, and maybe when it is overriden
Just in case, you may also try some of the methods indicated here, but it's probably not relevant to your issue: setValue of ScrollBar Don't work at first time

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);

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.

QTableWidget alignement with the borders of its parent QWidget

Let's consider we have QWidget that contains QTableWidget (only). So we want to resize the table by resizing the widget, and we dont want to have an indet between the QWidget border and cells of the table. What kind of property is making posible for QTableWidget to be aligned with the borders of it parent widget?
Thanks.
First, you want to make sure the QTableWidget is placed inside a layout. For instance,
QTableWidget* tw = new QTableWidget(parent_widget);
QVBoxLayout* layout = new QVBoxLayout();
layout->addWidget(tw);
parent_widget->setLayout(layout);
assuming parent_widget is already pointing to the widget containing the QTableWidget. This will ensure that the table resizes when the parent widget does. To have the table fill the entire space of the widget, just set the margin to zero on the layout. Try one of these:
layout->setMargin(0);
or
layout->setContentsMargins(0,0,0,0);