Trolltech Home | Qt-interest Home | Recent Threads | All Threads | Author | Date
All threads index page 2

Qt-interest Archive, January 2008
changeEvent, Minimize, Tray, QSystemTrayIcon


Message 1 in thread

I try minimize my application with "Minimize" button - [_], not close - 
[X]. And have problem with hide() main window. Window don't hide and i 
think this becouse hide() call inside Event handler.
I found solution like this: QTimer::singleShot(0, this, SLOT(hide()));
But i know what QTimer create other thread and this NOT good for simple
operation like this. Qt have methods/signals/slots who call before 
events/methods, after methods/events ?


This code:

#include "minimizetray.h"

MinimizeTray::MinimizeTray(QWidget *parent)
     : QMainWindow(parent)
{
	ui.setupUi(this);
	tray = new QSystemTrayIcon(QIcon(":/icons/apply.png"), this);
	connect(this, SIGNAL(signalPlaceToTray()),this, SLOT(slotPlaceToTray()));
}

MinimizeTray::~MinimizeTray()
{

}
void MinimizeTray::changeEvent(QEvent *event)
{
     if (event->type() == QEvent::WindowStateChange)
     {
         if (isMinimized())
         {
             emit signalPlaceToTray();
             event->ignore();
             return;
         }
     }
QMainWindow::changeEvent(event);
}
void MinimizeTray::slotPlaceToTray()
{
     tray->show();
     hide();
}

JPEG image

JPEG image

begin:vcard
fn:SABROG
n:;SABROG
email;internet:sabrog@xxxxxxxxx
x-mozilla-html:FALSE
version:2.1
end:vcard


Message 2 in thread

> I found solution like this: QTimer::singleShot(0, this, SLOT(hide()));
> But i know what QTimer create other thread and this NOT good 
> for simple operation like this. 

QTimer doesn't create a new thread. 
All it does is create a QSingleShotTimer object, which is automatically
discarded once the timerEvent has occured.

Cheers,
Peter

--
 [ signature omitted ] 

Message 3 in thread

But QSingleShotTimer call startTimer and inside i found this:

int QObject::startTimer(int interval)
{
     Q_D(QObject);

     if (interval < 0) {
         qWarning("QObject::startTimer: QTimer cannot have a negative 
interval");
         return 0;
     }

     d->pendTimer = true;                                // set timer flag

     if (!d->threadData->eventDispatcher) {
         qWarning("QObject::startTimer: QTimer can only be used with 
threads started with QThread");
         return 0;
     }
     return d->threadData->eventDispatcher->registerTimer(interval, this);
}


> QTimer doesn't create a new thread. 
> All it does is create a QSingleShotTimer object, which is automatically
> discarded once the timerEvent has occured.

begin:vcard
begin:vcard
fn:SABROG
n:;SABROG
email;internet:sabrog@xxxxxxxxx
x-mozilla-html:FALSE
version:2.1
end:vcard


Message 4 in thread

> qWarning("QObject::startTimer: QTimer can only be used with 
> threads started with QThread");

This seems to be the warning message for the following sentence in the 
docs: "In multithreaded applications, you can use QTimer in any thread 
that has an event loop."

Greetings,
Christoph

--
 [ signature omitted ] 

Message 5 in thread

Yes. But this doesn't mean it creates a thread.
Rest assured, only your main thread (every application has at least 1 thread) will be used :) 

What the error message means is that if you manually create more threads and want to use QTimer in them, they have to be QThreads, not custom ones.

Cheers,
Peter

> -----Ursprüngliche Nachricht-----
> Von: SABROG [mailto:sabrog@xxxxxxxxx] 
> Gesendet: Donnerstag, 10. Januar 2008 16:39
> An: qt-interest@xxxxxxxxxxxxx
> Betreff: Re: AW: changeEvent, Minimize, Tray, QSystemTrayIcon
> 
> But QSingleShotTimer call startTimer and inside i found this:
> 
>      if (!d->threadData->eventDispatcher) {
>          qWarning("QObject::startTimer: QTimer can only be used with 
> threads started with QThread");

--
 [ signature omitted ] 

Message 6 in thread

I found what singleShot call PostEvent, so this mean, what i can 
manually generate PostEvent with my custom event and write inside event 
handler hide() and tray->show() ? Events have queue and my event must 
generate after exit changeEvent. Or i dont understand something...

--
 [ signature omitted ] 

Message 7 in thread

Hello,

take a look at closeEvent().

void MainWindow::closeEvent(QCloseEvent* event)
{
   if ( allowclose )
     QMainWindow::closeEvent(event);
   else
   {
     event->ignore(); // Window does not close

     // TODO: Show our tray icon if not already done

     hide(); // Hides the window away
   }
}

Greetings,
Christoph

SABROG schrieb:
> I try minimize my application with "Minimize" button - [_], not close - 
> [X]. And have problem with hide() main window. Window don't hide and i 
> think this becouse hide() call inside Event handler.
> I found solution like this: QTimer::singleShot(0, this, SLOT(hide()));
> But i know what QTimer create other thread and this NOT good for simple
> operation like this. Qt have methods/signals/slots who call before 
> events/methods, after methods/events ?
> 
> 
> This code:
> 
> #include "minimizetray.h"
> 
> MinimizeTray::MinimizeTray(QWidget *parent)
>     : QMainWindow(parent)
> {
>     ui.setupUi(this);
>     tray = new QSystemTrayIcon(QIcon(":/icons/apply.png"), this);
>     connect(this, SIGNAL(signalPlaceToTray()),this, 
> SLOT(slotPlaceToTray()));
> }
> 
> MinimizeTray::~MinimizeTray()
> {
> 
> }
> void MinimizeTray::changeEvent(QEvent *event)
> {
>     if (event->type() == QEvent::WindowStateChange)
>     {
>         if (isMinimized())
>         {
>             emit signalPlaceToTray();
>             event->ignore();
>             return;
>         }
>     }
> QMainWindow::changeEvent(event);
> }
> void MinimizeTray::slotPlaceToTray()
> {
>     tray->show();
>     hide();
> }
> 
> ------------------------------------------------------------------------
> 

--
 [ signature omitted ] 

Message 8 in thread

On Thursday 10 January 2008 16:16:51 SABROG wrote:
> 	connect(this, SIGNAL(signalPlaceToTray()),this, SLOT(slotPlaceToTray()));

You might try connecting this as Qt::QueuedConnection so it gets processed 
after you return to event loop.

-- 
 [ signature omitted ] 

Attachment: signature.asc
Description: This is a digitally signed message part.


Message 9 in thread

Thanks, this is work :) !

Vladimir Pouzanov пишет:
> On Thursday 10 January 2008 16:16:51 SABROG wrote:
>> 	connect(this, SIGNAL(signalPlaceToTray()),this, SLOT(slotPlaceToTray()));
> 
> You might try connecting this as Qt::QueuedConnection so it gets processed 
> after you return to event loop.
> 

begin:vcard
begin:vcard
fn:SABROG
n:;SABROG
email;internet:sabrog@xxxxxxxxx
x-mozilla-html:FALSE
version:2.1
end:vcard