user4063815
user4063815

Reputation:

qTableView select row -> display image

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?

Upvotes: 1

Views: 417

Answers (2)

Subrata Das
Subrata Das

Reputation: 133

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

Upvotes: 0

Jablonski
Jablonski

Reputation: 18524

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.

Upvotes: 1

Related Questions