How to disable scroll function when Ctrl is pressed in QMainWindow - c++

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);
}
...
}

Related

How to return keyboard control back to an QT window when clicked on it

I am having this weird bug.
I have a class that inherits both QObject and QGraphicsRectItem.
I am having it do something if a key like space bar is pressed. However if I click outside a window, like say my desktop for example, the key no longer works. I tried clicking on the QT window to make the key responsive again but no luck.
Here is my code
Header:
class rectangle : public QObject, public QGraphicsRectItem{
public:
rectangle();
void keyPressEvent(QKeyEvent *event);
}
cpp file:
rectangle::rectangle(){}
void rectangle::keyPressEvent(QKeyEvent *event){
if (event->key() == Qt::Key_Space){
// do something
qDebug() << "Key pressed";
}
}
Normally, clicking on a program would give it back the input control but it isn't the case here. What do I need to do to make the keyPressedEvent worked again after I clicked on another application or just an empty area of my desktop?
Your keyPressEvent does only work if QGraphicsRectItem has focus. This means, if you switch back to your Qt application the focus of QGraphicsRectItem may get lost and another widget (i. e QLabel, MainWindow, etc.) will have focus. Then the keyPressEvent of QGraphicsRectItem will not trigger. You can use installEventFilter to install your keyPressEvent filter on multiple widgets, or you simply use an application wide shortcut.
QShortcut *shortcut = new QShortcut(QKeySequence(Qt::Key_Space), this);
connect(shortcut, &QShortcut::activated, [](){ qDebug() << "Space pressed"; });

Qt event when widget goes out of sight

Is there a way in Qt to handle situation when any widget of Window goes out of sight. I.e if a widget was in tab control and user have changed active tab, or if user just scrolls and widget goes offscreen, and also when it goes back on screen.
Is that possible to add some code to this two events?
Best if this can be done globally...
Is there a way in Qt to handle situation when any widget of Window goes out of sight. I.e if a widget was in tab control and user have changed active tab, or if user just scrolls and widget goes offscreen, and also when it goes back on screen.
The way the question asked makes one think that the widget show-hide-expose state changes need to be handled:
bool MyWidget::event(QEvent* pEvent)
{
if (pEvent->type() == QEvent::Show)
{
// event "shown"
}
else if (pEvent->type() == QEvent::Hide)
{
// event "hidden"
}
else if (pEvent->type() == QEvent::Expose)
{
// event "exposure changed"
// deal with QExposeEvent and evaluate the exposed region
// QExposeEvent* pExposeEvent = reinterpret_cast<QExposeEvent*>(pEvent);
}
return QWidget::event(pEvent);
}
Best if this can be done globally...
Event filter at the top level widget may solve that. Or you can override event() function for the top level widget but finding what exact widget was affected is another thing.
Refer to QExposeEvent description.

QSystemTrayIcon handle left and right click separately?

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();

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!

QLineEdit Subclassing

I have Subclassed QlineEdit. Since I needed a specific operation on mousePress Event.
Now, as soon as I click the mouse on the subclassed QlineEdit (I call it CustomLineEdit). I am trying to pop up a virtual keyboard. A new Window panel shows up, but the virtual keyboard is not showing up. I have checked for the correctness of the virtual keyboard by using it on other classes.
This is my code:
void CustomLineEdit::mousePressEvent(QMouseEvent *s)
{
qDebug() << " Custom Line EDit is kicking";
myKeyBoard->show(this); // once created keyboard object, use this method to switch between windows
myKeyBoard->move(0, 0 + myKeyBoard->height() + 175); // to move keyboard
}
Btw, the virtual keyboard was downloaded from this link.
Any suggestions will highly be appreciated.
myKeyBoard->show(this);
This show method needs a parent to drawing into this widget. If a widget is into another one, it cannot paint out of parent's work area. Maybe, if you change the line in this way, virtual keyboard will show:
myKeyBoard->show( parent( ) );