I'd like to create a tray icon for my application for showing the main window on clicking on it after the former was minimized.
Here's the implementation:
TrayIcon.h:
class TrayIcon_t : public QSystemTrayIcon {
Q_OBJECT
public:
TrayIcon_t();
};
TrayIcon.cpp:
TrayIcon_t::TrayIcon_t() {
setIcon(QIcon(":/icons/tray.ico"));
}
Main.cpp (part only, there's no more code related to the tray):
TrayIcon_t *tray = new TrayIcon_t;
QObject::connect(tray, SIGNAL(activated(QSystemTrayIcon::ActivationReason)), &MainWindow, SLOT(show()));
tray->show();
While on LXDE it works just fine, on MATE desktop it mostly opens a small menu (I think) containing no items and doesn't emit the signal required by the main window to be shown. See this picture.
Do you think I've encountered a bug in Qt 5.7.0?
Turns out that this is a bug. Reported it and became accepted at:
https://bugreports.qt.io/browse/QTBUG-55717
Related
I'm trying to create a C++ Widget Application in QT with multiple windows where you can return to the mainwindow by clicking a Pushbutton.
I'm a complete noobie so I try to follow YouTube tutorial and for opening windows by pushbuttons I watched this one (minute 8:00): https://youtu.be/tP70B-pdTH0
It works when opening secondary windows from the main one but if I try to do the same from a secondary windows to the mainwindow it doesn't. It appears an error "cannot initialize object parameter of type 'Widget' with an expression of type 'MainWindow'"
in the source file I wrote:
void Crediti::on_pushButton_clicked()
{
close();
mainwindow = new MainWindow(this);
mainwindow->show();
}
mainwindow->show(); is the incriminated part
I also included mainwindow in the header of the secondary window and specified the class
MainWindow *mainwindow
in the header so It recognizes mainwindow initially in the source.
I'm doubting if doing this thing is possible at all, and if not so how can I make a pushbutton that, when clicked, can redirect me to the mainwindow?
Please I need this for a school application, thanks
So here you're creating a new main window each time you click on the button. From your description that's not the behaviour you want. I understand you have an application with a main window and other secondary windows and want to bring up the main window when clicking on the button, assuming the main window still exists somewhere and hasn't been deleted.
What I would try is to find the main window when hitting the push button and show / raise it, something along the line of:
#include <QApplication>
#include "MainWindow.h" // Adapt that one to you main window header
// ... some code of your secondary window
void SecondaryWindow::on_pushButton_clicked()
{
for(auto widget : QApplication::topLevelWidgets())
{
// This will return a nullptr if the widget is not the main window
auto mainWindow = dynamic_cast<MainWindow*>(widget);
// skip if not the main window
if(!mainWindow)
continue;
// Show it if hidden
if(mainWindow->isHidden())
mainWindow->show();
// raise it, as in bring it forward, over all other windows
mainWindow->raise();
}
// eventually close the current window if that's what you want
close();
// if you close it and don't need it any more you might also want to delete it
deleteLater();
}
Note that this function won't do anything if the main window has been deleted in the meantime, which might be the case if you closed it and the Qt::WA_DeleteOnClose attribute is set.
Hope that helps.
I'm new at Qt. I've created small application and I created second page help.cpp. On MainWindow.cpp I have a button, that switches to help.cpp page.
Function which switches to "help" page:
void MainWindow::on_box1button_clicked()
{
helpwindow = new help(this);
helpwindow->show();
}
This code works properly.
On the "help" page I've got a QButton, which will switch back to mainwindow.cpp. How Can I code that button to actually make this action?
If your intention by "switching" is hiding one window and showing another one, so you can simply pass a reference of the main window to your help window and there when you want to switch back, you can hide/close itself and show the main window.
MainWindow (this code is fine)
helpwindow = new help(this);
helpwindow->show();
HelpWindow
When you want to switch back to the main window, you can do this:
// Hide the HelpWindow itself
// or this->close()
this->hide()
// Show the MainWindow (i.e. the parent window)
QWidget *parent = this->parentWidget();
parent->show();
Since you are creating a new help(this); on mainwindow it is better to close the help window
Use
this->close();
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!
Single-clicking an application icon in the Dock launches that application or, if the application is already open, switches you to that application and brings forward all open windows in that application.
In my Qt app, I do not see this behavior. I have a dock shortcut, I minimize the app on dock, and if I click the minimized instance the app is restored.
If I click the application icon in the Dock (dock shortcut), nothing happens.
(I can right-click, and "Show All Windows" - but that is not similar to the behavior of all other apps)
I tried to place the code from this answer: https://stackoverflow.com/a/15363738/1217150
Placing qDebug statements,
if(test) qDebug("registered");
in the constructor, and
void MyApplictionClass::onClickOnDock()
{
qDebug("dock clicked");
}
On start-up I get both messages... but after minimizing, or any time after start-up, there is no reaction to dock shortcut being clicked or double clicked.
Full code insertion:
MyApplicationClass.h:
#include <objc/objc.h>
#include <objc/message.h>
class MyApplicationClass: public QApplication
{
....
bool dockClickHandler(id self, SEL _cmd, ...);
void onClickOnDock();
};
MyApplicationClass.cpp:
MyApplicationClass::MyApplicationClass()
{
....
objc_object* cls = objc_getClass("NSApplication");
SEL sharedApplication = sel_registerName("sharedApplication");
objc_object* appInst = objc_msgSend(cls, sharedApplication);
if(appInst != NULL)
{
objc_object* delegate = objc_msgSend(appInst, sel_registerName("delegate"));
objc_object* delClass = objc_msgSend(delegate, sel_registerName("class"));
const char* tst = class_getName(delClass->isa);
bool test = class_addMethod((objc_class*)delClass,
sel_registerName("applicationShouldHandleReopen::hasVisibleWindows:"),
(IMP)dockClickHandler(appInst, sharedApplication), "B#:");
if(!test) qDebug("not registered");
else qDebug("registered");
}
.....
}
bool MyApplicationClass::dockClickHandler(id self, SEL _cmd, ...)
{
Q_UNUSED(self)
Q_UNUSED(_cmd)
onClickOnDock();
return true;
}
void MyApplicationClass::onClickOnDock()
{
qDebug("dock clicked");
}
Perhaps I did something wrong ? There are a few items I don't understand... The referred post had dockClickHandler not part of the class... I just don't know where they have it... And I had to give it arguments when I registered it, otherwise it refused to build. Still, it seems to do... something... just not the right thing. In that code, I do not see where the app will react to clicking the dock.
Is this the correct approach ? Or how can I get my app to restore when its dock shortcut is clicked ?
(OSX 10.6-10.9, Qt 4.8)
If you are refering to this: QT on OS X, how to detect clicking the app Dock Icon, the onClickOnDock should not be a member of the class.
You CAN put it as a normal function inside the MyApplicationClass.cpp file.
There is a hack at least for Qt 5.9.1, when you can implement onClickOnDock() without platform specific code at all. See https://stackoverflow.com/a/46488514/8695355
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.