PDA

View Full Version : Qpainter



rafik
28th January 2016, 14:00
Hello,

i want to draw some points (gravity centers) in a QImage, how i can do that ? i heared about QPainter but i don't understand how doing that?

here the code i want to show the points in the fensec object



#include "otsu.h"
#include "fenetreprincipale.h"

FenetrePrincipale::FenetrePrincipale(): QMainWindow()
{

label =new QLabel(&fentroi);

label->setGeometry(100,100,400,400);
QMenu *menuFichier = menuBar()->addMenu("Fichier");
QAction *actionOuvrir = new QAction("Ouvrir", this);
QAction *actionQuitter = new QAction("Quitter", this);
menuFichier->addAction(actionOuvrir);
menuFichier->addAction(actionQuitter);

QMenu *menuTraitement = menuBar()->addMenu("Traitement");
QAction *actionBinarisation = new QAction("Binarisation", this);
menuTraitement->addAction(actionBinarisation);

QObject::connect(actionOuvrir, SIGNAL(triggered(bool)), this, SLOT(ouvrir()));
QObject::connect(actionQuitter, SIGNAL(triggered(bool)), this, SLOT(close()));

QObject::connect(actionBinarisation, SIGNAL(triggered(bool)), this, SLOT(traiterOtsu()));
}
void FenetrePrincipale::ouvrir()
{
nomFichier = QFileDialog::getOpenFileName(this, tr("Open File"),"C:/Users/amine/Downloads");



image = new QImage(nomFichier);
QPixmap pixmap;
pixmap.convertFromImage(*image);
label->setPixmap(pixmap);
fentroi.show(); // fentroi (fenetre qui affiche l'image) attribut de fenetre principale
label->update();

}

void FenetrePrincipale::traiterOtsu()
{
OtsuAlgo traitement(nomFichier);
traitement.traiter(nomFichier);
traitement.binarisation();
QImage *im = new QImage(traitement.getImage());
QLabel *label1 = new QLabel(&fenSec);
label1->setGeometry(100,100, 400, 400);
label1->setPixmap(QPixmap::fromImage(*im));
QLCDNumber *lcd = new QLCDNumber(&fenSec);
lcd->move(70,70);
lcd->setSegmentStyle(QLCDNumber::Flat);
lcd->display(traitement.getSeuil());
fenSec.show();
traitement.detection();
}


if some one can help me
thank you.

anda_skoa
28th January 2016, 14:08
Don't create QImage on the heap, you will just more easily leak it.

QImage is derived from QPaintDevice, which is what QPainter works on.

Just create a QPainter instance, call its begin() method with the image as its argument, paint using the painters methods and then call end().
The image then contains whatever you have drawn.

Cheers,
_

rafik
28th January 2016, 14:59
thank you, i will try it later , i have a problem with my program

rafik
28th January 2016, 19:43
i tried this (i added the last lines in cpp) but i didn't have the points, i think i didn't understand
fenetrePrincipale.h


#ifndef FENETREPRINCIPALE_H
#define FENETREPRINCIPALE_H

#include <QtWidgets>

class FenetrePrincipale : public QMainWindow
{
Q_OBJECT
public :
FenetrePrincipale();
public slots :
void ouvrir();
void traiterOtsu();
private:
QImage *image;
QLabel *label;
QString nomFichier;
QWidget fentroi;
QWidget fenSec;



};

#endif // FENETREPRINCIPALE_H

fenetrePrincipale.cpp


#include "otsu.h"
#include "fenetreprincipale.h"

FenetrePrincipale::FenetrePrincipale(): QMainWindow()
{

label =new QLabel(&fentroi);

label->setGeometry(100,100,400,400);
QMenu *menuFichier = menuBar()->addMenu("Fichier");
QAction *actionOuvrir = new QAction("Ouvrir", this);
QAction *actionQuitter = new QAction("Quitter", this);
menuFichier->addAction(actionOuvrir);
menuFichier->addAction(actionQuitter);

QMenu *menuTraitement = menuBar()->addMenu("Traitement");
QAction *actionBinarisation = new QAction("Binarisation", this);
menuTraitement->addAction(actionBinarisation);

QObject::connect(actionOuvrir, SIGNAL(triggered(bool)), this, SLOT(ouvrir()));
QObject::connect(actionQuitter, SIGNAL(triggered(bool)), this, SLOT(close()));

QObject::connect(actionBinarisation, SIGNAL(triggered(bool)), this, SLOT(traiterOtsu()));
}
void FenetrePrincipale::ouvrir()
{
nomFichier = QFileDialog::getOpenFileName(this, tr("Open File"),"C:/Users/amine/Downloads");



image = new QImage(nomFichier);
QPixmap pixmap;
pixmap.convertFromImage(*image);
label->setPixmap(pixmap);
fentroi.show(); // fentroi (fenetre qui affiche l'image) attribut de fenetre principale
label->update();

}

void FenetrePrincipale::traiterOtsu()
{
QVector<Position> v;
OtsuAlgo traitement(nomFichier);
traitement.traiter(nomFichier);
traitement.binarisation();
QImage *im = new QImage(traitement.getImage());
QLabel *label1 = new QLabel(&fenSec);
label1->setGeometry(100,100, 400, 400);
label1->setPixmap(QPixmap::fromImage(*im));
QLCDNumber *lcd = new QLCDNumber(&fenSec);
lcd->move(70,70);
lcd->setSegmentStyle(QLCDNumber::Flat);
lcd->display(traitement.getSeuil());
fenSec.show();
traitement.detection();
traitement.centrer();

v = traitement.getCentres();

QFile monFichier("C:/Users/amine/Downloads/essai.txt");
if (!monFichier.open(QIODevice::WriteOnly|QIODevice:: Text))
return;
QTextStream flux(&monFichier);
for(int i=0;i< v.size();i++)
{
flux << "i " << v[i].getI() << " " << "j " << v[i].getJ() << endl;
}

for (int i=0;i<v.size();i++)
{
QPainter painter;
QPen pen(Qt::blue);
painter.setPen(pen);
painter.begin(image);
painter.drawPoint(v[i].getI(),v[i].getJ());
painter.end();
}

}

anda_skoa
28th January 2016, 19:49
setPen after begin

Also how did you determine that it doesn't work?
You are neither saving nor displaying the modified image.

Cheers,
_

rafik
28th January 2016, 20:46
i am a bad programmer,
thank you Mr anda, it run now :)

just a question, can i resize the QLabel to appear larger?

anda_skoa
28th January 2016, 21:04
Of course, see QWidget::resize( or QWidget::setGeometry().

Cheers,
_

rafik
28th January 2016, 21:09
thank you Mr anda.