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. [Solved] Image overlay issue during run-time
QtWS25 Last Chance

[Solved] Image overlay issue during run-time

Scheduled Pinned Locked Moved General and Desktop
5 Posts 2 Posters 1.7k Views
  • 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.
  • B Offline
    B Offline
    bc913
    wrote on last edited by
    #1

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

    }
    @

    1 Reply Last reply
    0
    • D Offline
      D Offline
      dbzhang800
      wrote on last edited by
      #2

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

      Seems one Item is enough for you.

      1 Reply Last reply
      0
      • B Offline
        B Offline
        bc913
        wrote on last edited by
        #3

        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?

        1 Reply Last reply
        0
        • D Offline
          D Offline
          dbzhang800
          wrote on last edited by
          #4

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

          1 Reply Last reply
          0
          • B Offline
            B Offline
            bc913
            wrote on last edited by
            #5

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

            }@

            1 Reply Last reply
            0

            • Login

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