Qt: QDialog - activate windows taskbar icon (C++) - c++

I just can not understand one seemingly basic thing.
If we want to show our custom dialog, we can do smth like this:
OurDialog * dlg = new OurDialog; // (this);
dlg->setAttribute(Qt::WA_DeleteOnClose);
dlg->show();
dlg->activateWindow();
hide(); // hide MainWindow
Depending on giving a parent to constructor or not we can make taskbar icon visible or not.
But how to make the icon not only visible but also active?
Moreover, if we move the string
hide(); // hide MainWindow
before
dlg->show();
The taskbar icon will be active, but in this case we'll get a "blinking effect" on showing the dialog.
So is there any possibility to hide MainWindow, show Dialog and make the taskbar icon active?
Thank you!

Related

What happens to a Qt::Popup widget when I click outside of it?

I want to understand Qt:: Popup window slightly deeper. I have custom QWidget and set it as popup window setWindowFlag(Qt::Popup); . It works perfect, but what happens when we click outside of it? Is it just hiding? What signals emitted?

Ghost Window/Bitmap lingers after closing window - MFC SDI

Environment: MFC frame based SDI application
Problem: After closing modal dialog boxes, the bitmap stays displayed/the window beneath doesn't repaint.
Dialog creation:
PortSettings Dlg;
Dlg.DoModal();
I've tried explicitly setting OnCancel() for the dialog class,
void PortSettings::OnCancel()
{CDialog::OnCancel();}
tried calling RedrawWindow from the parent window and the Dialog OnCancel.
This issue happens for all the dialog boxes, and other modal windows that open (Like a file browser) I assume because I am using MFC I've somehow interfered with the underlying Window Proc but I'm not sure how to investigate or what to try.
Solution was multifaceted:
Dialog Window Properties affect the image lingering - in my case, it was necessary to change the dialog frame to "thin" style.
After the Dlg.DoModal() call, call ParentWnd->RedrawWindow(). In the Parent Window's OnPaint, I added a fillSolidRect to repaint the background white. This section is controlled with Boolean logic to only repaint immediately after a dialog closes to avoid flickering.

I want to show menu when cursor hovers over pushbutton and close menu when cursor is moved away

I am using Qt on Ubuntu.
I have a menu on QPushButton. I want to show menu when cursor hovers over the QPushButton and close menu when cursor is moved away.
Showing a popup menu on "hover" event seems to violate the user experience, as users expect to see the popup when they click the button. This is called a menu button. If you really want to use hover event, you may subclass the QPushButton class and use its respective events. However if you would like to use a menu button, you can try this:
QMenu *menu = new QMenu();
QAction *testAction = new QAction("test menu item", this);
menu->addAction(testAction);
button->setMenu(menu);
Documentation on QPushButton::setMenu.
You have to implement your owen QPushButton. Let's start by checking the MouseMoveEvent to handle when the mouse hover the widget.
To check if the cursos pos is inside your widget:
void CustomPushButton::mousePressEvent(QMouseEvent *e) {
const QRect widgetRect = ui->followersWidget->geometry();
const QPoint mousePos = ui->followersWidget->mapFromGlobal(QCursor::pos()); // or e->pos()
if (widgetRect.contains(mousePos)) {
// Mouse inside the widget, lets show the menu
} else {
// Mouse outside the widget, if the menu is open, close it.
}
QWidget::mousePressEvent(e);
}
To show/hide the menu you could use the QMenu::popup(..), from Qt Doc:
Displays the menu so that the action atAction will be at the specified global position p. To translate a widget's local coordinates into global coordinates, use QWidget::mapToGlobal().

how to forbid dialog show in the windows taskbar in Qt programming when I open a new dialog?

how to forbid dialog show in the windows taskbar in Qt programming when I open a new dialog?
when I open a new a dialog in the main window.In the windows taskbar always show a new dialog task,I don't want it shows. how to do it?
Quoting from QDialog:
A dialog is always a top-level widget, but if it has a parent, its default location is centered on top of the parent's top-level widget (if it is not top-level itself). It will also share the parent's taskbar entry.
Use the overload of the QWidget::setParent() function to change the ownership of a QDialog widget.
If Dialog's parent is set to None then it opens another window, and i have two entries in taskbar.
If Dialog's parent is set to mainwindow - the dialog is shown inside the main window and share taskbar entry.

Hide main MFC window while modal dialog is active?

I have a native C++ MFC app. It has a main window based on CWnd, and user action can create a modal dialog. While the dialog is active, I want the main window to disappear, the dialog to be visible, and the main window's icon to remain in the task bar.
How can I accomplish this?
If I hide the main window (ShowWindow(SW_HIDE)), the task bar icon disappears. If I minimize the main window (SW_MINIMIZE), the icon remains. However, since the dialog is owned by the main window, this also hides the dialog.
After the dialog is created, clicking on the task bar icon makes the dialog visible. Naturally, I do not want to require the user to do this.
Even if I insert ShowWindow(SW_SHOW) in the dialog's OnInit handler, the dialog remains not visible. Spy++ shows that its visible bit is set, though. Same is true if I add SetWindowActive to OnInit.
I am not interested in changing the UI design. While the dialog is active, the user interacts only with it, and is not interested in anything in the main window. Therefore, the main window should disappear.
Using Windows VS2005 under WinXP32.
Well, in the block of code where you create the dialog and show it modal, you can do whatever you want to the main window of your app (show/hide) as long as you make the desktop window the parent of your dialog. Usually, the constructor for CDialog and derivatives takes a default argument of NULL for the parent window in which the framework ends up substituting AfxGetMainWnd(). Instead pass CWnd::GetDesktopWindow() as the parent of your dialog and then you should probably be able to hide your main window. However, you still might have a problem with the taskbar--but I'll let someone else give hints since I know nothing offhand about it.
In OnInitDialog, add following codes
//Set windows size zero, the windows disappear.
MoveWindow(0,0,0,0);
//If you want it invisible on taskbar. add following codes.
DWORD dwStyle = GetWindowLong(GetSafeHwnd(), GWL_EXSTYLE);
dwStyle &=~WS_EX_APPWINDOW;
dwStyle |= WS_EX_TOOLWINDOW;
SetWindowLong(GetSafeHwnd(), GWL_EXSTYLE, dwStyle);
You're fighting the OS. A modal dialog, by definition, disables but does not hide the "main" (parent) window. If you wanted another window, make a second one, but don't tell the OS to treat it as a modal dialog over the first window.
Perhaps you can resize the main window to a really small size and always keep it behind the modal dialog.