使QSlider在QSystemTrayIcon中工作

时间:2013-05-03 18:12:03

标签: qt tray qslider

我正在尝试在Ubuntu 13.04中创建一个亮度控制应用程序。我们的想法是制作一个托盘图标和滑块,在按下图标时显示。问题是托盘菜单只包含一个空项目。这是代码块,它工作但不正确(仅在按下空菜单项时显示滑块):

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
}

怎么了?

1 个答案:

答案 0 :(得分:1)

您的问题很容易解决。首先,您不需要为菜单项添加操作来触发显示/隐藏QSlider。您只需为整个托盘图标添加触发器和插槽。

connect(tray, SIGNAL(activated(QSystemTrayIcon::ActivationReason)),
  this, SLOT(trayIconClicked(QSystemTrayIcon::ActivationReason)));

而不仅仅是添加插槽:

void VolumeQWindow::trayIconClicked(QSystemTrayIcon::ActivationReason reason)
{
  if (reason == QSystemTrayIcon::Trigger)
  {
    show();
  }
}

就是这样。