Answer the question
In order to leave comments, you need to log in
How to catch signal of overridden QGraphicsPixmapItem in main window slot?
There is a custom `QGraphicsView` , I catch mouse events in it to scale and pan the image. In `QGraphicsScene` there is a custom `QGraphicsPixmapItem` , in it, by `mousePressEvent` , pixel information is determined and a `pixelData` signal is emitted , which I want to catch by the main window:
void myPixItemX::mousePressEvent(QGraphicsSceneMouseEvent *event)
{
if (event->button() != Qt::RightButton) {
QPoint position(event->pos().x(), event->pos().y());
QImage local_image = QImage(qImg_x);
QColor *color = new QColor;
pixel_color = color->fromRgb(local_image.pixel(position));
if (pixel_color.isValid ()) {
pixel_data (position.x (), position.y (), pixel_color.red (), pixel_color.green (), pixel_color.blue ());
}
}
}
class myPixItemX : public QObject, public QGraphicsPixmapItem
{
Q_OBJECT
public:
myPixItemX(const QPixmap & pixmap, QGraphicsItem * parent = 0);
// Ну и далее по списку, не буду тут все приводить...
connect(ui->graphicsView->Pix, SIGNAL(pixel_data(int,int,int,int,int)),this , SLOT(color_pick(int,int,int,int,int)));
QObject::connect: Cannot connect (null)::pixel_data(int,int,int,int,int) to MainWindow::color_pick(int,int,int,int,int)
void ImageViewer::setPixmapImage(QByteArray image_data, int image_width, int image_height, int image_resolution)
{
del_grid();
scene->clear();
w_img = image_width;
h_img = image_height;
zoom = 0;
unsigned char data[image_resolution];
memcpy(data, image_data.toStdString().c_str(), image_resolution);
qImg = QImage(data, image_width, image_height, image_width, QImage::Format_Grayscale8);
pixmap = QPixmap::fromImage(qImg);
Pix = new myPixItemX(pixmap);
Pix->setQImg_x (qImg);
scene->addItem(Pix);
scene->setSceneRect(0, 0, pixmap.size().width(), pixmap.size().height());
this->fitInView();
}
self._photo = QGraphicsPixmapItem(QPixmap(self.qImg))
self._photo.mousePressEvent = self.pixelSelect
self._scene.addItem(self._photo)
Answer the question
In order to leave comments, you need to log in
I don't think it can be solved without redefining QGraphicsPixmapItem. It is best to create myPixItemX once at the beginning and update its pixmap by calling setPixmap. As you yourself understood, connect must be written after the creation of the object. To fix your solution, just write:
Pix = new myPixItemX(pixmap);
Pix->setQImg_x (qImg);
scene->addItem(Pix);
connect(Pix, SIGNAL(pixel_data(int,int,int,int,int)),this , SLOT(color_pick(int,int,int,int,int)));
class myPixItemX : public QGraphicsPixmapItem
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question