A.J
A.J

Reputation: 735

QDialog show is does not happen immediately

I have a sever client application and in the beginning client loads data from server. I have a dialog showing status of getting data from server (has progress bar). But when I call the function the dialog appears with no contents in it with white background and suddenly changes to completed status.

void SystemScreen::loadServerData()
{
    qDebug() << Q_FUNC_INFO << "Invoked";

    if (NULL != mpDataManagerDlg)
    {
        qDebug() << Q_FUNC_INFO << "show progres screen";
        mpDataManagerDlg->showScreen();
    }

    loadData();

    qDebug() << Q_FUNC_INFO << "Exits";
}

void SystemScreen::loadData()
{
    qDebug() << Q_FUNC_INFO << "Invoked";

    if (NULL != mpDataManager)
    {
        mpDataManager->loadDataFromServer();
    }

    qDebug() << Q_FUNC_INFO << "Exits";
}

I feel that dialog is displayed only after loadData() function is completed. Is there any alternative to do this?

I used a timer to start

QTimer::singleShot(100, this, SLOT(loadData()));

But then I have some trouble in getting data. ie data is empty if I read suddenly.

EDIT:

void DataManagerDialog::setDefault()
{
    qDebug() << Q_FUNC_INFO << "Invoked";
    setProgressBar(0);
    setProgressBarColor(false);

    ui->deptFailButton->hide();
    ui->deptOkButton->hide();
    ui->deptLabel->setStyleSheet("color:gray");
    ui->subGroupFailButton->hide();
    ui->subGroupOkButton->hide();
    ui->subGroupLabel->setStyleSheet("color:gray");
    ui->itemFailButton->hide();
    ui->itemOkButton->hide();
    ui->itemLabel->setStyleSheet("color:gray");
    ui->salesBtnFailButton->hide();
    ui->salesBtnOkButton->hide();
    ui->salesBtnLabel->setStyleSheet("color:gray");

    qDebug() << Q_FUNC_INFO << "Exits";
}

void DataManagerDialog::alignCenter()
{
    qDebug() << Q_FUNC_INFO << "Invoked";

    QWidget *par = parentWidget();
    if (par)
    {
        int x = width()/2;
        int y = height()/2;

        QPoint mid(mapToGlobal(QPoint(x, y)));

        int px = par->width()/2;
        int py = par->height()/2;

        QPoint parMid(mapToGlobal(QPoint(px, py)));

        move(parMid.x()-mid.x(), parMid.y()-mid.y());
    }

    qDebug() << Q_FUNC_INFO << "Exits";
}

void DataManagerDialog::showScreen()
{
    setDefault();
    alignCenter();
    show();
}

Upvotes: 0

Views: 1001

Answers (1)

CHW
CHW

Reputation: 2664

You probably do not enter the event loop.

Try to call QCoreApplication::processEvents() from time to time in mpDataManager->loadDataFromServer() to update the GUI.

From the processEvents documentation :

You can call this function occasionally when your program is busy performing a long operation (e.g. copying a file).

Edit added after getting feedback from the comments

A better approach would be to send signals in your loadDataFromServer() method with the status information and have a slot listen to the signal and update the GUI.

Here a prototype illustrating the idea :

void mpDataManagerDlg::loadDataFromServer() {
    while(true) {
        // do some work
        int progress = // some value
        emit updateDialogSignal(progress);
    }
}

// in your dialog class
public slots:
void DataManagerDialog::updateDialog(int progress) {
    // update gui
}

More about signals and slots can be found here.

Upvotes: 2

Related Questions