QSystemTrayIcon handle left and right click separately? - c++

I have a class called StatusIcon that extends QSystemTrayIcon. I want to set it up so right click opens the context menu and left click opens a window.
Currently the default behaviour seems to be both left and right click open the context menu.
I need to find a way to block the left click and run my own code instead.
From the documentation it looks like this could be achieved using eventFilter I have setup an eventFilter method on StatusIcon with a qdebug in it. This doesn't get called with a right or left click.
I installed it using a line of code like:
this->installEventFilter(this)
I'm wondering if its not working as its already overriding the virtual method as I've got QSystemTrayIcon as the super class.
Does anyone know why eventFilter is not being called?
Could anyone think of a way to achieve this functionality?

You don't need eventFilter. For left click:
//somewhere in constructor
connect(tray,SIGNAL(activated(QSystemTrayIcon::ActivationReason)),this,SLOT(showHide(QSystemTrayIcon::ActivationReason)));
//...
void MainWindow::showHide(QSystemTrayIcon::ActivationReason r)
{
if (r == QSystemTrayIcon::Trigger)
{
if (!this->isVisible()) {
this->show();
} else {
this->hide();
}
}
}
For menu, just use setContextMenu():
QMenu *menu = new QMenu(this);
//for example
menu->addAction(showHideAct);
menu->addAction(optionAct);
menu->addAction(infoAct);
menu->addSeparator();
menu->addAction(quitAct);
tray = new QSystemTrayIcon();
tray->setIcon(QIcon("://data/tray.png"));
tray->setContextMenu(menu);//important method for you
tray->show();

Related

Call button click function from grandchild

I'm creating my first C++ wxWidgets application. I'm trying to create some kind of split button where the options are displayed in a grid. I have a custom button class which, when right-clicked on, opens a custom wxPopupTransientWindow that contains other buttons.
When I click on the buttons in the popup, I want to simulate a left click on the main button. I'm trying to achieve this through events, but I'm kinda confused.
void expandButton::mouseReleased(wxMouseEvent& evt)
{
if (pressed) {
pressed = false;
paintNow();
wxWindow* mBtn = this->GetGrandParent();
mBtn->SetLabel(this->GetLabel());
mBtn->Refresh();
wxCommandEvent event(wxEVT_BUTTON);
event.SetId(GetId());
event.SetEventObject(mBtn);
mBtn-> //make it process the event somehow?
wxPopupTransientWindow* popup = wxDynamicCast(this->GetParent(), wxPopupTransientWindow);
popup->Dismiss();
}
}
What is the best way to do this?
You should do mBtn->ProcessWindowEvent() which is a shorter synonym for mBtn->GetEventHandler()->ProcessEvent() already mentioned in the comments.
Note that, generally speaking, you're not supposed to create wxEVT_BUTTON events from your own code. In this particular case and with current (and all past) version(s) of wxWidgets it will work, but a cleaner, and guaranteed to also work with the future versions, solution would be define your own custom event and generate it instead.

How to disable minimizing by taskbar icon click

I've stumbled across very strange behaviour during work on my program.
I've written custom changeEvent class, which allows me to hide program to SysTray on minimizing.
But when i double click on taskbar app icon, the function goes crazy. It creates 2 to 4 systray icons and on requesting window show again, it just shows main window borders without any content inside.
Here's my changeEvent code:
void MainWindow::changeEvent(QEvent *e) {
QMainWindow::changeEvent(e);
if(e->type()==QEvent::WindowStateChange)
if(isMinimized()) {
trayIcon=new QSystemTrayIcon(QIcon(":/icon/itime.ico"));
connect(trayIcon,SIGNAL(activated(QSystemTrayIcon::ActivationReason)),this,SLOT(on_show(QSystemTrayIcon::ActivationReason)));
QAction *showAction=new QAction("Pokaż",trayIcon);
connect(showAction,SIGNAL(triggered()),this,SLOT(on_show()));
QMenu *trayIconMenu=new QMenu;
trayIconMenu->addAction(showAction);
trayIcon->setContextMenu(trayIconMenu);
trayIcon->show();
this->hide();
}
}
on_show(QSystemTrayIcon::ActivatioReason) SLOT:
void MainWindow::on_show(QSystemTrayIcon::ActivationReason reason) {
if(reason) {
if(reason!=QSystemTrayIcon::DoubleClick)
return;
}
if(this->isMinimized()) {
this->raise();
this->showNormal();
this->setWindowState(Qt::WindowActive);
trayIcon->hide();
}
}
on_show() SLOT is just the same besides that first if.
Soo, I would like to know whether there is any way to disable minimizing of window by taskbar icon click.
If there's none, then maybe you have any ideas what can go wrong in here when doubleclicking on icon in taskbar?
Thanks for help!
I've managed to work around that problem by overloading closeEvent function and leaving alone changeEvent function.
So, I'm using boolean flag to distinct between closing of program by menu item and by clicking "X" button and the rest stays just the same, as posted in my earlier post with one change.
I've moved this whole block of code to window constructor in order to prevent multiple creation of trayIcon, as pointed out by Nicolas.
trayIcon=new QSystemTrayIcon(QIcon(":/icon/itime.ico"));
connect(trayIcon,SIGNAL(activated(QSystemTrayIcon::ActivationReason)),this,SLOT(on_show(QSystemTrayIcon::ActivationReason)));
QAction *showAction=new QAction("Pokaż",trayIcon);
connect(showAction,SIGNAL(triggered()),this,SLOT(on_show()));
QMenu *trayIconMenu=new QMenu;
trayIconMenu->addAction(showAction);
trayIcon->setContextMenu(trayIconMenu);
Thanks for your help!

