Solved How to Get Selected QLabel's Pixmap?
-
I'm trying to select a QLabel and get its pixmap. Subclassed QLabel:
qlabelevent.h#ifndef QLABELEVENT_H #define QLABELEVENT_H #include <QWidget> #include <QLabel> #include <QMouseEvent> class qlabelEvent : public QLabel { Q_OBJECT public: explicit qlabelEvent(QWidget *parent); void mousePressEvent(QMouseEvent *pressEvent); signals: void Mouse_Pressed(); public slots: }; #endif // QLABELEVENT_H
qlabelevent.cpp:
#include "qlabelevent.h" #include <QWidget> qlabelEvent::qlabelEvent(QWidget *parent) : QLabel(parent) { } void qlabelEvent::mousePressEvent(QMouseEvent *pressEvent) { emit Mouse_Pressed(); }
mainwindow.h private slot:
private slots: void Mouse_Pressed();
mainwindow.cpp connect and Mouse_Pressed() function:
//in the constructor connect(ui->mouseEventLabel, SIGNAL(Mouse_Pressed()), this, SLOT(Mouse_Pressed())); void MainWindow::Mouse_Pressed() { const QPixmap *pixMap = ui->mouseEventLabel->pixmap(); ui->testoLabel->setPixmap(*pixMap); }
I just want to select a label in the UI (there is many of them so i can not do just like the above function).
Any idea how to do? -
Question is slightly confused me. Still let me try. If your question is how to get the pixmap from label you have to use the pixmap(..) function only. You have no other choice. Your code is already doing. So what is the real question. May be you are trying asking something different.
-
@dheerendra I am creating 100 labels and each one has image, so has pixmaps. The question is, is it possible when i pressed any QLabel (in these 100 labels), can i get its pixmap.
My code is working for one and specific QLabel. Problem is, when i clicked a label, how i know which label it is.
-
Ok. What you are doing is right. Define another signal inside the qlabelevent class called mytext(..).
signals :
void mytext(QString text)void MainWindow::Mouse_Pressed()
{
const QPixmap *pixMap = ui->mouseEventLabel->pixmap();
ui->testoLabel->setPixmap(*pixMap);
emit mytext(this->text())
}You can use the mytext(..) signal wherever you want.
Why do you want to identify which label ? What are you doing with that ? Based on this I can suggest you to do.
There is another way to identify the label. You can use the objectName().. as well.
-
@dheerendra I don't want to emit text Mr. Dheerendra. I am making a simple gallery and i want to transfer pixmap to another qlabel.
Just tried a simple visual:
When a label (in the right side) clicked, i just want to set its image to label at left side.
Btw i created labels with code, not with ui. -
see if this simple example helps.
MyWidget::MyWidget(QWidget *parent)
: QWidget(parent)
{
this->m_lab = new MyLabel;
this->m_lab2 = new MyLabel;
this->m_lab->setPixmap(QPixmap("/Users/dheeru/myImage.png"));this->hlyt = new QHBoxLayout(this); this->hlyt->addWidget(m_lab); this->hlyt->addWidget(m_lab2); connect(m_lab,SIGNAL(mypix(const QPixmap*)),this,SLOT(changeMyPix(const QPixmap*)));
}
void MyWidget::changeMyPix(const QPixmap* pix){
this->m_lab2->setPixmap(*pix);
}
======= Custom Label =============
MyLabel::MyLabel()
{}
void MyLabel::mousePressEvent(QMouseEvent *ev)
{
emit mypix(this->pixmap());
} -
Hi
Just as a note.
a QListWidget in icon mode would most likely work better than tons of labels. -
Hit the bull's-eye @mrjj this actually what i want to do and completely appropriate for my demand. So let me write here how i did a quite simple example:
///in the constructor QDirIterator it("/ImageDirectory", QDir::NoDotAndDotDot, QDirIterator::Subdirectories); for(int i = 0; i < 200; i++) { QString fileName = it.next(); QListWidgetItem *item = new QListWidgetItem(QIcon(fileName ), NULL); ui->listWidget->addItem(item); } //in the itemClicked slot void MainWindow::on_listWidget_itemClicked(QListWidgetItem *item) { QPixmap pixmapFromIcon = item->icon().pixmap(item->icon().actualSize(QSize(70,70))); ui->testoLabel->setPixmap(pixmapFromIcon); }
Thank you @dheerendra and @mrjj
I couldn't marked as answer your answer @mrjj, it wasn't display in dropdown menu, maybe it might be a bug, here is a screenshot:
-
hi
Good to hear :)
Yep, im not sure what is up "correct answer"
i only get it on my own posts, if I start the thread
so not sure it works as intended.