Toggle Qt::WA_TranslucentBackground - c++

I've been searching around but haven't found an answer that helps me.
As the Title says i want to toggle the attribute "Qt::WA_TranslucentBackground" on/off.
I need WA_TranslucentBackground but some users of my app reported that this doesn't work in OBS(Open Broadcaster Software) so i have to make a seperate version without the TranslucentBackground.
My Code:
void MainWindow::action_widgetMode(){
if(displayOBS ==0){
this->setAttribute(Qt::WA_TranslucentBackground,true);
}else{
this->setAttribute(Qt::WA_TranslucentBackground,false);
}
this->setWindowFlags(Qt::Widget | Qt::FramelessWindowHint | Qt::WindowStaysOnTopHint | Qt::X11BypassWindowManagerHint);
this->activateWindow();
this->setFocus();
this->show();
}
I am calling this function once upon start. And additionally on a checkbox click where i want to toggle it on/off. This code does work when i restart my application, but i want it to be immediate after the checkbox is checked/unchecked.

Related

Disable QTRadioButton click [duplicate]

Any good way to make a checkbox readonly, but also not grayed-out (hardly visible).
I have used setEnabled(bool) which works, but the checkbox then is grayed-out and hardly readable
I can react on a toggle signal and reset the state. But I would need a kind of flag to determine if the box is read-only and then reset the check state, means I need to create my own CheckBox class.
setCheckable does not work either, it does not allow me to set a checked state at all:
cb = this->ui->cb_RealWorld->isCheckable();
this->ui->cb_RealWorld->setCheckable(true);
this->ui->cb_RealWorld->setChecked(someValue);
this->ui->cb_RealWorld->setCheckable(cb);
So the best thing I have is to use enable/disable and accept the grayed out style.
------- Edit -------
Following the stylesheet examples I was hoping I could set the style of a disabled checkbox like the one of an enabled. Failed so far to do so. More specific: Changing the icon like in the examples does not work for me, maybe because I am using Windows and the icons are not available under the path as in the examples.
PS: Related, but no answer here
Disabling a QCheckbox in a tricky way
Qt - How to disable QCheckBox while retaining checked state?
Following the below my code:
this->ui->cb_RealWorld->setAttribute(Qt::WA_TransparentForMouseEvents);
this->ui->cb_RealWorld->setFocusPolicy(Qt::NoFocus);
This is Devopia's solution as a function:
void SetReadOnly(QCheckBox* checkBox, bool readOnly)
{
checkBox->setAttribute(Qt::WA_TransparentForMouseEvents, readOnly);
checkBox->setFocusPolicy(readOnly ? Qt::NoFocus : Qt::StrongFocus);
}
On Windows, remember to #include "windows.h" and set flags as follows:
this->ui->cb_RealWorld->setWindowFlags(this->ui->cb_RealWorld->windowFlags() | Qt::WindowTransparentForInput);
Inherit from QCheckBox and override nextCheckState method https://doc.qt.io/qt-5/qcheckbox.html#nextCheckState do the trick.
void ReadOnlyCheckBox::nextCheckState()
{
}

Enable maximize button in QWizard

I have a Windows application that is built on QWizard (which inherits from QDialog). It must have a working maximization button.
By default maximization button is not even visible. i have set it to show, using:
auto flags = windowFlags();
flags ^= Qt::WindowContextHelpButtonHint;
flags |= Qt::WindowMinMaxButtonsHint;
setWindowFlags(flags);
However, it shows up disabled (grayed out, non-responding).
How can i enable it?
This works for me:
setWindowFlags(windowFlags() | Qt::CustomizeWindowHint |
Qt::WindowMinimizeButtonHint |
Qt::WindowMaximizeButtonHint |
Qt::WindowCloseButtonHint);
According to the documentation, you have to use the Qt::CustomizeWindowHint to be able to change the individual hints on the min/max buttons.
Someone here says this solved his problem:
setWindowFlags(Qt::Window);
I believe that you'll get better results creating your own dialog, but if you really wanna do it, one way is use window styles (Windows only, not cross-plataform).
Wizard class example:
class wizard : public QWizard
{
public:
wizard() {}
~wizard() {}
protected:
bool event(QEvent *event)
{
#ifdef Q_OS_WIN /*Make this code Windows OS only*/
if (event->type() == QEvent::WinIdChange)
{
HWND hwnd = (HWND)winId();
LONG lStyle = GetWindowLong(hwnd, GWL_STYLE);
lStyle |= (WS_MINIMIZEBOX | WS_MAXIMIZEBOX); /*Enable minimize and maximize*/
SetWindowLong(hwnd, GWL_STYLE, lStyle);
}
#endif
return QWizard::event(event);
}
};
I have this:
QWizard *wizard = new QWizard(this, Qt::CustomizeWindowHint | Qt::WindowMaximizeButtonHint | Qt::Window);
wizard->setSizeGripEnabled(true);
Running Windows 10 on my dev-box, Qt 5.5.1, working for me.
One of my pages is a big QTableWidget that ends up being like an Excel sheet of some sorts (a big page to verify and edit-in-place a lot of data). Making the window resizable and let the user maximize it if they want makes it much easier to work with, instead of having to constantly scroll in a small dialog.
Normally you would say: If you need such a big window, it probably shouldn't be in a QWizard. But in this case it's really the middle of a workflow thing. A big 'verify, edit-if-needed and continue' page so it would be weird to stop the QWizard before and then having to start another one after or something.

Setting Windows Flag on a QDialog hides the form