How to disable scroll function when Ctrl is pressed in QMainWindow

I currently works on QT for my project. I implemented a MainWindow class which inherited from QMainWindow.
In MainWindow, I handled mouse wheel event like this:
void MainWindow::wheelEvent ( QWheelEvent * event )
{
if (event->modifiers().testFlag(Qt::ControlModifier)) {
if (event->delta() > 0) {
zoomInAct();
}
else if(event->delta()<0){
zoomOutAct();
}
}
}
The problem is: when I press CONTROL KEY and Wheel the mouse, the scroll bar alway scroll to top or bottom before reach my wheelEvent function. Would you please help to allow zoom-in/out when press control and wheel the mouse? (Not scroll the scroll bar)
Sorry for my bad english.
Looking at your current implementation here you have not specified event->accept() if you have added your own functionality and you don't want to propagate the event further to the parent. Try adding event->accept() in the if conditions where you have added your logic.
And try adding debug statement to test whether the event is reaching here or someone else is handling the event. By someone else I mean some other child widget. Some description of the UI and what widget is to be zoomed in would be helpful to further investigate the problem.
Make sure you read about the event system in Qt. Docs available here
Acctualy, there is a child widget that handle the wheel event first (default event handle is scroll the scrollbar).
Solution: override wheelevent in child widget to send it to parent widget (MainWindow) when the control key is pressed.
class WorkArea: public QWidget {
...
virtual void wheelEvent(QWheelEvent *event)
{
if(event->modifiers().testFlag(Qt::ControlModifier))
MainWindow::wheelEvent(event);
else
QWidget::wheelEvent(event);
}
...
}

How to create a MFC CSliderCtrl?

I created a new MFC project with MS VS 2010 using their wizard (I chose single document if it matters). Then, I created a dialog box and from the toolbox I dragged a new slider. Using VS's wizard (right click on dialog-->add class) I created a class for my dialog called MyDialog. Next, I used VS's wizard to add a variable to to slider.
So now, in my MyDialog class I have a CSliderCtrl and I can't get it to work.
I tried to use SetRange() and\or SetPos() in OnInitDialog() but they crash to program and throws an exception.
If I create a new CSliderCtrl in OnInitDialog() and set it's range with SetRange() like so, it does works.
BOOL MyDialog::OnInitDialog()
{
CSliderCtrl *TrackBar = new CSliderCtrl;
TrackBar->Create(WS_CHILD | WS_VISIBLE,CRect(20, 20, 60, 280),this, IDC_SLIDER1);
int min,max;
TrackBar->GetRange(min,max);
TrackBar->SetPos(10);
return TRUE;
}
But this doesn't work:
BOOL MyDialog::OnInitDialog()
{
m_mySlider.SetRange(1,100); //sending true\false doesn't matter
return TRUE;
}
Can anyone help me please?
From the code you shown, you missed this important line,
CDialogEx::OnInitDialog();
You should add SetRange and SetPos calls after the above line.

Qt UI testing: How to simulate a click on a QMenuBar item using QTest?

I am trying to simulate a mouse click on a QMenu item from a QMenuBar, for example clicking on "Save As" QAction using the QTestLib framework.
I am triyng this under Windows XP 32 bit and Qt 5.0.2.
Any Ideas?
Probably this question is not relevant for question owner, but I suppose it could be helpful for others.
Unlike QToolBar, QMenu doesn't have method widgetForAction. I found a simple workaround for this case. Try popup menu with QTest::mouseClick if nothing happens try to use QTest::keyClick(..., first_char_in_menu_tite, Qt::AltModifier). To simulate action execution you can navigate with Qt::Key_Down to action until you reach it and then press Qt::Key_Enter. I suppose following code can help you to understand
QMenu *menu = getMenu(mainWindow, menuName);
if (menu != nullptr) {
QTest::keyClick(mainWindow, menu->title().at(1).toLatin1(), Qt::AltModifier);
}
QList<QAction *> actions = menu->actions();
foreach (QAction *action, actions) {
if (action->objectName() == actionName) {
QTest::keyClick(menu, Qt::Key_Enter);
break;
}
QTest::qWait(1000);
QTest::keyClick(menu, Qt::Key_Down);
}
You should use the QTest::mouseClick function. It will simulate the click on any QWidget. I have found that trying to click on a QMenu that causes a blocking call will not work with a unit test.