How to transffer QImage or QPixmap from one window to another using signals and slots
-
@JonB i changed to Qstring and hello is not displaying. how to rectify this problem
wrote on 3 Nov 2021, 09:51 last edited by@swansorter said in How to transffer QImage or QPixmap from one window to another using signals and slots:
i changed to Qstring and hello is not displaying. how to rectify this problem
Are you sure the
connect()
statement was successful?
Are you sure to use the right class instance?
Can you showmy_qlabel
class header? -
@JonB i changed to Qstring and hello is not displaying. how to rectify this problem
wrote on 3 Nov 2021, 10:09 last edited by@swansorter said in How to transffer QImage or QPixmap from one window to another using signals and slots:
how to rectify this problem
Like I said, produce a minimal example, paste the code, and we will tell you what you are doing wrong.....
-
@swansorter said in How to transffer QImage or QPixmap from one window to another using signals and slots:
how to rectify this problem
Like I said, produce a minimal example, paste the code, and we will tell you what you are doing wrong.....
wrote on 3 Nov 2021, 11:16 last edited by swansorter 11 Mar 2021, 11:35mainwindow.h file #ifndef MAINWINDOW_H #define MAINWINDOW_H #include <QMainWindow> #include<QRubberBand> #include<my_qlabel.h> namespace Ui { class MainWindow; } class MainWindow : public QMainWindow { Q_OBJECT public: explicit MainWindow(QWidget *parent = 0); ~MainWindow(); static QPoint origin; static QPoint Move_test; static QRect f_r; private slots: void onGetImage (QString data); private: Ui::MainWindow *ui; my_qlabel *obj; QPoint origin_x; }; #endif // MAINWINDOW_H
``
my qlable.h file`
#ifndef MY_QLABEL_H
#define MY_QLABEL_H#include <QWidget>
#include <QLabel>
#include <QEvent>
#include <QMouseEvent>
#include <QDebug>
#include<QRubberBand>
class my_qlabel : public QLabel
{
Q_OBJECT
public:
explicit my_qlabel(QWidget *parent = 0);
void mouseMoveEvent(QMouseEvent *ev);
void mousePressEvent(QMouseEvent *ev);
void mouseReleaseEvent(QMouseEvent *ev);
int x,y;
signals:
void onSendImage(QString data);public slots:
private:QRubberBand *rubberBand=0; QPoint origin_x;
};
#endif // MY_QLABEL_H
#include "mainwindow.h"
#include "ui_mainwindow.h"
QPoint MainWindow::origin;
QPoint MainWindow::Move_test;
QRect MainWindow::f_r;
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
ui->label->setStyleSheet("QLabel{background: yellow;}");
obj=new my_qlabel(this);
connect(obj,SIGNAL(onSendimage(QString)),this,SLOT(onGetimage(QString)));
}MainWindow::~MainWindow()
{
delete ui;
}void MainWindow::onGetimage (QString data)
{
QPixmap image=data ;
ui->label_2->setPixmap(image);
}#include "my_qlabel.h"
#include<mainwindow.h>
my_qlabel::my_qlabel(QWidget *parent) :
QLabel(parent)
{
emit Mouse_Left("image");
}void my_qlabel::mouseMoveEvent(QMouseEvent *ev)
{
this->x = ev->x();
this->y = ev->y();
rubberBand->setGeometry(QRect(origin_x,ev->pos()).normalized());}
void my_qlabel::mousePressEvent(QMouseEvent *ev)
{origin_x=ev->pos(); if(!rubberBand) rubberBand=new QRubberBand(QRubberBand::Rectangle,this); rubberBand->setGeometry(QRect((origin_x),QSize())); rubberBand->show();
}
void my_qlabel::mouseReleaseEvent(QMouseEvent *ev)
{rubberBand->hide(); MainWindow::f_r=rubberBand->geometry(); qDebug()<<"f_r"<<MainWindow::f_r; QPixmap image(rubberBand->geometry().width(),rubberBand->geometry().height()); image = grab(rubberBand->geometry()); //copy the selected part emit onSendimage("image");
}
-
@swansorter said in How to transffer QImage or QPixmap from one window to another using signals and slots:
i changed to Qstring and hello is not displaying. how to rectify this problem
Are you sure the
connect()
statement was successful?
Are you sure to use the right class instance?
Can you showmy_qlabel
class header?wrote on 3 Nov 2021, 11:19 last edited by@KroMignon yes i am using right class instance . i have pasted my code
-
@KroMignon yes i am using right class instance . i have pasted my code
wrote on 3 Nov 2021, 11:25 last edited by@swansorter said in How to transffer QImage or QPixmap from one window to another using signals and slots:
yes i am using right class instance . i have pasted my code
Your code seem at little bit confusing to me.
Why do you use old connect syntax here:connect(obj,SIGNAL(Mouse_Left(QString)),this,SLOT(Mouse_Left(QString)));
and not new syntax?
connect(obj,&my_qlabel::Mouse_Left,this, &MainWindow::Mouse_Left);
Where did you add obj to the layout?
-
@KroMignon yes i am using right class instance . i have pasted my code
@swansorter And please be so kind and format your code properly to make it easier for others to read it...
-
@swansorter said in How to transffer QImage or QPixmap from one window to another using signals and slots:
how to rectify this problem
Like I said, produce a minimal example, paste the code, and we will tell you what you are doing wrong.....
wrote on 3 Nov 2021, 11:27 last edited by@JonB i resolved issue .by changing this
connect(obj,SIGNAL(Mouse_Left(QString)),this,SLOT(Mouse_Left(QString)));
to
connect(ui.label,SIGNAL(Mouse_Left(QString)),this,SLOT(Mouse_Left(QString)));
} -
mainwindow.h file #ifndef MAINWINDOW_H #define MAINWINDOW_H #include <QMainWindow> #include<QRubberBand> #include<my_qlabel.h> namespace Ui { class MainWindow; } class MainWindow : public QMainWindow { Q_OBJECT public: explicit MainWindow(QWidget *parent = 0); ~MainWindow(); static QPoint origin; static QPoint Move_test; static QRect f_r; private slots: void onGetImage (QString data); private: Ui::MainWindow *ui; my_qlabel *obj; QPoint origin_x; }; #endif // MAINWINDOW_H
``
my qlable.h file`
#ifndef MY_QLABEL_H
#define MY_QLABEL_H#include <QWidget>
#include <QLabel>
#include <QEvent>
#include <QMouseEvent>
#include <QDebug>
#include<QRubberBand>
class my_qlabel : public QLabel
{
Q_OBJECT
public:
explicit my_qlabel(QWidget *parent = 0);
void mouseMoveEvent(QMouseEvent *ev);
void mousePressEvent(QMouseEvent *ev);
void mouseReleaseEvent(QMouseEvent *ev);
int x,y;
signals:
void onSendImage(QString data);public slots:
private:QRubberBand *rubberBand=0; QPoint origin_x;
};
#endif // MY_QLABEL_H
#include "mainwindow.h"
#include "ui_mainwindow.h"
QPoint MainWindow::origin;
QPoint MainWindow::Move_test;
QRect MainWindow::f_r;
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
ui->label->setStyleSheet("QLabel{background: yellow;}");
obj=new my_qlabel(this);
connect(obj,SIGNAL(onSendimage(QString)),this,SLOT(onGetimage(QString)));
}MainWindow::~MainWindow()
{
delete ui;
}void MainWindow::onGetimage (QString data)
{
QPixmap image=data ;
ui->label_2->setPixmap(image);
}#include "my_qlabel.h"
#include<mainwindow.h>
my_qlabel::my_qlabel(QWidget *parent) :
QLabel(parent)
{
emit Mouse_Left("image");
}void my_qlabel::mouseMoveEvent(QMouseEvent *ev)
{
this->x = ev->x();
this->y = ev->y();
rubberBand->setGeometry(QRect(origin_x,ev->pos()).normalized());}
void my_qlabel::mousePressEvent(QMouseEvent *ev)
{origin_x=ev->pos(); if(!rubberBand) rubberBand=new QRubberBand(QRubberBand::Rectangle,this); rubberBand->setGeometry(QRect((origin_x),QSize())); rubberBand->show();
}
void my_qlabel::mouseReleaseEvent(QMouseEvent *ev)
{rubberBand->hide(); MainWindow::f_r=rubberBand->geometry(); qDebug()<<"f_r"<<MainWindow::f_r; QPixmap image(rubberBand->geometry().width(),rubberBand->geometry().height()); image = grab(rubberBand->geometry()); //copy the selected part emit onSendimage("image");
}
wrote on 3 Nov 2021, 11:29 last edited by JonB 11 Mar 2021, 11:31@swansorter
This is hardly the minimal example I asked for....Do not use
SIGNAL
/SLOT
macros if you are to have any chance of debugging what is going on. You do not even check yourconnect()
s return result. Use the new syntax only.You declare
Mouse_Left
as a signal, yet you provide your own implementation of a method with that name. I don't know that you can do that.I'm quite mixed up about what are signals and what are slots in main window vs the label.
If you want me to look further: Name your slots differently from your signals, else it's too confusing. Reduce to a minimal example. And enclose blocks of code with the form posts' Code tag.
UPDATE
Crossed with your latest post. Advice: get rid ofSIGNAL
/SLOT
in all your code, only use new style, to help both you and others. -
@swansorter And please be so kind and format your code properly to make it easier for others to read it...
wrote on 3 Nov 2021, 11:29 last edited by@jsulm sorry ill do that
-
@swansorter
This is hardly the minimal example I asked for....Do not use
SIGNAL
/SLOT
macros if you are to have any chance of debugging what is going on. You do not even check yourconnect()
s return result. Use the new syntax only.You declare
Mouse_Left
as a signal, yet you provide your own implementation of a method with that name. I don't know that you can do that.I'm quite mixed up about what are signals and what are slots in main window vs the label.
If you want me to look further: Name your slots differently from your signals, else it's too confusing. Reduce to a minimal example. And enclose blocks of code with the form posts' Code tag.
UPDATE
Crossed with your latest post. Advice: get rid ofSIGNAL
/SLOT
in all your code, only use new style, to help both you and others.wrote on 3 Nov 2021, 11:32 last edited by@JonB do you have the link for the new style SIGNAL/SLOT Diclaration
-
@JonB do you have the link for the new style SIGNAL/SLOT Diclaration
@swansorter said in How to transffer QImage or QPixmap from one window to another using signals and slots:
do you have the link for the new style SIGNAL/SLOT Diclaration
Easy to find with Google: https://doc.qt.io/qt-5/signalsandslots.html
-
@JonB do you have the link for the new style SIGNAL/SLOT Diclaration
wrote on 3 Nov 2021, 11:37 last edited by@swansorter
Additionally to @jsulm, you originally had it with yourconnect(test_label,&my_qlabel::sendImage,this,&MainWindow::onGetImage);
You then changed over to
SIGNAL
/SLOT
, for no discernible reason, which may be a clue as to why it went wrong....
27/29