0

I have a QDialog which I open like this:

void MyModule::openDialog() {
    this->dialog->open();
    printf("Hello World");
}

The dialog opens just fine, but unfortunately "Hello World" isn't printed when calling openDialog() - even after closing the dialog. However, when I open another completely unrelated file dialog afterwards, the message is printed out.

What is causing the dialog to block until another dialog is opened? The same thing happens when I'm using this->dialog->exec(); or this->dialog->show();.

It might be worth noting that MyModule::openDialog() is a slot which is connected to the click-event of a button.

Any ideas?

André Hoffmann
  • 3,505
  • 1
  • 25
  • 39

2 Answers2

4

It depends on the OS but generally printf() doesn't play nicely with GUIs.

On windows for instance nothing would appear, on unix you could do fprintf(stdout, ) and then fflush(stdout) or fprintf(stderr, ) which isn't buffered

If this is just a debug function, take a look at qDebug() - it's what it's there for.

Martin Beckett
  • 94,801
  • 28
  • 188
  • 263
  • 2
    printf() works fine with GUIs, even under Windows... but if you want to actually see stdout output under Windows you have to do AllocConsole(); freopen("conout$", w, stdout); at the top of main(). Then a console window containing your output will appear when the program runs. – Jeremy Friesner Feb 12 '11 at 06:13
  • @Jeremy - that was pretty much my defn of 'not playing nicely' – Martin Beckett Feb 12 '11 at 15:40
1

stdout is buffered. Calling printf("Hello world"); will not output anything until the output buffer is full or (sometimes) a newline is printed. Try calling printf("Hello World\n"); instead or printf("Hello World\n"); fflush(stdout);

Chris
  • 11
  • 1