title: Save and restore Qt widgets on exact location on the screen
body: <p>I've been trying to use the existing code to save and restore the labels and also in the future the Qt widgets at the exact position on the screen where the user placed it. The way I am trying to do this is by using using a <code>QPoint position</code> variable in the <code>Label</code> header file. </p>
  
  <p>So far the save and restore code works perfectly. The only thing is that the imported label image saves on the top left screen. I can't seem to crack saving and restoring it where the user had placed it using the <code>QPoint position</code> variable. </p>
  
  <p><strong>label.h</strong></p>
  
  <pre><code>#ifndef LABEL_H
  #define LABEL_H
  
  #include "mainwindow.h"
  
  #include &lt;QtGui&gt;
  #include &lt;QLabel&gt;
  #include &lt;QFileDialog&gt;
  #include &lt;QBoxLayout&gt;
  #include &lt;QVariant&gt;
  #include &lt;QGraphicsItem&gt;
  #include &lt;QPoint&gt;
  
  class Label : public QLabel
  {
  public:
      // Constructor
      Label();
  
      Label(QWidget* aParentWidget)
          : QLabel(aParentWidget)
          , m_nMouseClick_X_Coordinate(0)
          , m_nMouseClick_Y_Coordinate(0)
      {
          m_pParentWidget = aParentWidget;
      }
  
      ~Label();
  
      void mousePressEvent(QMouseEvent* event);
      void mouseMoveEvent(QMouseEvent* event);
      void mouseDoubleClickEvent(QMouseEvent* event);
  
      QPoint position;  // Exact location debugged on console
  
  private:
      int  m_nMouseClick_X_Coordinate;
      int  m_nMouseClick_Y_Coordinate;
      QWidget* m_pParentWidget;
  };
  
  #endif // LABEL_H
  </code></pre>
  
  <p><strong>label.cpp</strong></p>
  
  <pre><code>#include "label.h"
  
  //---------------------------------------
  // Deconstructor
  //---------------------------------------
  Label::~Label()
  {
  }
  
  void Label::mousePressEvent(QMouseEvent *event)
  {
      // Move the coordinates on the main window
      m_nMouseClick_X_Coordinate = event-&gt;x();
      m_nMouseClick_Y_Coordinate = event-&gt;y();
  
      // Display coordinates in qDebug
      //position = event-&gt;pos();
  
      position = event-&gt;pos();
      //qDebug() &lt;&lt; event-&gt;pos();
      qDebug() &lt;&lt; position;
  }
  
  void Label::mouseMoveEvent(QMouseEvent *event)
  {
      //-------------------------------------------------------------
      // Allow the user to drag the graphics on the Display
      //-------------------------------------------------------------
      move(event-&gt;globalX()-m_nMouseClick_X_Coordinate-m_pParentWidget-&gt;geometry().x(),
           event-&gt;globalY()-m_nMouseClick_Y_Coordinate-m_pParentWidget-&gt;geometry().y());
  }
  
  void Label::mouseDoubleClickEvent(QMouseEvent *event)
  {
      //QByteArray bArray;
      //QBuffer buffer(&amp;bArray);
      //buffer.open(QIODevice::WriteOnly);
  
      //--------------------------------
      // Open file dialog
      //--------------------------------
      QFileDialog dialog(this);
      dialog.setNameFilter(tr("Images(*.png, *.dxf, *.jpg"));
      dialog.setViewMode(QFileDialog::Detail);
      QString fileName = QFileDialog::getOpenFileName(this,
          tr("Open Images"),
          "/home",
          tr("Image Files (*.png *.jpg *.bmp)"));
  
  
      if (!fileName.isEmpty())
      {
          QImage image(fileName);
          Label::setPixmap(fileName);
          Label::adjustSize();
      }
  }
  </code></pre>
  
  <p><strong>mainwindow.cpp</strong></p>
  
  <pre><code>MainWindow::MainWindow(QWidget *parent) :
      QMainWindow(parent),
      ui(new Ui::MainWindow)
  {
      readSettings();
      ui-&gt;setupUi(this);
  
      // Set up the window size
      this-&gt;setWindowTitle(QString::fromUtf8("Raspberry PI GUI v1.0"));
  
      this-&gt;resize(800, 400);
  
      // Add label Button
      button = new QPushButton("Add Graphic", this);
      button-&gt;setGeometry(QRect(QPoint(10, 20), QSize(200, 50)));
      button-&gt;show();
      QObject::connect(button, SIGNAL(pressed()), this, SLOT(input_label()));
  }
  
  MainWindow::~MainWindow()
  {
      delete ui;
  }
  
  void MainWindow::input_label()
  {
      Label *label = new Label(this);
      label-&gt;setText("New Graphic");
      label-&gt;show();
      this-&gt;labels.append(label);
  }
  
  void MainWindow::writeSettings()
  {
    // Save location
    //https://www.ics.com/designpatterns/book/qsettings.html
  
    int i = 1;
    Q_FOREACH(auto label, labels)
    {
      if (label-&gt;pixmap() != nullptr)
      {
        QByteArray bArray;
        QBuffer buffer(&amp;bArray);
        buffer.open(QIODevice::WriteOnly);
        label-&gt;pixmap()-&gt;save(&amp;buffer, "PNG");
  
        QSettings settings("Save state", "GUIApp");
        settings.beginGroup("MainWindow");
        settings.setValue(QString("image-%1").arg(i), bArray);
  
        ++i;
      }
    }
  }
  
  void MainWindow::readSettings()
  {
      QSettings settings("Save state", "GUIApp");
  
      settings.beginGroup("MainWindow");
      int i = 1;
      while (true)
      {
        // Restore position
        // Need to find a way to find coordinates of x and y from label
        QPoint pos = settings.value("pos", QPoint(Label.position)).toPoint();
  
        QByteArray image = settings.value(QString("image-%1").arg(i)).toByteArray();
        if (!image.isNull()) {
          QPixmap pixmap;
          if (pixmap.loadFromData(image))
          {
            input_label(); // add new label
            this-&gt;labels.back()-&gt;setPixmap(pixmap);
          }
        } else break;
        ++i;
      }
  }
  
  void MainWindow::closeEvent(QCloseEvent *event)
  {
      writeSettings();
      event-&gt;accept();
  }
  </code></pre>