PDA

View Full Version : alternative to QMessageBox



incapacitant
19th May 2006, 17:51
In my application I indicate to the user that an action has been performed by using QMessageBox.
But the drawback is that it stops the appliction and requires the user to click OK to proceed.
I would like to display a window with a message for say 1 second and then remove it and proceed with the processing.

Can you point me to the docs ? or give me a hint ?

wysota
19th May 2006, 18:03
Make a dialog with a timer and connect its timeout() signal to dialogs accept() slot.

incapacitant
22nd May 2006, 10:34
So I tried this short piece of code but I won't quit.
The window with my button is displayed ans sits there forever.



#include "mainwindow.h"
#include <QTimer>

MainWindow::MainWindow() : QMainWindow()
{

//Make a dialog with a timer and
//connect its timeout() signal to dialogs accept() slot.

pbClose = new QPushButton( this );
pbClose->resize( QSize( 10, 10 ) );
QTimer *timer = new QTimer(this);
connect(timer, SIGNAL(timeout()), this, SLOT(quit()));
connect( pbClose, SIGNAL( timeout() ), this, SLOT( quit() ) );
timer->start(100);

}

Connecting to accept() fails too.



QTimer::singleShot(200, this, SLOT(quit()));

is no better

jpn
22nd May 2006, 10:41
Use QDialog. QMainWindow does not have a slot quit().

incapacitant
22nd May 2006, 10:53
My knowledge does not goes as far as subclassing for the time being.
Where can I read something about subclassing Accelerated C++ does not even have it in the index !

In any case my application has otherthings to do after the timeout.
So I rewrote it as this, and it works :



#include "mainwindow.h"
#include <QTimer>

MainWindow::MainWindow() : QMainWindow()
{

//Make a dialog with a timer and
//connect its timeout() signal to dialogs accept() slot.

pbClose = new QPushButton( this );
pbClose->resize( QSize( 10, 10 ) );
QTimer *timer = new QTimer(this);
QTimer::singleShot(2000, this, SLOT(stop()));
timer->start();

}

void MainWindow::stop()
{
close();
}

wysota
22nd May 2006, 12:01
MainWindow is a subclass of QMainWindow, so you are subclassing :) Subclassing is a process of implementing a class which inherits some other classes.

incapacitant
22nd May 2006, 17:01
I'm trying to integrate to the successfull short code above in my application as :



sendMsg sendMsg_dlg( this );
if ( sendMsg_dlg.exec() )
{
QTimer *timer = new QTimer(this);
QTimer::singleShot(2000, this, SLOT(closeSendMsg()));
timer->start();
}





void ApplicationWindow::closeSendMsg()
{
close();
finaliseEnvoi();
}


The sendMsg dialog is displayed but the closeSendMsg slot is not called and the dialog sits there.

I was told : Make a dialog with a timer and connect its timeout() signal to dialogs accept() slot.

I must be doing something wrong but despite several tries cannot fix it.

wysota
22nd May 2006, 18:01
Try something like this:


class WaitDialog : public QDialog {
public:
WaitDialog(uint interval, QWidget *p=0) : QDialog(p){
QVBoxLayout *l = new QVBoxLayout(this);
QLabel *lab = new QLabel("Please wait");
l->addWidget(lab);
setLayout(l);
timer.setSingleShot(true);
timer.setInterval(interval);
connect(&timer, SIGNAL(timeout()), this, SLOT(accept()));
}
void exec(){
timer.start();
QDialog::exec();
}
private:
QTimer timer;
};

incapacitant
22nd May 2006, 18:35
Ok, I am thick, I can't even call it from my code.



sendMsg sendMsg_dlg( this );
if ( sendMsg_dlg.exec() )
{
WaitDialog::WaitDialog(200,sendMsg);
}

This does not compile:
447 C:\root\dev\Qt\sms data\sms\menu.cpp expected primary-expression before '(' token
447 C:\root\dev\Qt\sms data\sms\menu.cpp expected primary-expression before ')' token

All I am doing is trying random combination, without really understanding all to call your class. Hopeless

But elsewhere I managed to set an icon to my application without asking on the forum. lol

wysota
22nd May 2006, 19:07
You should learn instead of shooting random. I won't give you the exact code, let it be your homework for today to think and learn how to do it. I'll give you a hint though. You use this dialog like any other dialog. The only difference is the time to wait parameter which you have to use while constructing the dialog.

incapacitant
22nd May 2006, 19:51
ok, I already understood the way you think about all this and I think it is 100% sane.
I will search and try... thanks

wysota
22nd May 2006, 21:05
You don't have to search. Just look at the code you already have and *think*.

incapacitant
23rd May 2006, 07:21
You say that I should consider this dialog as any other one, so I think I found out how to instantiate it as it compiles :) :



waitDialog_dlg = new WaitDialog::WaitDialog(200, this);


The trouble now is to invoke the dialog :


if ( waitDialog_dlg.exec() )
{
}


compile error :
445 C:\root\dev\Qt\sms data\sms\menu.cpp `exec' has not been declared

With :


if ( WaitDialog::waitDialog_dlg.exec() )
{
}


compile error :
445 C:\root\dev\Qt\sms data\sms\menu.cpp `waitDialog_dlg' is not a member of `WaitDialog'

waitDialog_dlg is a member of my ApplicationWindow class like my other dialogs.


class ApplicationWindow: public QMainWindow
{
Q_OBJECT

public:
ApplicationWindow();
~ApplicationWindow();

// dialog
Destinataires *Destinataires_dlg;
Archives *Archives_dlg;
sendMsg *sendMsg_dlg;
WaitDialog *waitDialog_dlg;



I tried to declare the dialog in the WaitDialog class but things get worse...

wysota
23rd May 2006, 09:49
Oh man... I hate to do this, but I suggest you learn some C++ before trying with Qt.

Check out our links section, especially the one with C++ links (http://www.qtcentre.org/index.php?option=com_weblinks&catid=13&Itemid=23). You should start with Thinking in C++ and Correct C++ Tutorial.

incapacitant
23rd May 2006, 11:02
ok, I will read. Thanks.

zlatko
23rd May 2006, 11:51
waitDialog_dlg = new WaitDialog(200, this);

incapacitant
23rd May 2006, 13:35
zlatko:
Thanks I have done that bit to instantiate the dialog.
The trouble remains how to call, display and start the dialog and its timer :crying:

wysota
23rd May 2006, 13:38
Please learn it yourself.

incapacitant
23rd May 2006, 15:21
I knew zlatko was going to get trouble. Yes, master. I have downloaded the books.

incapacitant
24th May 2006, 12:24
wysota: It is true that I have to learn, and I thank you for your attitude.

For info, your class had a bug, the correct code is :


class WaitDialog : public QDialog {
public:
WaitDialog(uint interval, QWidget *p=0) : QDialog(p)
{
QVBoxLayout *l = new QVBoxLayout(this);
QLabel *lab = new QLabel("Please wait");
l->addWidget(lab);
setLayout(l);
timer.setSingleShot(true);
timer.setInterval(interval);
connect(&timer, SIGNAL(timeout()), this, SLOT(accept()));
}

int exec() // rather than void
{
timer.start();
return QDialog::exec(); // needed return
}

private:
QTimer timer;
};


Then to call it :


WaitDialog waitDialog_dlg( 2000, this );
if ( waitDialog_dlg.exec() )
{
}


I learn slowly, but I am stubborn and I will get there. ;)

wysota
24th May 2006, 14:49
A little bug. void should be ok too. It just didn't return a value, which is (by the way) not important here.