I currently have a form that inherits from QDialog.
Now in order to hide the ? icon on the form I am doing something like this in the constructor.
foo::foo(QWidget *parent): QDialog(parent)
{
.....
this->setWindowFlags(Qt::WindowTitleHint);
}
The problem with this is that the Dialog does not show up. If I ommit the flags line it shows up. I am using QT 5.1.1
To answer the question, here the solution:
setWindowFlags(windowFlags() & ~Qt::WindowContextHelpButtonHint);
If you want the minimize and maximize options, do the following:
setWindowFlags(windowFlags() & ~Qt::WindowContextHelpButtonHint) | Qt::WindowMaximizeButtonHint | Qt::WindowMinimizeButtonHint);
Eventually you want to call
this->setWindowFlags(this->windowFlags() | Qt::WindowTitleHint);
To get this to work on linux I had to use both options described above:
setFixedSize(width(), height());
setWindowFlags(Qt::Drawer);
The result is a dialog with only a close button.

Notification window in Mac. With or without Qt

Qt project on Mac OS X. I need to show notification window on top without stealing a focus from any active application.
Here the widget constructor part:
setWindowFlags(
Qt::FramelessWindowHint |
Qt::WindowSystemMenuHint |
Qt::Tool |
Qt::WindowStaysOnTopHint
);
setAttribute(Qt::WA_TranslucentBackground);
Qt::WA_ShowWithoutActivating doesn't affect anything.
Is there a way to do that? I'm ready to implement the native Carbon/Cocoa solution there, but Qt is preferred.
Or maybe I'm wrong in Mac philosophy and I should notify user in a kind another manner?
Update Growl doesn't support editor line in its notifications, does it?
I did it!
#ifdef Q_OS_MAC
#include <Carbon/Carbon.h>
#endif
NotifyWindow::NotifyWindow() : QWidget(0 /* This zero is the first point */) {
setWindowFlags(
#ifdef Q_OS_MAC
Qt::SubWindow | // This type flag is the second point
#else
Qt::Tool |
#endif
Qt::FramelessWindowHint |
Qt::WindowSystemMenuHint |
Qt::WindowStaysOnTopHint
);
setAttribute(Qt::WA_TranslucentBackground);
// And this conditional block is the third point
#ifdef Q_OS_MAC
winId(); // This call creates the OS window ID itself.
// qt_mac_window_for() doesn't
int setAttr[] = {
kHIWindowBitDoesNotHide, // Shows window even when app is hidden
kHIWindowBitDoesNotCycle, // Not sure if required, but not bad
kHIWindowBitNoShadow, // Keep this if you have your own design
// with cross-platform drawn shadows
0 };
int clearAttr[] = { 0 };
HIWindowChangeAttributes(qt_mac_window_for(this), setAttr, clearAttr);
#endif
}
We get almost the same nice behavior as in Windows:
It does not stole focus on show. (Two weeks of searching over the Internet)
The controls there handle the first user click, while other windows need an extra click to activate.
When the window is being activated, the other windows of the same application, do not bubble up to the front.
And a small problem remains, but at least it has a simple workaround. Or even could be left.
Pavel,
Have you heard of Growl? Growl is a VERY impressive notification app that you can bundle and use with your application. Adium - a popular instant messaging app for OS X - uses it for all notifications.
http://growl.info/
You could implement Growl. http://growl.info/documentation/developer/
try this on mac:
setWindowFlags(windowFlags() | Qt::FramelessWindowHint | Qt::WindowStaysOnTopHint);
setAttribute(Qt::WA_TranslucentBackground);
setAttribute(Qt::WA_AlwaysStackOnTop);
Ive just tested these flags
Qt::FramelessWindowHint |Qt::WindowSystemMenuHint |Qt::WindowStaysOnTopHint
And
setFocusPolicy(Qt::NoFocus);
setAttribute(Qt::WA_ShowWithoutActivating,true);
Without Cocoa or Carbon code for window flags/masks.
And notifyWindow works like on Windows or Linux.

How can I disable and gray the top level menu item using MFC

I have a dialog application in which I want to have clickable menu items at the top of the dialog. These items do not show a drop down menu but actually run the associated commands.
I did this by setting Popup=False in the dialogs properties and assigning a message-id but my problem is not having the ability to disable the item properly when it makes no sense for the item to be clickable (depending on internal state stored in the dialog)
I have already found out how to disable any popup-parent menu items from http://www.microsoft.com/msj/0299/c/c0299.aspx, but this isn't exactly what I want
I have also found out how to add menu command routing to dialogs from the msdn knowledgebase article KB242577.
This works fine for sub-menu items, but not for the top level menu.
I am currently using the following function to do the disabling
void CYourDlg::EnableMenuItem(UINT nCommand, BOOL bEnable)
{
CMenu* pMenu = GetMenu();
pMenu->EnableMenuItem(nCommand, bEnable ? 0 : MF_DISABLED | MF_GRAYED);
}
This half works, if you alt-tab away from the app it does show as disabled, otherwise it doesn't.
Is there a way to invalidate the area programmatically?
I think an non-client area message may be involved.
I have not tried but in regular window (not dialog) CWnd::DrawMenuBar should do what you want. It might work with dialog based applications as well.
void CYourDlg::EnableMenuItem(UINT nCommand, BOOL bEnable)
{
CMenu* pMenu = GetMenu();
pMenu->EnableMenuItem(nCommand, bEnable ? 0 : MF_DISABLED | MF_GRAYED);
DrawMenuBar();
}
I think you should add an ON_UPDATE handler for your menu ID. This would ensure that the menu is enabled/disabled when you want to.