V
V
Vas3K2011-05-17 15:00:48
linux
Vas3K, 2011-05-17 15:00:48

QSystemTrayIcon drag-and-drop how?

I would like to process the drag-n-grop file on the system tray icon in a Qt application.

Google gave:
Here they say it is not necessary and impossible, however, when they link to droplr, they calm down and retreat.
Here it is more interesting. It is advised to make a transparent widget on top of the tray icon. However, I'm not sure how it will behave when changing the focus and changing the position of the tray, so beware of such a crutch.

Maybe somehow you can play with inheritance, or who has an implementation already? The main requirement would be to support at least two operating systems: Linux (GNOME, KDE) and MacOS. Well, Windows will not be superfluous.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Vitaly Petrov, 2011-06-06
@Vas3K

Since QSystemTrayIcon still inherits from QObject, it still cannot process events intended for QWidget. I think the option with a transparent widget is quite suitable. It is not possible to receive any events from the icon, such as show / hide, but you can still get its geometry through

QRect QSystemTrayIcon::geometry () const

Therefore, you can inherit from QSystemTrayIcon, redefine
void QObject::timerEvent ( QTimerEvent * event ) [virtual protected]

and periodically interrogate the geometry of the icon to move our transparent widget...
well, something like:
#include <QSystemTrayIcon>

 class MySystemTrayIcon : public QSystemTrayIcon, QWidget
 {
     Q_OBJECT

 public:
     MySystemTrayIcon(QObject *parent = 0);

 protected:
     void timerEvent(QTimerEvent *event);
 };

 MySystemTrayIcon::MySystemTrayIcon(QObject *parent)
     : QSystemTrayIcon(parent), QWidget(0)
 {
     startTimer(1000);   // 1-second timer
 }

 void MySystemTrayIcon::timerEvent(QTimerEvent *event)
 {
     QWidget::setGeometry (QSystemTrayIcon::geometry());
 }

Not a fountain, of course, but it’s quite workable ... How can I hang drag-and-drop this widget, I think you yourself know ...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question