Reputation: 21
I'm trying to create an application for brightness control in Ubuntu 13.04. The idea is to make a tray icon and slider which appears as the icon is pressed. The problem is that the tray menu contains just an empty item. Here is the block of code, which works but not properly (it shows the slider only when the empty menu item is pressed):
cntrl::cntrl(QWidget *parent):
QWidget(parent)
{
value = 2500;
slider = new QSlider (Qt::Horizontal,this);
slider->setValue(2500);
slider -> setRange(0,maxBrightness);
slider -> setSingleStep(50);
slider->setPageStep(50);
tray = new QSystemTrayIcon (this);
menu = new QMenu (this);
act = new QWidgetAction (this);
act->setDefaultWidget(slider);
menu->addAction(act);
tray->setContextMenu(menu);
tray->setIcon(QIcon(":/brightness2.png"));
tray->show();
connect (slider,SIGNAL(valueChanged(int)),this,SLOT(changeBrightness(int)));
connect(act,SIGNAL(triggered()),menu,SLOT(show())); //trying to make it work somehow
}
What's wrong ?
Upvotes: 2
Views: 393
Reputation: 824
Your problem is easy to solve. At first of all you do not need to add an action for the menu item to trigger showing/hiding of the QSlider. All you need is to add the trigger and slot for a whole tray icon.
connect(tray, SIGNAL(activated(QSystemTrayIcon::ActivationReason)),
this, SLOT(trayIconClicked(QSystemTrayIcon::ActivationReason)));
And than just add the slot:
void VolumeQWindow::trayIconClicked(QSystemTrayIcon::ActivationReason reason)
{
if (reason == QSystemTrayIcon::Trigger)
{
show();
}
}
That's it.
Upvotes: 1