Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

[Solved] Image overlay issue during run-time



  • Hi all,

    In my app, I try to connect each Combobox item to an image file. But, when I would choose one of the item during run-time, the new selected image is overlapped to the previous one. I think it is a result of memory leak but which kind of approach (or func or class) help to fix this issue?

    Also, any help/comment about my design approach for this application would highly be appreciated.

    Thanks in advance,

    @//main.cpp
    #include "mainwindow.h"
    #include <QApplication>

    int main(int argc, char *argv[])
    {
    QApplication a(argc, argv);
    MainWindow w;
    w.show();

    return a.exec&#40;&#41;;
    

    }@

    @//mainwindow.h
    #ifndef MAINWINDOW_H
    #define MAINWINDOW_H

    #include <QMainWindow>
    #include <QGraphicsScene>
    #include <QGraphicsView>

    namespace Ui {
    class MainWindow;
    }

    class MainWindow : public QMainWindow
    {
    Q_OBJECT

    public:
    explicit MainWindow(QWidget *parent = 0);
    ~MainWindow();

    private slots:
    void on_comboBox_currentIndexChanged(int index);

    private:
    Ui::MainWindow *ui;
    QGraphicsScene *scene;
    QGraphicsView *view;
    };

    #endif // MAINWINDOW_H
    @

    @//mainwindow.cpp
    #include <QGridLayout>
    #include <QGraphicsItem>
    #include "mainwindow.h"
    #include "ui_mainwindow.h"

    MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
    {
    ui->setupUi(this);
    scene = new QGraphicsScene(ui->centralWidget);
    view = new QGraphicsView(scene);
    QGridLayout *gLayout = new QGridLayout(ui->centralWidget);
    QVBoxLayout *vLayout = new QVBoxLayout();
    gLayout -> addWidget(ui->comboBox,0,0,1,2);
    gLayout -> addLayout(vLayout, 1,0,3,2);
    gLayout ->addWidget(view,0,2,3,3);

    ui->lineEdit->setEnabled(false);
    ui->lineEdit_2->setEnabled(false);
    ui->lineEdit_3->setEnabled(false);
    

    }

    MainWindow::~MainWindow()
    {
    delete ui;
    }

    void MainWindow::on_comboBox_currentIndexChanged(int index)
    {
    QGraphicsPixmapItem *item;

    if(index == 0){
    
        ui->lineEdit->hide();
        ui->lineEdit_2->hide();
        ui->lineEdit_3->hide();
        item = new QGraphicsPixmapItem(QPixmap("C:\\... jpg"));
        scene->addItem(item);
        view->show();}
    
    else if(index == 1) {
        ui->lineEdit->show();
        ui->lineEdit_2->show();
        ui->lineEdit_3->hide();
        ui->lineEdit->setEnabled(true);
        ui->lineEdit_2->setEnabled(true);
        item= new QGraphicsPixmapItem(QPixmap("C:\\... jpg"));
        scene->addItem(item);
        view->show();}
    
    else if(index == 2){
        ui->lineEdit->show();
        ui->lineEdit_2->hide();
        ui->lineEdit_3->hide();
        ui->lineEdit->setEnabled(true);
        item = new QGraphicsPixmapItem(QPixmap("C:\\... jpg"));
        scene->addItem(item);
        view->show();}
    

    }
    @



  • Every time the currentIndex Changed, you create a new Item but never delete it.

    Seems one Item is enough for you.



  • Thanks,

    But for each combobox index, It would show a different image file. If I use, one item it always shows the same image file per each index.

    Also, how can I delete the item? To delete them, first I should check them if they are active(alive) or not? Which function and/or class I should use to check?



  • You can change the contents of the your item through QGraphicsPixmapItem::setPixmap()



  • Thanks a lot,

    It is solved...

    @void MainWindow::on_comboBox_currentIndexChanged(int index)
    {
    //QGraphicsPixmapItem *item;
    //scene->addItem(item);
    qDebug() << scene->items().isEmpty();
    qDebug() << item ->scene();

    if(!(scene->items().isEmpty())){scene->removeItem(item);}
    //if(item!=0x0) {delete item;}
    

    if(index == 0){
    ui->lineEdit->hide();
    ui->lineEdit_2->hide();
    ui->lineEdit_3->hide();
    item ->setPixmap(QPixmap("D:\bc913\capture\Capture115.JPG"));
    }
    else if(index == 1) {
    ui->lineEdit->show();
    ui->lineEdit_2->show();
    ui->lineEdit_3->hide();
    ui->lineEdit->setEnabled(true);
    ui->lineEdit_2->setEnabled(true);
    item ->setPixmap(QPixmap("D:\bc913\capture\Capture116.JPG"));
    }
    else if(index == 2){
    ui->lineEdit->show();
    ui->lineEdit_2->hide();
    ui->lineEdit_3->hide();
    ui->lineEdit->setEnabled(true);
    item ->setPixmap(QPixmap("D:\bc913\capture\Capture117.JPG"));

    }
    scene->addItem(item);
    qDebug() << item ->scene();
    qDebug() << item;
    view->show();

    }@


Log in to reply