title: How to draw a point on top of a QImage for Qt5
body: <p>I am fairly new to Qt and I am completely stuck at this point. My only option was to turn to the experts. I have designed a GUI that will load an image into a Qlabel on a QMainWindow. I then want to draw multiple points on the image itself. However, what seems to be happening is that the point is being drawn behind the image and I don't know how to draw the point on top of the image. </p>
  
  <p>My Code:</p>
  
  <p>main.cpp</p>
  
  <pre><code>int main(int argc, char *argv[])
  {
     QApplication a(argc, argv);
     MainWindow w;
     w.show();
  
     return a.exec();
  } 
  </code></pre>
  
  <p>mainwindow.h</p>
  
  <pre><code>#ifndef MAINWINDOW_H
  #define MAINWINDOW_H
  
  #include &lt;QMainWindow&gt;
  #include &lt;QMouseEvent&gt;
  
  //Declaring the external Variables
  extern QString name;
  
  namespace Ui {
  class MainWindow;
  }
  
  class MainWindow : public QMainWindow
  {
  Q_OBJECT
  
  public:
  explicit MainWindow(QWidget *parent = 0);
  ~MainWindow();
  
  private slots:
  
  void on_actionOpen_triggered();
  void on_actionDraw_2_triggered();
  void on_actionCreate_2_triggered();
  void mousePressEvent(QMouseEvent* event);
  void paintEvent(QPaintEvent* e);
  
  private:
  Ui::MainWindow *ui;
  
  
  };
  
  #endif // MAINWINDOW_H
  </code></pre>
  
  <p>mainwindow.cpp</p>
  
  <pre><code>#include "mainwindow.h"
  #include "ui_mainwindow.h"
  #include "createdialog.h"
  #include &lt;QMouseEvent&gt;
  #include &lt;qpainter.h&gt;
  #include &lt;QFileDialog&gt;
  #include &lt;QLabel&gt;
  #include &lt;Qimage&gt;
  #include &lt;QPaintDevice&gt;
  #include &lt;QDebug&gt;
  #include &lt;boost/filesystem.hpp&gt;
  #include &lt;boost/filesystem/path.hpp&gt;
  #include &lt;boost/system/config.hpp&gt;
  #include "iostream"
  
  using namespace std;
  
  QString fileName;
  QString name;
  int drawFlag=0;
  int paintFlag=0;
  int xCord = 0;      //mouse click x location
  int yCord = 0;      //mouse click y loaction
  
  MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
  {
   ui-&gt;setupUi(this);
  }
  
  MainWindow::~MainWindow()
  {
    delete ui;
  }
  
  void MainWindow::on_actionOpen_triggered()
  {
  //Opening a dialog box to search for the images
  QFileDialog dialog(this);
  dialog.setNameFilter(tr("Images (*.png *.xpm *.jpg)"));
  dialog.setViewMode(QFileDialog::Detail);
  fileName = QFileDialog::getOpenFileName(this, tr("Open File"),
                                                     "C:/", tr("Images (*.png *.xpm *.jpg)"));
  
  //Pulling just the file name out of the file path
      std::string filename = fileName.toStdString();
      boost::filesystem::path p(filename);
      name = QString::fromStdString(p.stem().string());
  
  //Checking to see if the file name is empty
  if (!fileName.isEmpty()) {
      QImage img(fileName);
      int width = img.width();
      int height = img.height();
      MainWindow::showMaximized();
      ui-&gt;label-&gt;setGeometry(0,0,width,height);       //Setting the label to the size of the image
      ui-&gt;label-&gt;setPixmap(QPixmap::fromImage(img));  //Inserting the image into the label
  
  }
      else{
         ui-&gt;label-&gt;setText("Error");
  }
  
  }
  
  void MainWindow::mousePressEvent(QMouseEvent* event)
   {
    //If the draw flag is set record mouse press events
    if(drawFlag==1){
      if(event-&gt;button() == Qt::LeftButton){
          xCord = event-&gt;x();
          yCord = event-&gt;y();
          std::cout &lt;&lt; "x-location: " &lt;&lt; xCord &lt;&lt; endl;
          std::cout &lt;&lt; "y-location: " &lt;&lt; yCord &lt;&lt; endl;
          paintFlag=1;
          update();
  
      }
    }
  }
  void MainWindow::paintEvent(QPaintEvent* e)
  {
    QMainWindow::paintEvent(e);
    //When the paint flag is set then paint
    if(paintFlag==1){
      QPainter painter(this);
      QPen paintpen(Qt::red);
      paintpen.setWidth(10);
      QPoint p1;
      p1.setX(xCord);
      p1.setY(yCord);
      painter.setPen(paintpen);
      painter.drawPoint(p1);
    }
  }
  
  void MainWindow::on_actionDraw_2_triggered()
  {
  drawFlag=1;
  
  }
  </code></pre>
  
  <p>Any advice is very much appreciated. Thank you! </p>