Getting a list of window WIds in QT - c++

I'm writing a library in QT which will take screenshots of arbitrary external windows. I know how to take the screenshot using QScreen::grabWindow(), but this takes as an argument a WId, and I would like to know if there is a way to get a list of WIds for all windows on the screen and/or desktop (or something similar, such as getting a WId for a specific window using a title name), via QT. I am aware that I can do this in a platform dependent way, such as EnumWindows in Windows, but I was hoping to keep it cross-platform within QT if possible.

This isn't possible with Qt. If you want your library to be platform independent, you need to write a code for each platform you want to support.
To make this platform independent, you have to write a (public) function in which you test for the platform using preprocessor #if:
#ifdef __unix__
// call unix specific code
#elseif ...
// other platforms
#else
#error Platform not supported!
#endif
For the unix specific code, you need to use xlib, which manages the windows in a tree. From the following code, you will get ALL windows, and in X11 there are a lot of invisible windows and windows which you don't think that they are separate windows. So you definitely have to filter the results, but this depends on which window types you want to have.
Take this code as a start:
#include <X11/Xlib.h>
// Window is a type in Xlib.h
QList<Window> listXWindowsRecursive(Display *disp, Window w)
{
Window root;
Window parent;
Window *children;
unsigned int childrenCount;
QList<Window> windows;
if(XQueryTree(disp, w, &root, &parent, &children, &childrenCount))
{
for(unsigned int i = 0; i < childrenCount; ++i)
{
windows << children[i];
windows << listXWindowsRecursive(disp, children[i]);
}
XFree(children);
}
return windows;
}
Display *disp = XOpenDisplay(":0.0");
Window rootWin = XDefaultRootWindow(disp);
QList<Window> windows = listXWindowsRecursive(disp, rootWin);
foreach(Window win, windows)
{
// Enumerate through all windows
}

Related

Is this "modern" message box UI available in Qt? Or is it pure Windows API?

Modern Windows versions (I would say 7+) have this nice UI that I see often:
It doesn't seem like it was designed from scratch. It seems to me that it's already available in the Windows API.
Is it possible to load this in Qt? What's the widget name? Or is it just MFC or something?
The three buttons are called Command Buttons or Command Link Control. It was first introduced as part of the Windows XP API. The form in the picture (with explaining text under the command link) is available since Windows Vista, I think.
If you want portable code, you can't use it in Qt. Not even Wine can display command buttons by now. (In Wine 2.0 they are invisible but they can be klicked).
Update: There is a QCommandLinkButton class in Qt.
This is called a TaskDialog which is available beginning with Windows Vista. It is pure Win API.
I don't know if there is a Qt wrapper available (Edit: #thomiels answer links to a Qt widget).
Here is some native code to create a dialog similar to the one shown in the screenshot:
#include <Windows.h>
#include <CommCtrl.h>
#pragma comment(lib,"comctl32.lib")
#pragma comment(linker,"/manifestdependency:\"type='win32' name='Microsoft.Windows.Common-Controls' version='6.0.0.0' processorArchitecture='*' publicKeyToken='6595b64144ccf1df' language='*'\"")
int main(int argc, char* argv[])
{
TASKDIALOGCONFIG cfg{ sizeof(cfg) };
const TASKDIALOG_BUTTON buttons[] = {
{ IDOK, L"Do something" },
{ IDCANCEL, L"Do another something" },
};
cfg.hInstance = ::GetModuleHandle( nullptr );
cfg.dwCommonButtons = 0;
cfg.pszMainIcon = TD_INFORMATION_ICON;
cfg.pszMainInstruction = L"Here you can do awesome stuff";
cfg.pszContent = L"What do you want to do?";
cfg.pButtons = buttons;
cfg.cButtons = ARRAYSIZE(buttons);
cfg.dwFlags = TDF_USE_COMMAND_LINKS;
HRESULT hr = TaskDialogIndirect( &cfg, nullptr, nullptr, nullptr );
}

C++ Global Hotkeys with platform APIs

