How to Get the Topmost Window Title using Qt/C++ (in Linux)

Below I have described the method to get active (focused) windows title using Qt in Linux systems (Such as Ubuntu, CentOS etc.) Basically you use xdotool with two parameters mentioned below to get the title of current window which has the focus.

Parameter 1 needs to be getwindowfocus and param 2 should be getwindowname. See below:

QString MyClass::getTopWindowTitleLinux()
{
	QProcess process(this);
	process.setProgram("xdotool");
	process.setArguments(QStringList() << "getwindowfocus" << "getwindowname");
	process.start();
	while (process.state() != QProcess::NotRunning)
		qApp->processEvents();
	return process.readAll();
}

Updated June 22, 2016

You might need to install xdotool if it doesn’t exist.



Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.