Reopening / Rexecuting QDialog is slow



  • Hello,

    I noticed a strange behaviour with QDialogs. When reopening, respectively reexecuting (exec()) an instance of a QDialog it takes quite a while for the Dialog to popup (nearly a second i would guess). The first time the QDialog is used it pops up instantaneously.

    Here is a minimal working example:

    mainwindow.h :
    @
    #ifndef MAINWINDOW_H
    #define MAINWINDOW_H

    #include <QMainWindow>
    #include <QDialog>

    class MainWindow : public QMainWindow
    {
    Q_OBJECT

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

    #endif // MAINWINDOW_H
    @

    mainwindow.cpp :

    @
    #include "mainwindow.h"
    #include <QMenuBar>
    #include <QMenu>
    #include <QAction>
    #include <QDialog>

    MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent)
    {
    QMenu *fileMenu = menuBar()->addMenu("File");
    QAction *action = fileMenu->addAction("New File");
    QDialog *dialog = new QDialog(this);

    connect(action, SIGNAL(triggered()), dialog, SLOT(exec&#40;&#41;&#41;);
    

    }
    @

    I also tried open() and show() as the slot, but all show the same behaviour. Is this a known problem / bug or am I doing something wrong here? Maybe it's a problem with my machine? Or are QDialogs
    simply not supposed to be reused / -opened?

    PS: I'm using Qt 5.2.1 with kubuntu 14.04.

    /edit: OK, back home i used the same minimal example i gave in this thread and everything works fine. But my home machine is far more powerful than at my office. Besides that I'm using QT version 5.1.1 at home, so it might be an indication that this behavior is really a bug in version 5.2.1. Maybe someone could confirm this?

    Best regards

    BaBene



  • No issue with usage like this. Tried in Win and Ubuntu. Did not give delay. Can you try your example with QPushButton and connecting clicked slot with same dialog ? Just checking is it just issue with Menu or some thing as simple pushbutton slot connection as well.



  • OK, back to the office i tried what you suggested. I simply used a QPushButton and connected the clicked() signal to the exec() slot of the QDialog, and noticed the same behavior ... So i guess it's an issue with the QDialog itself.



  • You can try to measure the delay

    Create a Dialog class and pass QTime to its constructor then before running exec() start timer in MainWindow and print out the elapsed time in showEvent of Dialog class. Something like this

    @
    class Dialog : public QDialog
    {
    Q_OBJECT

    public:
        explicit Dialog(QTime& elapsed, QWidget *parent = 0);
        ~Dialog();
    
    protected:
        void showEvent(QShowEvent *);
    
    private:
        Ui::Dialog *ui;
        QTime& elapsed;
    

    };
    @

    @
    Dialog::Dialog(QTime& elapsed, QWidget *parent)
    : QDialog(parent)
    , ui(new Ui::Dialog)
    , elapsed(elapsed)
    {
    ui->setupUi(this);
    }

    void Dialog::showEvent(QShowEvent *)
    {
    qDebug() << "Elapsed:" << elapsed.elapsed();
    }
    @

    @
    class MainWindow : public QMainWindow
    {
    // all other things
    private slots:
    void openDialog();

    private:
        Dialog* dialog;
        QTime elapsed;
    

    };
    @

    @
    MainWindow::MainWindow(QWidget* parent)
    : QMainWindow(parent)
    {
    dialog = new Dialog(elapsed, this);

    connect(action, SIGNAL(triggered()), this, SLOT(openDialog()));
    

    }

    void MainWindow::openDialog()
    {
    elapsed.restart();
    dialog->exec();
    }

    @



  • I'll give it a try tomorrow. However, I think that I'll switch to the latest Qt version when I'm back at office, since I switched to the latest version at home, too ;) I hope that this behavior will end when using the latest version (but i might try a few other versions if you want me to). I will keep you updated!

    Thanks so far!


Log in to reply
 

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