I'm working on an application for taking screenshots on Windows, OSX and Linux in C++/Qt. Now I need to set global hotkeys, so the user can take screenshots when the application is running in the background. I tried with Qxt and UGlobalHotkey, which are both Qt libraries, but neither of them seemed to work.
I tried to implement it for OSX with Carbon (tutorial), but I need to call a class member function, which just doesn't work. Could someone provide me with an example? You can find my code here. The function i need to call is new_screenshot().
Or is there any other way to achieve something like this? I really need my application to take a screenshot from the background, otherwise it's pretty useless (yes, I should probably have implemented it at the very beginning to see if it even works). Would it maybe be better to have a separate client for every platform (Cocoa Swift for OSX, GTK for Linux, C# client for Windows)? I have often thought about this the past few days.
Do I understand correctly that you want to call new_screenshot from the hot key event handler? If so, InstallApplicationEventHandler lets you pass a pointer to user data in 4th argument. Pass a pointer to your MainWindow instance (based on code from the tutorial):
MainWindow *mainWindow = ... // get main window somehow
InstallApplicationEventHandler(&MyHotKeyHandler,1,&eventType,mainWindow,NULL);
Then you can use it in the event handler.
OSStatus MyHotKeyHandler(EventHandlerCallRef nextHandler,EventRef theEvent, void *userData)
{
//Do something once the key is pressed
static_cast<MainWindow*>(userData)->new_screenshot();
return noErr;
}
I did something in the past with MFC and WIN32 API....so it only works on Windows...but pressing ALT+F10 was able to hide/show a window...
void CWinHideDlg::OnButtonActive()
{
CString tmp;
GetDlgItemText(IDC_BUTTON_ACTIVE,tmp);
if(0 == strcmp(tmp.GetBuffer(tmp.GetLength()),"Activate"))
{
m_myAtom=GlobalAddAtom("MY_GLOBAL_HOT_HIDE_KEY");
int err=RegisterHotKey(this->GetSafeHwnd(),m_myAtom,MOD_ALT,VK_F10);
SetDlgItemText(IDC_BUTTON_ACTIVE,"Stop");
CButton *pBtn = (CButton *)GetDlgItem(IDC_BUTTON_UNHIDE);
pBtn->EnableWindow(TRUE);
SetDlgItemText(IDC_STATIC_INFO,"Set the mouse over the window \nand press ALT + F10 to hide it...");
}
else
{
UnregisterHotKey(this->GetSafeHwnd(),m_myAtom);
GlobalDeleteAtom(m_myAtom);
CButton *pBtn = (CButton *)GetDlgItem(IDC_BUTTON_UNHIDE);
pBtn->EnableWindow(FALSE);
SetDlgItemText(IDC_BUTTON_ACTIVE,"Activate");
}
}
Basically this code activates/deactivates the hot key ALT+F10, once it activates you can hide/unhide a running window on the system by setting the mouse pointer over the window and press ALT+F10...
This is from the WindowProc function:
if(message == WM_HOTKEY)
{
CString tmp;
POINT pc;
GetCursorPos(&pc);
if(GetAsyncKeyState(VK_F10))
{
HWND hwnd=::WindowFromPoint(pc);
if(hwnd)
{
tmp.Format("%08Xh",hwnd);
m_HideWins.InsertString(m_HideWins.GetCount(),tmp);
::ShowWindow(hwnd,SW_HIDE);
}
}
}
You can use the code to register your own HOT Key and use it to take a screenshot...
Hope it helps...

QMessageBox Compatibility

I'm looking through some Qt code and see that rather than just using QMessageBox, the program checks whether QAPPLICATION_H is defined. If it isn't, then it uses some default system message box. Here's what it looks like:
bool Connect()
{
...
{
#ifdef QAPPLICATION_H
QMessageBox::critical(0,QString("Error!"),QString("Cannot Connect To PS3"));
#else
MessageBoxA(0,"Error!","Cannot Connect To PS3",MB_ICONINFORMATION);
#endif
return false;
}
else
{
...
#ifdef QAPPLICATION_H
QMessageBox::information(0,QString("Sucess!"),QString("Connected To PS3!"));
#else
MessageBoxA(0,"Sucess!", "Connected To PS3", MB_ICONINFORMATION);
#endif
return true;
}
}
Basically, my question is: what's the compatibility of QMessageBox? If I released a program that only uses QMessageBox, will people without Qt not be able to see the message pop up? I just don't want to have to check for this every time in my own code, and also the standard non-Qt box looks worse.
Qt is cross platform QMessageBox will be available on any platform you compile your code. I don't know why in the listed code is that define and the call to MessageBoxA, maybe the developer wanted to be able to display a more windows look and feel message box, in case the target platform is windows.

C++ Windows System Tray wont display message

