Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Using QSettings to save and reload images stored in QLabels
Forum Updated to NodeBB v4.3 + New Features

Using QSettings to save and reload images stored in QLabels

Scheduled Pinned Locked Moved Unsolved General and Desktop
4 Posts 3 Posters 1.4k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    Sahil885
    wrote on last edited by
    #1

    I've been struggling with this problem that I am trying to implement for this GUI application for an automation machine I am working on. What I am trying to do is save images loaded in dynamically by the user and be able to restore the same image and location where it's placed on the screen when the GUI application starts again with QSettings. The images are opened and loaded in by using QLabels.

    Here's my source code

    mainwindow.cpp

    #include "mainwindow.h"
    #include "ui_mainwindow.h"
    
    MainWindow::MainWindow(QWidget *parent) :
        QMainWindow(parent),
        ui(new Ui::MainWindow)
    {
        readSettings();
        ui->setupUi(this);
    
        // Set up the window size
        this->setWindowTitle(QString::fromUtf8("Raspberry PI GUI v1.0"));
        // 1366x768
        this->resize(800, 400);
    
        //-------------------------------------------
        // Setting up buttons on the main screen
        //-------------------------------------------
        // Add label
        button = new QPushButton("Add Graphic", this);
        button->setGeometry(QRect(QPoint(10, 20), QSize(200, 50)));
        button->show();
        QObject::connect(button, SIGNAL(pressed()), this, SLOT(input_label()));
    
        button = new QPushButton("Load Window", this);
        button->setGeometry(QRect(QPoint(10, 60), QSize(200, 50)));
        button->show();
        QObject::connect(button, SIGNAL(pressed()), this, SLOT(load_window()));
    
    }
    
    MainWindow::~MainWindow()
    {
        delete ui;
    }
    
    void MainWindow::closeEvent(QCloseEvent *)
    {
        // This will be called whenever this window is closed.
        writeSettings();
    }
    
    
    void MainWindow::input_label()
    {
        Label *label = new Label(this);
        label->setText("New Graphic");
        label->show();
    }
    
    void MainWindow::writeSettings()
    {
        QSettings settings("qsettingsexample.ini", QSettings::IniFormat);
    
        settings.beginGroup("MAINGUI");
        settings.setValue("size", this->size());
        settings.endGroup();
    
        settings.beginGroup("labels");
        settings.endGroup();
    
    }
    
    void MainWindow::readSettings()
    {
        QSettings settings("qsettingsexample.ini", QSettings::IniFormat);
    }
    

    Label.cpp

    #include "label.h"
    
    
    //---------------------------------------
    // Deconstructor
    //---------------------------------------
    Label::~Label()
    {
    
    }
    
    
    void Label::mousePressEvent(QMouseEvent *event)
    {
        // Move the coordinates on the main window
        m_nMouseClick_X_Coordinate = event->x();
        m_nMouseClick_Y_Coordinate = event->y();
    }
    
    void Label::mouseMoveEvent(QMouseEvent *event)
    {
        //-------------------------------------------------------------
        // Allow the user to drag the graphics on the Display
        //-------------------------------------------------------------
        move(event->globalX()-m_nMouseClick_X_Coordinate-m_pParentWidget->geometry().x(),
    
            event->globalY()-m_nMouseClick_Y_Coordinate-m_pParentWidget->geometry().y());
    }
    
    void Label::mouseDoubleClickEvent(QMouseEvent *event)
    {
        //--------------------------------
        // 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();
        }
    }
    
    Manohar_SLM J.HilkJ 2 Replies Last reply
    0
    • S Sahil885

      I've been struggling with this problem that I am trying to implement for this GUI application for an automation machine I am working on. What I am trying to do is save images loaded in dynamically by the user and be able to restore the same image and location where it's placed on the screen when the GUI application starts again with QSettings. The images are opened and loaded in by using QLabels.

      Here's my source code

      mainwindow.cpp

      #include "mainwindow.h"
      #include "ui_mainwindow.h"
      
      MainWindow::MainWindow(QWidget *parent) :
          QMainWindow(parent),
          ui(new Ui::MainWindow)
      {
          readSettings();
          ui->setupUi(this);
      
          // Set up the window size
          this->setWindowTitle(QString::fromUtf8("Raspberry PI GUI v1.0"));
          // 1366x768
          this->resize(800, 400);
      
          //-------------------------------------------
          // Setting up buttons on the main screen
          //-------------------------------------------
          // Add label
          button = new QPushButton("Add Graphic", this);
          button->setGeometry(QRect(QPoint(10, 20), QSize(200, 50)));
          button->show();
          QObject::connect(button, SIGNAL(pressed()), this, SLOT(input_label()));
      
          button = new QPushButton("Load Window", this);
          button->setGeometry(QRect(QPoint(10, 60), QSize(200, 50)));
          button->show();
          QObject::connect(button, SIGNAL(pressed()), this, SLOT(load_window()));
      
      }
      
      MainWindow::~MainWindow()
      {
          delete ui;
      }
      
      void MainWindow::closeEvent(QCloseEvent *)
      {
          // This will be called whenever this window is closed.
          writeSettings();
      }
      
      
      void MainWindow::input_label()
      {
          Label *label = new Label(this);
          label->setText("New Graphic");
          label->show();
      }
      
      void MainWindow::writeSettings()
      {
          QSettings settings("qsettingsexample.ini", QSettings::IniFormat);
      
          settings.beginGroup("MAINGUI");
          settings.setValue("size", this->size());
          settings.endGroup();
      
          settings.beginGroup("labels");
          settings.endGroup();
      
      }
      
      void MainWindow::readSettings()
      {
          QSettings settings("qsettingsexample.ini", QSettings::IniFormat);
      }
      

      Label.cpp

      #include "label.h"
      
      
      //---------------------------------------
      // Deconstructor
      //---------------------------------------
      Label::~Label()
      {
      
      }
      
      
      void Label::mousePressEvent(QMouseEvent *event)
      {
          // Move the coordinates on the main window
          m_nMouseClick_X_Coordinate = event->x();
          m_nMouseClick_Y_Coordinate = event->y();
      }
      
      void Label::mouseMoveEvent(QMouseEvent *event)
      {
          //-------------------------------------------------------------
          // Allow the user to drag the graphics on the Display
          //-------------------------------------------------------------
          move(event->globalX()-m_nMouseClick_X_Coordinate-m_pParentWidget->geometry().x(),
      
              event->globalY()-m_nMouseClick_Y_Coordinate-m_pParentWidget->geometry().y());
      }
      
      void Label::mouseDoubleClickEvent(QMouseEvent *event)
      {
          //--------------------------------
          // 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();
          }
      }
      
      Manohar_SLM Offline
      Manohar_SLM Offline
      Manohar_SL
      wrote on last edited by
      #2

      Hi @Sahil885

      Try storing Image bytearray in QSetting setvalue() function and retrieve the same image with key and convert it back to appropriate image format u require.

      Manohar SL
      Embedded Qt & QML Developer

      S 1 Reply Last reply
      3
      • S Sahil885

        I've been struggling with this problem that I am trying to implement for this GUI application for an automation machine I am working on. What I am trying to do is save images loaded in dynamically by the user and be able to restore the same image and location where it's placed on the screen when the GUI application starts again with QSettings. The images are opened and loaded in by using QLabels.

        Here's my source code

        mainwindow.cpp

        #include "mainwindow.h"
        #include "ui_mainwindow.h"
        
        MainWindow::MainWindow(QWidget *parent) :
            QMainWindow(parent),
            ui(new Ui::MainWindow)
        {
            readSettings();
            ui->setupUi(this);
        
            // Set up the window size
            this->setWindowTitle(QString::fromUtf8("Raspberry PI GUI v1.0"));
            // 1366x768
            this->resize(800, 400);
        
            //-------------------------------------------
            // Setting up buttons on the main screen
            //-------------------------------------------
            // Add label
            button = new QPushButton("Add Graphic", this);
            button->setGeometry(QRect(QPoint(10, 20), QSize(200, 50)));
            button->show();
            QObject::connect(button, SIGNAL(pressed()), this, SLOT(input_label()));
        
            button = new QPushButton("Load Window", this);
            button->setGeometry(QRect(QPoint(10, 60), QSize(200, 50)));
            button->show();
            QObject::connect(button, SIGNAL(pressed()), this, SLOT(load_window()));
        
        }
        
        MainWindow::~MainWindow()
        {
            delete ui;
        }
        
        void MainWindow::closeEvent(QCloseEvent *)
        {
            // This will be called whenever this window is closed.
            writeSettings();
        }
        
        
        void MainWindow::input_label()
        {
            Label *label = new Label(this);
            label->setText("New Graphic");
            label->show();
        }
        
        void MainWindow::writeSettings()
        {
            QSettings settings("qsettingsexample.ini", QSettings::IniFormat);
        
            settings.beginGroup("MAINGUI");
            settings.setValue("size", this->size());
            settings.endGroup();
        
            settings.beginGroup("labels");
            settings.endGroup();
        
        }
        
        void MainWindow::readSettings()
        {
            QSettings settings("qsettingsexample.ini", QSettings::IniFormat);
        }
        

        Label.cpp

        #include "label.h"
        
        
        //---------------------------------------
        // Deconstructor
        //---------------------------------------
        Label::~Label()
        {
        
        }
        
        
        void Label::mousePressEvent(QMouseEvent *event)
        {
            // Move the coordinates on the main window
            m_nMouseClick_X_Coordinate = event->x();
            m_nMouseClick_Y_Coordinate = event->y();
        }
        
        void Label::mouseMoveEvent(QMouseEvent *event)
        {
            //-------------------------------------------------------------
            // Allow the user to drag the graphics on the Display
            //-------------------------------------------------------------
            move(event->globalX()-m_nMouseClick_X_Coordinate-m_pParentWidget->geometry().x(),
        
                event->globalY()-m_nMouseClick_Y_Coordinate-m_pParentWidget->geometry().y());
        }
        
        void Label::mouseDoubleClickEvent(QMouseEvent *event)
        {
            //--------------------------------
            // 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();
            }
        }
        
        J.HilkJ Offline
        J.HilkJ Offline
        J.Hilk
        Moderators
        wrote on last edited by
        #3

        @Sahil885

        QSettings is more designed to use it on for single values than for huge quantities of data.

        Therefore use Qt's Ressoucre system und use the QSettings to simply save a reference to the img ("path to ressources") that was last loaded.


        Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


        Q: What's that?
        A: It's blue light.
        Q: What does it do?
        A: It turns blue.

        1 Reply Last reply
        3
        • Manohar_SLM Manohar_SL

          Hi @Sahil885

          Try storing Image bytearray in QSetting setvalue() function and retrieve the same image with key and convert it back to appropriate image format u require.

          S Offline
          S Offline
          Sahil885
          wrote on last edited by
          #4

          @Manohar_SL

          Hey thanks for your response
          Could you give me a code example on how to do that, it's still not clear to me. Thanks :)

          1 Reply Last reply
          1

          • Login

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • Users
          • Groups
          • Search
          • Get Qt Extensions
          • Unsolved