Qt-jambi-interest Archive, January 2008
Popup Modal Dialog on showEvent() ?
Message 1 in thread
Hi all,
I have another question: I have a QDialog mainWindow
that is my main window. When I click a button, the
mainWindow will be hidden and replaced with a new
QDialog mainWindow2. I want that, when I show
mainWindow2, a new QDialog popupDlg to show as modal.
For this, here is what I am doing in mainWindow2:
class MainWindow2 extends QDialog {
protected void showEvent( QShowEvent ev ) {
super.showEvent(ev);
QDialog popupDlg = new QDialog();
popupDlg.exec();
}
}
The problem is, that the content of the main window
will still be the content of mainWindow, and will get
replaced with mainWindow2 contents only after popupDlg
is closed... Is there any way to popup a new modal
dialog when a widget is shown, but only AFTER it
completely shows on the screen ?
Thanks,
Danny
____________________________________________________________________________________
Looking for last minute shopping deals?
Find them fast with Yahoo! Search. http://tools.search.yahoo.com/newsearch/category.php?category=shopping
Message 2 in thread
Hi!
Can you create and show your popupDlg in the mainWindow button clicked
method and just set mainWindow2 as parent?
Danny Sporea wrote:
> Hi all,
>
> I have another question: I have a QDialog mainWindow
> that is my main window. When I click a button, the
> mainWindow will be hidden and replaced with a new
> QDialog mainWindow2. I want that, when I show
> mainWindow2, a new QDialog popupDlg to show as modal.
>
> For this, here is what I am doing in mainWindow2:
>
> class MainWindow2 extends QDialog {
>
> protected void showEvent( QShowEvent ev ) {
>
> super.showEvent(ev);
>
> QDialog popupDlg = new QDialog();
>
> popupDlg.exec();
>
> }
>
> }
>
> The problem is, that the content of the main window
> will still be the content of mainWindow, and will get
> replaced with mainWindow2 contents only after popupDlg
> is closed... Is there any way to popup a new modal
> dialog when a widget is shown, but only AFTER it
> completely shows on the screen ?
>
> Thanks,
> Danny
>
>
> ____________________________________________________________________________________
> Looking for last minute shopping deals?
> Find them fast with Yahoo! Search. http://tools.search.yahoo.com/newsearch/category.php?category=shopping
>
>
Message 3 in thread
Hi, Danny.
Danny Sporea wrote:
> Hi all,
>
> I have another question: I have a QDialog mainWindow
> that is my main window. When I click a button, the
> mainWindow will be hidden and replaced with a new
> QDialog mainWindow2. I want that, when I show
> mainWindow2, a new QDialog popupDlg to show as modal.
>
[...]
> The problem is, that the content of the main window
> will still be the content of mainWindow, and will get
> replaced with mainWindow2 contents only after popupDlg
> is closed... Is there any way to popup a new modal
> dialog when a widget is shown, but only AFTER it
> completely shows on the screen ?
>
>
The problem is that the show event is called before the window is shown,
and as long as the modal event loop is running, your mainWindow2 is not
receiving any more events.
One solution would be to listen for events that are delivered after the
window has been shown, but I think the better way in your case is to
post a timer event to yourself using a "zero timer". A zero timer has a
timeout of zero, thus it should fire as soon as possible, and it is
delivered after all pending system events are cleared out from the event
queue. There are several ways of doing this, but I think the easiest
would be to use QTimer.singleShot():
http://doc.trolltech.com/qtjambi-4.3.3_01/com/trolltech/qt/core/QTimer.html#singleShot(int,%20com.trolltech.qt.core.QObject,%20java.lang.String)
I've attached a small example to this mail to illustrate what I mean.
Note that in the show event handler, I am checking that the event was
not spontaneous. Spontaneous show events are posted to the window
whenever it has been hidden and is shown by the system (e.g. when the
user minimizes the window and then restores it.)
-- Eskil
import com.trolltech.qt.core.*;
import com.trolltech.qt.gui.*;
public class ModalDialog extends QDialog {
public ModalDialog(QWidget parent) {
super(parent);
QHBoxLayout layout = new QHBoxLayout(this);
layout.addWidget(new QLabel("Main window 2"));
}
@Override
protected void showEvent(QShowEvent e) {
if (!e.spontaneous())
QTimer.singleShot(0, this, "showDialog()");
}
public void showDialog() {
QDialog dialog = new QDialog();
QHBoxLayout layout = new QHBoxLayout(dialog);
layout.addWidget(new QPushButton("Hello World"));
dialog.exec();
}
public static void main(String args[]) {
QApplication.initialize(args);
ModalDialog d = new ModalDialog(null);
d.show();
QApplication.exec();
}
}
Message 4 in thread
Eskil,
Thanks for your answer. I made it work another way:
TaskDetailsDialog tdd = new TaskDetailsDialog();
tdd.setWindowModality(
Qt.WindowModality.ApplicationModal );
tdd.show();
This seem to be working when using show() instead of
exec() and setting the modality as ApplicationModal...
Do you see any problems with this approach ?
Thanks,
Danny
--- Eskil Abrahamsen Blomfeldt
<eblomfel@xxxxxxxxxxxxx> wrote:
> Hi, Danny.
>
> Danny Sporea wrote:
> > Hi all,
> >
> > I have another question: I have a QDialog
> mainWindow
> > that is my main window. When I click a button, the
> > mainWindow will be hidden and replaced with a new
> > QDialog mainWindow2. I want that, when I show
> > mainWindow2, a new QDialog popupDlg to show as
> modal.
> >
> [...]
> > The problem is, that the content of the main
> window
> > will still be the content of mainWindow, and will
> get
> > replaced with mainWindow2 contents only after
> popupDlg
> > is closed... Is there any way to popup a new modal
> > dialog when a widget is shown, but only AFTER it
> > completely shows on the screen ?
> >
> >
>
>
> The problem is that the show event is called before
> the window is shown,
> and as long as the modal event loop is running, your
> mainWindow2 is not
> receiving any more events.
>
> One solution would be to listen for events that are
> delivered after the
> window has been shown, but I think the better way in
> your case is to
> post a timer event to yourself using a "zero timer".
> A zero timer has a
> timeout of zero, thus it should fire as soon as
> possible, and it is
> delivered after all pending system events are
> cleared out from the event
> queue. There are several ways of doing this, but I
> think the easiest
> would be to use QTimer.singleShot():
>
>
>
http://doc.trolltech.com/qtjambi-4.3.3_01/com/trolltech/qt/core/QTimer.html#singleShot(int,%20com.trolltech.qt.core.QObject,%20java.lang.String)
>
> I've attached a small example to this mail to
> illustrate what I mean.
> Note that in the show event handler, I am checking
> that the event was
> not spontaneous. Spontaneous show events are posted
> to the window
> whenever it has been hidden and is shown by the
> system (e.g. when the
> user minimizes the window and then restores it.)
>
>
> -- Eskil
>
>
> >
>
> import com.trolltech.qt.core.*;
> import com.trolltech.qt.gui.*;
>
> public class ModalDialog extends QDialog {
>
> public ModalDialog(QWidget parent) {
> super(parent);
>
> QHBoxLayout layout = new QHBoxLayout(this);
> layout.addWidget(new QLabel("Main window
> 2"));
> }
>
> @Override
> protected void showEvent(QShowEvent e) {
> if (!e.spontaneous())
> QTimer.singleShot(0, this,
> "showDialog()");
> }
>
> public void showDialog() {
> QDialog dialog = new QDialog();
>
> QHBoxLayout layout = new
> QHBoxLayout(dialog);
> layout.addWidget(new QPushButton("Hello
> World"));
>
> dialog.exec();
> }
>
> public static void main(String args[]) {
> QApplication.initialize(args);
>
> ModalDialog d = new ModalDialog(null);
> d.show();
>
> QApplication.exec();
> }
>
> }
>
____________________________________________________________________________________
Be a better friend, newshound, and
know-it-all with Yahoo! Mobile. Try it now. http://mobile.yahoo.com/;_ylt=Ahu06i62sR8HDtDypao8Wcj9tAcJ
Message 5 in thread
Hi, Danny.
Danny Sporea wrote:
> TaskDetailsDialog tdd = new TaskDetailsDialog();
> tdd.setWindowModality(
> Qt.WindowModality.ApplicationModal );
>
> tdd.show();
>
> This seem to be working when using show() instead of
> exec() and setting the modality as ApplicationModal...
> Do you see any problems with this approach ?
>
No, I believe that should be fine. Using show(), you will however not be
able to retrieve the result code of the dialog directly, since the call
returns before the dialog has been closed, but you can still use e.g.
its signals to get this information if you need it.
-- Eskil