I have been stuck here for 4 days. I made a function that puts a program in the system tray but the problem here is that it wont show balloon title and message. What Am I doing Wrong? I even made a separate function to determine what windows os we are running on and initialize cbSize based on the Os detected. Any help will be appreciated. Bellow is the function.
EDIT: I am using Windows 7 and the Icon shows up in the system tray but wont show the message or title. I am also doing this Console Application right now as this will be used as a plugin in Unity3D. I want a solution that uses windows api but not windows form as I don't want any new window to open from this.
void createSystemTray()
{
HWND wHandler = GetDesktopWindow();
NOTIFYICONDATA iData;
ZeroMemory(&iData,sizeof(iData));
if(getOsVersion()=="Windows Vista" || getOsVersion()=="Windows 7" || getOsVersion()=="Windows 8" || getOsVersion()=="Windows 8.1")
{
iData.cbSize = sizeof(NOTIFYICONDATA);
}
else if (getOsVersion()=="Windows XP"||getOsVersion()=="Windows XP Professional x64 Edition")
{
iData.cbSize = NOTIFYICONDATA_V3_SIZE;
}
else if (getOsVersion()=="Windows 2000")
{
iData.cbSize = NOTIFYICONDATA_V2_SIZE;
}
else if (getOsVersion()=="UNKNOWN OS")
{
//Assume we have old Windows Os such as Me,95....
iData.cbSize = NOTIFYICONDATA_V1_SIZE;
}
iData.hWnd = wHandler;
iData.uID = 100;
iData.uVersion = NOTIFYICON_VERSION_4;
iData.uCallbackMessage = WM_MESSAGE;
iData.hIcon = LoadIcon(NULL,(LPCTSTR)IDI_WARNING);
lstrcpy(iData.szTip,"My First Tray Icon");
lstrcpy(iData.szInfo,"My App Info");
lstrcpy(iData.szInfoTitle,"My Info Title");
iData.uFlags = NIF_MESSAGE|NIF_ICON|NIF_TIP;
Shell_NotifyIcon(NIM_SETVERSION,&iData); //called only when usingNIM_ADD
Shell_NotifyIcon(NIM_ADD,&iData);
}
I added NIF_INFO to the uFlags and the problem is gone. Now it displays everything including text, title and info title.
The code below is what solved it.
iData.uFlags = NIF_MESSAGE|NIF_ICON|NIF_TIP|NIF_SHOWTIP|NIF_INFO;
Your biggest problem with the code in the question is that you pass the wrong window handle. You have to pass one of your window handles. But instead you pass the window handle of the desktop.
You will need to create a window and use its handle. The window does not need to be visible. I believe that you can use a message only window.
You must also call NIM_SETVERSION after NIM_ADD.
I'm very sceptical of your version switching being based on string equality testing. Your code will break on Windows 9 for instance. Use the version helper functions.
You also perform no error checking. This isn't the easiest function to call but your failure to check for errors makes things even harder than they need to be. Please read the documentation and add error checking code.

Is there a Mac OSX version of windows GetFontData function?

We have an application that allows our users to generate print-ready PDF documents using variable data (names, titles, phone numbers, etc) that their customer's enter via an online e-commerce website. To facilitate the variable data we have to embed a font's entire character map. On windows we have been using the windows API function GetFontData(...) like so:
#ifdef Q_WS_WIN
//this is windows specific code
HDC DC = CreateCompatibleDC(NULL);
HFONT hFont = font.handle();
HFONT oFont=(HFONT)SelectObject(DC,hFont);
DWORD fontLength = ::GetFontData(DC, 0, 0, 0, 0);
if(fontLength != GDI_ERROR)
{
fontData.GrowAllocation(fontLength);
if(::GetFontData(DC, 0, 0, fontData.GetBuffer(), fontLength) == GDI_ERROR)
{
fontData.Clear();
}
else
{
fontData.SetLength(fontLength);
returnVal = true;
}
}
SelectObject(DC,oFont);
DeleteDC(DC);
//End of windows specific code
#elif defined(Q_WS_MAC)
#endif
This technique works very successfully on our windows specific version; however, we are porting the application to Qt to target the Mac OSX platform.
My first question: Is there a Qt way of accessing the raw font data from a QFont, QFontDatabase, etc. that we could use to embed in the pdf? We have been unable to find a way. Notice the #ifdef wrapper in the above code. Note that the variable fontData is a self contained memory buffer that manages its own memory, please disregard.
My second question: If there is no Qt way of accessing the font data in an OS agnostic way, what is the OSX equivalent to the windows GetFontData?
Thanks
I've only skimmed documentation for GetFontData, and I don't know much about Qt (though it does sound like a Qt solution would be preferable), but you might want to look at the ObjC class for fonts, NSFont.
The latest (until Apple hires new font people and gets infected with not-invented-here disease again) text and font API is Core Text. I never used it since chasing Apple's mood is harmful to my personal and organization's financial health, I stopped using any Apple-only API. :)
Kidding aside, it looks like CTFont does provide access to all tables in a font. I don't see anything that prepares a font for embedding but this probably gives you the best chance.