Connect trouble
-
Hello,
I have a main class imagedit.cpp which contains button Filter and Image. When i click to Filtre button, imagedit open a new widget which contains other button (changeBlackAndWhite button for example). I wish to connect my image with these button. That is the code:#include "imagedit.h" #include "ui_imagedit.h" #include <QFileDialog> #include <QStandardPaths> #include <QListWidgetItem> #include <QPushButton> #include <QDebug> ImagEdit::ImagEdit(QWidget *parent) : QMainWindow(parent), ui(new Ui::ImagEdit) { ui->setupUi(this); path = new QString(); pix = new QPixmap(*path); } ImagEdit::~ImagEdit() { delete ui; delete pix; delete path; } void ImagEdit::on_open_clicked() { QString cheminInitial = QStandardPaths::writableLocation(QStandardPaths::PicturesLocation); QString cheminFichier = QFileDialog::getOpenFileName(this, "Sélectionnez un fichier", cheminInitial); QFileInfo fileInfo(cheminFichier); QString nomFichier = fileInfo.fileName(); QPushButton *button = new QPushButton(nomFichier, this); connect(button, SIGNAL(clicked()), this, SLOT(displayOnEdition())); *path = cheminFichier; QListWidgetItem *item = new QListWidgetItem; ui->library->addItem(item); ui->library->setItemWidget(item, button); } void ImagEdit::displayOnEdition() { QPixmap pix(*path); pix = pix.scaled(381, 271, Qt::KeepAspectRatio); ui->imageLabel->setPixmap(pix); } void ImagEdit::on_filter_clicked() { FilterArea *filter = new FilterArea(); filter->setImage(pix->toImage()); filter->show(); } void ImagEdit::on_rogner_clicked() { RognerArea *rogner = new RognerArea(); rogner->show(); } ############################################ #include "filterarea.h" #include "ui_filterarea.h" FilterArea::FilterArea(QWidget *parent) : QWidget(parent) , ui(new Ui::FilterArea) { ui->setupUi(this); } FilterArea::~FilterArea() { delete ui; } void FilterArea::on_NbFilterButton_clicked() { qDebug() << "NbFilterButton clicked"; emit applyNbFilter(); } void FilterArea::setImage(QImage image) { imageSelected = image; //connect(ui->NbFilterButton, SIGNAL(clicked()), ImagEdit.cpp), SLOT(appliquerFiltreNoirEtBlanc())); } void FilterArea::appliquerFiltreNoirEtBlanc() { qDebug() << "Appliquer filtre Noir et Blanc"; if (!imageSelected.isNull()) { QImage imageNoirEtBlanc(imageSelected.size(), QImage::Format_Grayscale8); for (int y = 0; y < imageSelected.height(); ++y) { for (int x = 0; x < imageSelected.width(); ++x) { QRgb pixel = imageSelected.pixel(x, y); int grayValue = qGray(pixel); imageNoirEtBlanc.setPixel(x, y, qRgb(grayValue, grayValue, grayValue)); } } //setImage(imageNoirEtBlanc); } else { qDebug() << "Erreur : Aucune image actuelle à traiter."; } }
-
@marius-thorre said in Connect trouble:
//connect(ui->NbFilterButton, SIGNAL(clicked()), ImagEdit.cpp), SLOT(appliquerFiltreNoirEtBlanc()));
I don't know whether it does what you want, but are you asking for the syntax here for your "Connect trouble"? It would be
connect(ui->NbFilterButton, SIGNAL(clicked()), this, SLOT(appliquerFiltreNoirEtBlanc()));
If you intend to call
FilterArea::setImage()
more than once you should not put aconnect()
statement inside it.connect()
s are usually done once in the constructor. -
@JonB I find something:
#include "imagedit.h" #include "ui_imagedit.h" #include <QFileDialog> #include <QStandardPaths> #include <QListWidgetItem> #include <QPushButton> #include <QDebug> ImagEdit::ImagEdit(QWidget *parent) : QMainWindow(parent), ui(new Ui::ImagEdit) { ui->setupUi(this); path = new QString(); pix = new QPixmap(*path); } ImagEdit::~ImagEdit() { delete ui; delete pix; delete path; } void ImagEdit::on_open_clicked() { QString cheminInitial = QStandardPaths::writableLocation(QStandardPaths::PicturesLocation); QString cheminFichier = QFileDialog::getOpenFileName(this, "Sélectionnez un fichier", cheminInitial); QFileInfo fileInfo(cheminFichier); QString nomFichier = fileInfo.fileName(); QPushButton *button = new QPushButton(nomFichier, this); connect(button, SIGNAL(clicked()), this, SLOT(displayOnEdition())); *path = cheminFichier; QListWidgetItem *item = new QListWidgetItem; ui->library->addItem(item); ui->library->setItemWidget(item, button); } void ImagEdit::displayOnEdition() { QPixmap pix(*path); pix = pix.scaled(381, 271, Qt::KeepAspectRatio); ui->imageLabel->setPixmap(pix); } void ImagEdit::on_filter_clicked() { FilterArea *filterarea = new FilterArea(); //connect(filterarea, filterarea->appliquerFiltreNoirEtBlanc(pix->toImage()), this, ui->imageLabel); filterarea->setImage(ui->imageLabel->pixmap().toImage()); filterarea->show(); } void ImagEdit::on_rogner_clicked() { RognerArea *rogner = new RognerArea(); rogner->show(); } ###################################### #include "filterarea.h" #include "ui_filterarea.h" #include <iostream> using namespace std; FilterArea::FilterArea(QWidget *parent) : QWidget(parent) , ui(new Ui::FilterArea) { ui->setupUi(this); } FilterArea::~FilterArea() { delete ui; } void FilterArea::on_NbFilterButton_clicked() { qDebug() << "NbFilterButton clicked"; emit applyNbFilter(); } void FilterArea::setImage(QImage image) { imageSelected = image; connect(ui->NbFilterButton, SIGNAL(clicked()), this, SLOT(appliquerFiltreNoirEtBlanc())); } void FilterArea::appliquerFiltreNoirEtBlanc() { qDebug() << "Appliquer filtre Noir et Blanc"; if (!imageSelected.isNull()) { cout << "coucou "<< imageSelected.isNull() << endl; QImage imageNoirEtBlanc(imageSelected.size(), QImage::Format_Grayscale8); for (int y = 0; y < imageSelected.height(); ++y) { for (int x = 0; x < imageSelected.width(); ++x) { QRgb pixel = imageSelected.pixel(x, y); int grayValue = qGray(pixel); imageSelected.setPixel(x, y, qRgb(grayValue, grayValue, grayValue)); } } } else { qDebug() << "Erreur : Aucune image actuelle à traiter."; } }
but i cannot change color of my picture (i check into imageSelectedIsNull and the result is 0, so it's the good image)
-
@marius-thorre said in Connect trouble:
imageSelected = image;
This copies
image
.imageSelected.setPixel()
This alters the copied image.
So after copying and changing in
imageSelected
, where do you then show this changed image? -
@marius-thorre i was thinking it change himself