Results 1 to 8 of 8

Thread: Qpainter

  1. #1
    Join Date
    Jan 2016
    Posts
    22
    Thanks
    13

    Default Qpainter

    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
    Qt Code:
    1. #include "otsu.h"
    2. #include "fenetreprincipale.h"
    3.  
    4. FenetrePrincipale::FenetrePrincipale(): QMainWindow()
    5. {
    6.  
    7. label =new QLabel(&fentroi);
    8.  
    9. label->setGeometry(100,100,400,400);
    10. QMenu *menuFichier = menuBar()->addMenu("Fichier");
    11. QAction *actionOuvrir = new QAction("Ouvrir", this);
    12. QAction *actionQuitter = new QAction("Quitter", this);
    13. menuFichier->addAction(actionOuvrir);
    14. menuFichier->addAction(actionQuitter);
    15.  
    16. QMenu *menuTraitement = menuBar()->addMenu("Traitement");
    17. QAction *actionBinarisation = new QAction("Binarisation", this);
    18. menuTraitement->addAction(actionBinarisation);
    19.  
    20. QObject::connect(actionOuvrir, SIGNAL(triggered(bool)), this, SLOT(ouvrir()));
    21. QObject::connect(actionQuitter, SIGNAL(triggered(bool)), this, SLOT(close()));
    22.  
    23. QObject::connect(actionBinarisation, SIGNAL(triggered(bool)), this, SLOT(traiterOtsu()));
    24. }
    25. void FenetrePrincipale::ouvrir()
    26. {
    27. nomFichier = QFileDialog::getOpenFileName(this, tr("Open File"),"C:/Users/amine/Downloads");
    28.  
    29.  
    30.  
    31. image = new QImage(nomFichier);
    32. QPixmap pixmap;
    33. pixmap.convertFromImage(*image);
    34. label->setPixmap(pixmap);
    35. fentroi.show(); // fentroi (fenetre qui affiche l'image) attribut de fenetre principale
    36. label->update();
    37.  
    38. }
    39.  
    40. void FenetrePrincipale::traiterOtsu()
    41. {
    42. OtsuAlgo traitement(nomFichier);
    43. traitement.traiter(nomFichier);
    44. traitement.binarisation();
    45. QImage *im = new QImage(traitement.getImage());
    46. QLabel *label1 = new QLabel(&fenSec);
    47. label1->setGeometry(100,100, 400, 400);
    48. label1->setPixmap(QPixmap::fromImage(*im));
    49. QLCDNumber *lcd = new QLCDNumber(&fenSec);
    50. lcd->move(70,70);
    51. lcd->setSegmentStyle(QLCDNumber::Flat);
    52. lcd->display(traitement.getSeuil());
    53. fenSec.show();
    54. traitement.detection();
    55. }
    To copy to clipboard, switch view to plain text mode 
    if some one can help me
    thank you.

  2. #2
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Qpainter

    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,
    _

  3. #3
    Join Date
    Jan 2016
    Posts
    22
    Thanks
    13

    Default Re: Qpainter

    thank you, i will try it later , i have a problem with my program

  4. #4
    Join Date
    Jan 2016
    Posts
    22
    Thanks
    13

    Default Re: Qpainter

    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
    Qt Code:
    1. #ifndef FENETREPRINCIPALE_H
    2. #define FENETREPRINCIPALE_H
    3.  
    4. #include <QtWidgets>
    5.  
    6. class FenetrePrincipale : public QMainWindow
    7. {
    8. Q_OBJECT
    9. public :
    10. FenetrePrincipale();
    11. public slots :
    12. void ouvrir();
    13. void traiterOtsu();
    14. private:
    15. QImage *image;
    16. QLabel *label;
    17. QString nomFichier;
    18. QWidget fentroi;
    19. QWidget fenSec;
    20.  
    21.  
    22.  
    23. };
    24.  
    25. #endif // FENETREPRINCIPALE_H
    To copy to clipboard, switch view to plain text mode 
    fenetrePrincipale.cpp
    Qt Code:
    1. #include "otsu.h"
    2. #include "fenetreprincipale.h"
    3.  
    4. FenetrePrincipale::FenetrePrincipale(): QMainWindow()
    5. {
    6.  
    7. label =new QLabel(&fentroi);
    8.  
    9. label->setGeometry(100,100,400,400);
    10. QMenu *menuFichier = menuBar()->addMenu("Fichier");
    11. QAction *actionOuvrir = new QAction("Ouvrir", this);
    12. QAction *actionQuitter = new QAction("Quitter", this);
    13. menuFichier->addAction(actionOuvrir);
    14. menuFichier->addAction(actionQuitter);
    15.  
    16. QMenu *menuTraitement = menuBar()->addMenu("Traitement");
    17. QAction *actionBinarisation = new QAction("Binarisation", this);
    18. menuTraitement->addAction(actionBinarisation);
    19.  
    20. QObject::connect(actionOuvrir, SIGNAL(triggered(bool)), this, SLOT(ouvrir()));
    21. QObject::connect(actionQuitter, SIGNAL(triggered(bool)), this, SLOT(close()));
    22.  
    23. QObject::connect(actionBinarisation, SIGNAL(triggered(bool)), this, SLOT(traiterOtsu()));
    24. }
    25. void FenetrePrincipale::ouvrir()
    26. {
    27. nomFichier = QFileDialog::getOpenFileName(this, tr("Open File"),"C:/Users/amine/Downloads");
    28.  
    29.  
    30.  
    31. image = new QImage(nomFichier);
    32. QPixmap pixmap;
    33. pixmap.convertFromImage(*image);
    34. label->setPixmap(pixmap);
    35. fentroi.show(); // fentroi (fenetre qui affiche l'image) attribut de fenetre principale
    36. label->update();
    37.  
    38. }
    39.  
    40. void FenetrePrincipale::traiterOtsu()
    41. {
    42. QVector<Position> v;
    43. OtsuAlgo traitement(nomFichier);
    44. traitement.traiter(nomFichier);
    45. traitement.binarisation();
    46. QImage *im = new QImage(traitement.getImage());
    47. QLabel *label1 = new QLabel(&fenSec);
    48. label1->setGeometry(100,100, 400, 400);
    49. label1->setPixmap(QPixmap::fromImage(*im));
    50. QLCDNumber *lcd = new QLCDNumber(&fenSec);
    51. lcd->move(70,70);
    52. lcd->setSegmentStyle(QLCDNumber::Flat);
    53. lcd->display(traitement.getSeuil());
    54. fenSec.show();
    55. traitement.detection();
    56. traitement.centrer();
    57.  
    58. v = traitement.getCentres();
    59.  
    60. QFile monFichier("C:/Users/amine/Downloads/essai.txt");
    61. if (!monFichier.open(QIODevice::WriteOnly|QIODevice::Text))
    62. return;
    63. QTextStream flux(&monFichier);
    64. for(int i=0;i< v.size();i++)
    65. {
    66. flux << "i " << v[i].getI() << " " << "j " << v[i].getJ() << endl;
    67. }
    68.  
    69. for (int i=0;i<v.size();i++)
    70. {
    71. QPainter painter;
    72. QPen pen(Qt::blue);
    73. painter.setPen(pen);
    74. painter.begin(image);
    75. painter.drawPoint(v[i].getI(),v[i].getJ());
    76. painter.end();
    77. }
    78.  
    79. }
    To copy to clipboard, switch view to plain text mode 

  5. #5
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Qpainter

    setPen after begin

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

    Cheers,
    _

  6. The following user says thank you to anda_skoa for this useful post:

    rafik (28th January 2016)

  7. #6
    Join Date
    Jan 2016
    Posts
    22
    Thanks
    13

    Default Re: Qpainter

    i am a bad programmer,
    thank you Mr anda, it run now

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

  8. #7
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Qpainter

    Of course, see QWidget::resize( or QWidget::setGeometry().

    Cheers,
    _

  9. The following user says thank you to anda_skoa for this useful post:

    rafik (28th January 2016)

  10. #8
    Join Date
    Jan 2016
    Posts
    22
    Thanks
    13

    Default Re: Qpainter

    thank you Mr anda.

Similar Threads

  1. Replies: 2
    Last Post: 26th December 2012, 01:03
  2. QPainter(&QPrinter) & QPainter(&QImage) communication
    By gufeatza in forum Qt Programming
    Replies: 2
    Last Post: 2nd February 2010, 07:25
  3. Replies: 5
    Last Post: 7th September 2009, 20:57
  4. QPainter::save and QPAinter::restore()
    By quickNitin in forum Newbie
    Replies: 2
    Last Post: 17th June 2006, 22:11
  5. Replies: 3
    Last Post: 30th April 2006, 19:22

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.