GUI-Novice
GUI-Novice

Reputation: 361

How to get active window title in Ubuntu Qt?

I am using below win API code to capture the active working window title in windows desktop. Is there any alternative in Qt for Ubuntu platform for this implementation ?

QString getActiveWndTitle()
{
    char buff[256];
    HWND hwnd = GetForegroundWindow();
    GetWindowText(hwnd, (LPWSTR) buff, 254);
    QString title = QString::fromWCharArray((const wchar_t *)buff);
    return title;
}

Upvotes: 3

Views: 1924

Answers (2)

Yanet
Yanet

Reputation: 181

If you want to get the Qt application active window title, you can use

QApplication::activeWindow()->windowTitle();

If not, you can use QProcess to run an Ubuntu command. This is, xdotool or (if you dont want to install anything) this command:

xprop -id $(xprop -root _NET_ACTIVE_WINDOW | cut -d ' ' -f 5) WM_NAME | awk -F '"' '{print $2}' 

Upvotes: 3

GUI-Novice
GUI-Novice

Reputation: 361

I got it working with below code. We need to install xdotool in ubuntu

QProcess process(this);
process.setProgram(“xdotool”);
process.setArguments(QStringList() << “getwindowfocus” << “getwindowname”);
process.start();
while(process.state() != QProcess::NotRunning)
qApp->processEvents();
QString title = process.readAll();

Upvotes: 2

Related Questions