PDA

View Full Version : Ensuring a Widget Is Hidden



wswartzendruber
13th May 2009, 23:09
I'm making a little screenshot program that runs in the system tray. You click the tray icon, and a menu pops up. You click on "Copy to Clipboard" and the screenshot is sent to the clipboard. The only problem is, that menu is still visible. Is there anyway to work around this that doesn't involve a disgusting hack (timer, threading, etc...)?

wysota
14th May 2009, 00:32
You can call hide() on the menu and afterwards call QCoreApplication::sendPostedEvents() on the menu. Afterwards you should be able to make the screenshot.

wswartzendruber
14th May 2009, 19:07
It's still not working. I've uploaded the source files to http://www.wswartzendruber.net/uploads/projects/screenshooter/. The file in question is systemtrayiconmanager.cpp at line 110.

I've tried:


QCoreApplication::processEvents()

QCoreApplication::flush()

while (application->hasPendingEvents())
QCoreApplication::processEvents(QEventLoop::AllEve nts);

while (application->hasPendingEvents())
QCoreApplication::flush();

fifth
14th May 2009, 19:13
Should really use code tags above :p

wysota
14th May 2009, 20:44
But where did you put those lines? Did you do as I told you to?

wswartzendruber
14th May 2009, 22:09
But where did you put those lines? Did you do as I told you to?
I did try send posted events. I put all that stuff right above the aformentioned line in the aformentioned file.

wysota
14th May 2009, 22:46
What exactly did you put there?

wswartzendruber
14th May 2009, 23:14
What exactly did you put there?

void SystemTrayIconManager::copyToClipboardTriggered()
{
// I put those lines included in one of my previous posts here.

cout << "Screenshot taken." << endl;

application->clipboard()->setPixmap(getScreenshotPixmap());
}
I didn't put them in all at the same time, but one at a time.

wysota
14th May 2009, 23:27
But did you hide the menu before putting some event processing related code?

This works fine for me:

#include <QtGui>

class ShotButton : public QPushButton {
Q_OBJECT
public:
ShotButton() : QPushButton(){
setText("Take a shot");
connect(this, SIGNAL(clicked()), this, SLOT(makeAShot()));
}
private slots:
void makeAShot(){
hide();
QCoreApplication::sendPostedEvents();
QPixmap px = QPixmap::grabWindow(QApplication::desktop()->winId());
show();
px.save("/tmp/pixmap.jpg");
}
};

#include "main.moc"

int main(int argc, char **argv){
QApplication app(argc, argv);
ShotButton button;
button.show();
return app.exec();
}

wswartzendruber
19th May 2009, 10:46
Hmmm... Works on Windows, but not X11.

wysota
19th May 2009, 14:33
I'm working on X11, so it works there as well...