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. Debug pointer not following expected flow in 2nd and successive iterations
QtWS25 Last Chance

Debug pointer not following expected flow in 2nd and successive iterations

Scheduled Pinned Locked Moved General and Desktop
7 Posts 2 Posters 1.5k 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.
  • S Offline
    S Offline
    Sai Kamat
    wrote on last edited by
    #1

    I want to display a "generating image..." kind of modal dialog, other than the main GUI. This "generating image..." dialog should be temporary, and be displayed and disappear without user intervention. For displaying this dialog, the Qt code should check for existence of a usbResponse.txt file in a specific location in the PC's hard disk. If the usbResponse.txt file exists, then the dialog should pop-up. I am able to display this loading GIF.
    I am able to close the GIF whenever that txt file i.e. usbResponse.txt is modified, using FileSystemWatcher.

    I'm continuously scanning for the existence of the .txt using threads. When I find the file, I display the loading GIF. When the .txt is modified the GIF should disappear. This works fine for the first iteration, i.e. when (the following are observations after debugging)

    1. the usbResponse.txt exists => GIF is displayed

    2. when usbResponse.txt is modified => GIF is hidden & the .txt is deleted.

    THe problem, in next iteraiton,(i.e. all iterations after the first)

    1. the usbResponse.txt is created => the GIF is displayed.

    2. when usbResponse.txt is modified, the debug pointer continues to remain in

    afterFileHasBeenFound()

    whereas it should have gone in

    closeModified(const QString &str)
    What is my mistake here? Here is my code:
    mainwindow.h
    @
    #ifndef MAINWINDOW_H
    #define MAINWINDOW_H

    #include <QMainWindow>
    #include <QFile>
    #include <QDebug>
    #include <QFileSystemWatcher>
    #include "dialog.h"
    #include "mythread.h"
    
    namespace Ui {
    class MainWindow;
    }
    
    class MainWindow : public QMainWindow
    {
        Q_OBJECT
    
    public:
        explicit MainWindow(QWidget *parent = 0);
        ~MainWindow();
    
    
    public slots:
        void afterFileHasBeenFound();
        void closeModified(const QString &str);
    
    private slots:
    
    private:
        Ui::MainWindow *ui;
        Dialog *pDialog;
        MyThread *mThread;
    };
    

    #endif // MAINWINDOW_H
    @

    mythread.h
    @
    #ifndef MYTHREAD_H
    #define MYTHREAD_H

    #include <QThread>
    #include <QtCore>
    #include <QDebug>

    #define FILE_PATH "E:\QT1\dialogClose2\dialogClose2\usbResponse.txt"

    class MyThread : public QThread
    {
    Q_OBJECT
    public:
    explicit MyThread(QObject *parent = 0);
    void run();
    QString name;
    int exec();
    void checkFile();

    signals:
    void testSignal(QString message);
    void fileFoundDisplayGif();

    public slots:

    };

    #endif // MYTHREAD_H
    @

    dialog.h
    @
    #ifndef DIALOG_H
    #define DIALOG_H

    #include <QDialog>
    #include <QMovie>
    #include <QLabel>

    #define GIF_PATH "E:\QT1\timeStampPopUp\timeStampPopUp\loading.gif"
    namespace Ui {
    class Dialog;
    }

    class Dialog : public QDialog
    {
    Q_OBJECT

    public:
    explicit Dialog(QWidget *parent = 0);
    ~Dialog();
    void displayLoadingGif();

    private:
    Ui::Dialog *ui;
    };

    #endif // DIALOG_H

    @

    dialog.cpp
    @
    #include "dialog.h"
    #include "ui_dialog.h"

    Dialog::Dialog(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::Dialog)
    {
    ui->setupUi(this);
    displayLoadingGif();
    }

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

    void Dialog::displayLoadingGif()
    {
    QMovie *pMovie = new QMovie(GIF_PATH);
    ui->loadingGifLabel->setMovie(pMovie);
    pMovie->start();
    }
    @

    mythread.cpp
    @
    #include "mythread.h"

    MyThread::MyThread(QObject *parent) :
    QThread(parent)
    {
    }

    void MyThread::run()
    {
    exec();
    }

    int MyThread::exec()
    {
    while(1)
    {
    checkFile();
    emit(testSignal("hello world!!"));
    sleep(1);
    }
    }

    void MyThread::checkFile()
    {
    QFile file(FILE_PATH);
    if(file.exists())
    {
    qDebug()<<"exists";
    emit(fileFoundDisplayGif());
    }
    else
    qDebug()<<"doesn't exist";
    }

    @

    mainwindow.cpp
    @
    #include "mainwindow.h"
    #include "ui_mainwindow.h"

    MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
    {
    ui->setupUi(this);
    mThread = new MyThread(this);
    mThread->name = "mThread";
    connect(mThread, SIGNAL(fileFoundDisplayGif()), this, SLOT(afterFileHasBeenFound()), Qt::QueuedConnection);
    mThread->start();
    }

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

    void MainWindow::afterFileHasBeenFound()
    {
    if(pDialog != NULL)
    return;
    pDialog = new Dialog();
    pDialog->setModal(true);
    pDialog->show();
    }

    void MainWindow::closeModified(const QString &str)
    {
    Q_UNUSED(str)
    if(pDialog != NULL)
    {
    pDialog->hide();
    }
    QFile file(FILE_PATH);
    file.remove();
    pDialog = NULL;
    }
    @

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

    int main(int argc, char argv[])
    {
    QApplication a(argc, argv);
    QFileSystemWatcher fileWatcher;
    fileWatcher.addPath(FILE_PATH);
    QStringList fileList = fileWatcher.files();
    Q_FOREACH(QString file, fileList)
    qDebug() << "File name " << file;
    MainWindow
    mc = new MainWindow;
    QObject::connect(&fileWatcher, SIGNAL(fileChanged(QString)), mc, SLOT(closeModified(QString)));
    mc->show();

    return a.exec(&#41;;
    

    }
    @

    1 Reply Last reply
    0
    • A Offline
      A Offline
      Asperamanca
      wrote on last edited by
      #2

      In "afterFileHasBeenFound", you could check whether the dialog is already displayed. Just remember the current pointer to the open dialog (or NULL if no dialog is currently open) as a class member.

      1 Reply Last reply
      0
      • S Offline
        S Offline
        Sai Kamat
        wrote on last edited by
        #3

        Hi Asperamanca, could you please elaborate? I don't know how to check existence of dialog using pointer.

        I tried this:
        @
        void MainWindow::afterFileHasBeenFound()
        {
        pDialog = new Dialog();
        pDialog->setModal(true);
        pDialog->show();

        if(pDialog->isVisible())
            pDialog->hide();
        

        }
        @

        Bt it's not appropriate. Please suggest.
        [quote author="Asperamanca" date="1394778692"]In "afterFileHasBeenFound", you could check whether the dialog is already displayed. Just remember the current pointer to the open dialog (or NULL if no dialog is currently open) as a class member.[/quote]

        1 Reply Last reply
        0
        • A Offline
          A Offline
          Asperamanca
          wrote on last edited by
          #4

          You probably enter the method afterFileHasBeenFound multiple times.

          Make pDialog a module variable of your main window. Then you can do:

          @
          MainWindow::MainWindow(QWidget *parent) :
          QMainWindow(parent),
          ui(new Ui::MainWindow),
          m_pDialog(0)
          ...

          void MainWindow::afterFileHasBeenFound()
          {
          if (m_pDialog != NULL)
          return;
          m_pDialog = new Dialog();
          m_pDialog->setModal(true);
          m_pDialog->show();
          delete m_pDialog;
          m_pDialog = 0;
          }@

          Edit: You can optimize by not deleting the dialog after use. Then you need to check both that the dialog exists, and that it is visible before showing a new one.

          1 Reply Last reply
          0
          • S Offline
            S Offline
            Sai Kamat
            wrote on last edited by
            #5

            Hi, Thanks for that! i hope u have tried running the program appending ur suggestions?
            Coz i did and faced the following issues:-

            1. @
              m_pDialog(0);
              @
              gives compilation error :C2064: term does not evaluate to a function taking 1 arguments

            2. qDialog is never displayed.

            3. the FileSystemWatcher module does not work. What I mean is, if I modify the .txt file, the qDialog should disappear. But after appending your code, the program hangs.

            What would u suggest?

            [quote author="Asperamanca" date="1394780918"]You probably enter the method afterFileHasBeenFound multiple times.

            Make pDialog a module variable of your main window. Then you can do:

            @
            MainWindow::MainWindow(QWidget *parent) :
            QMainWindow(parent),
            ui(new Ui::MainWindow),
            m_pDialog(0)
            ...

            void MainWindow::afterFileHasBeenFound()
            {
            if (m_pDialog != NULL)
            return;
            m_pDialog = new Dialog();
            m_pDialog->setModal(true);
            m_pDialog->show();
            delete m_pDialog;
            m_pDialog = 0;
            }@

            Edit: You can optimize by not deleting the dialog after use. Then you need to check both that the dialog exists, and that it is visible before showing a new one.
            [/quote]

            1 Reply Last reply
            0
            • A Offline
              A Offline
              Asperamanca
              wrote on last edited by
              #6

              You have created a module variable called m_pDialog in the main window?

              1 Reply Last reply
              0
              • S Offline
                S Offline
                Sai Kamat
                wrote on last edited by
                #7

                Yes i have, i also made modifications in the code, and updated the question. Can you please help?

                [quote author="Asperamanca" date="1394789578"]You have created a module variable called m_pDialog in the main window?[/quote]

                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