Jump to content
  • 0
John User

Keypad, Timepad and Keyboard theme

Question

Could you show an example code for use of the keyboard, keypad or time pad that changes with the WBT system theme?

I have the template code that contains methods Q_INIT_RESOURCE(images); WbtBroadcastMessenger callbacks that catch "ThemeChanged" and "LoadResource" messages and then use the "setStyleSheet" and "QResource::registerResource", is there anything else?

Share this post


Link to post
Share on other sites

1 answer to this question

Recommended Posts

  • 0

The Keyboard Tray and its siblings get their theme from the same style sheet as the rest of the widgets. This style sheet is acquired in one of two ways depending on the firmware version of the WBT in which your App is running. Both should be accounted for to ensure compatibility with the widest number of system configurations.

Note: The following guide mimics the implementation utilized in 2.4.* versions of the API.

First, ensure your App handles messages from the WBT Client using WbtApi::WbtBroadcastMessenger. You can view an example of this in 2.4 by creating a new Wbt App GUI project and examining the default code. This guide will assume your App has already set an appropriate Callback.

Create 3 Signals and 3 Slots in your MainWindow class to handle theme related messages (shown below), each taking 1 QString as a parameter. Be sure to connect these in your constructor. Additionally, we'll want to create a QString that stores the location of the active resource files, as different themes will have different resource files.

 

//	mainwindow.h
//  ...

signals:
	void ThemeChanged( QString );
	void SetStyleSheet( QString );
	void LoadResource( QString );
	
private slots:
	void onThemeChanged( QString theme );
	void onSetStyleSheet( QString styleSheet );
	void onLoadResource( QString resource );

private:
	QString m_resource

//	mainwindow.cpp
//	In the constructor...

connect( this, SIGNAL( ThemeChanged( QString ) ),
		 this, SLOT( onThemeChanged( QString ) ) );
connect( this, SIGNAL( SetStyleSheet( QString ) ),
		 this, SLOT( onSetStyleSheet( QString ) ) );
connect( this, SIGNAL( LoadResource( QString ) ),
		 this, SLOT(onLoadResource( QString ) ) );

//	...

 

Next, emit the Signal which corresponds with the theme messages received in your Message Callback.

 

//	... In the MessageCallback function

if( subject.compare( "ThemeChanged" ) == 0 )
{	//	Handles Style messages from 2.3
	emit window->ThemeChanged( QString::fromStdString( body ) );
}
else if( subject.compare( "SetStyleSheet" ) == 0 )
{	//	Handles Style messages from 2.4
	emit window->SetStyleSheet( QString::fromStdString( body ) );
}
else if( subject.compare( "LoadResource" ) == 0 )
{
	emit window->LoadResource( QString::fromStdString( body ) );
}

//	...

 

Finally we needs to implement our slots, which will set the App wide style sheet and register the resource file.

 

void MainWindow::onThemeChanged( QString theme )
{
	//	2.3 provides an address to the theme file, so we'll have to read the style sheet manually.
	QFile themeFile( theme );
	if (!themeFile.open( QIODevice::ReadOnly | QIODevice::Text ) )
		return;

	QString styleSheet = "";
	QTextStream themeStream( &themeFile );
	while( !themeStream.atEnd() )
	{
		styleSheet += themeStream.readLine();
	}
	setStyleSheet( styleSheet );
}
void MainWindow::onSetStyleSheet( QString styleSheet )
{
	//	2.4 sends the actual style sheet, so all we need to do is set it.
	setStyleSheet( styleSheet );
}
void MainWindow::onLoadResource( QString resource )
{
	//	First we want to unregister the current resource file. 
	QResource::unregisterResource( m_resource );
	m_resource = resource;
	
	if( !QResource::registerResource( m_resource ) )
	{
		qDebug() << "Failed to register" << m_resource;
	}
}

 

Assuming Widgets and Dialogs within the App have their parents properly assigned, the style sheet will propogate through the App, settings the style of all widgets and keyboards.

Edited by Chris A
Slots should be private, not public. Clarified where an example may be viewed.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×