Using QSettings to save and reload images stored in QLabels



  • 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();
        }
    }
    


  • 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.



  • @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.



  • @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 :)


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.