PDA

View Full Version : How to hide virtual keyboard in Qt?



zed220
15th September 2017, 13:39
Hi all,

I have a very simple code to start/show the virtual keyboard which works fine


keyboard = new Keyboard(this);

but I can not hide it dynamically I have tried


keyboard->disconnect();
also this

keyboard->deleteLater();

Has anyone got any suggestions please.

d_stranz
16th September 2017, 18:37
How about simply calling hide() or show()?

disconnect() has nothing to do with visiblility - it simply disconnects any slots the keyboard signals are connected to. Likewise deleteLater() simply tells Qt to delete the keyboard instance the next time through the event loop (which will hide it and destroy the instance).

zed220
17th September 2017, 16:26
Thanks for your reply, 'keyboard' is a pointer I don't get hide() or show() methods as options, the only relevant methods I get are 'disconnect()' or 'deleteLater()' . I can't be this difficult to hide or disconnect a virtual keyboard can it ?

d_stranz
17th September 2017, 18:25
'keyboard' is a pointer

A pointer to what? You create it with "new Keyboard()" so somewhere you must have a "keyboard.h" or other header file where the Keyboard class is defined. That will tell you where the Keyboard class sits in the Qt class hierarchy. If it is derived from QWidget somewhere back along the line, then it inherits show() and hide() methods from QWidget.

zed220
17th September 2017, 21:43
Thanks again, you're right it's a pointer to a class the only difference is that it's derived from QObject and not from QWidget, do you think that's the reason I'm not seeing hide() and show() methods ?

d_stranz
18th September 2017, 04:24
do you think that's the reason I'm not seeing hide() and show() methods ?

Of course. But a QObject has no on-screen visibility, so what you are creating using the Keyboard class cannot be the virtual keyboard you see on the screen. Only things derived from QWidget (or things in the QGraphicsItem hierarchy) can have an on-screen appearance. It sounds to me as though your Keyboard class is just something that is a component of something else that displays a keyboard on screen.

Maybe you can post a link to whatever it is that you are using so we can better understand it? This seems like it isn't your code and that you got it from somewhere. Where is that?

zed220
18th September 2017, 16:13
This is the class I'm invoking wherever I need the keyboard, I hope it gives you a little idea where I'm going wrong.


#include "keyboard.h"
#include <QProcess>

#ifdef WIN32
#include <windows.h>
#include <QString>
#include <QStringList>
#include <QDebug>
#endif


#include "entrysettings.h"

Keyboard::Keyboard(QObject *parent) : QObject(parent), process(0)
{
#ifdef WIN32

QString program = "explorer.exe";
QString folder = "C:\\Windows\\System32\\osk.exe";
QString tt = "C:\\Progra~1\\Common~1\\micros~1\\ink\\TabTip.exe";

if( EntrySettings::windowsMajorVersion() >= 10 ) {
qDebug() << "Setting Win 10 OSK";

if( process ) {
delete process;
process = new QProcess(this);
process->start(program, QStringList() << folder);
}
else {
process = new QProcess(this);
process->start(program, QStringList() << folder);
}
}
else {
qDebug() << "Windows pre-10 identified";

if( process ) delete process;
process = new QProcess(this);
process->start(program, QStringList() << tt);
}
#endif
}

Keyboard::~Keyboard()
{
#ifdef WIN32
if( process ) delete process;
#endif
}

d_stranz
18th September 2017, 21:36
I hope it gives you a little idea where I'm going wrong.

No, not really. What you've posted is a constructor and destructor with a bunch of code that seem to have nothing to do with a virtual keyboard at all.

Where did this Keyboard class come from? Did you write it? Did you download it from somewhere? How are you creating it? What do you see on screen?

GeneCode
19th September 2017, 00:59
Go find a book and learn.

zed220
19th September 2017, 09:23
Go find a book and learn.

Thanks for the tip, you're too helpful !

VALE61
19th September 2017, 11:32
zed220, the problem is that you didn't create a virtual keyboard object.

You started an external application process that acts as a virtual keyboard.

The only way to interact with such application is using the processId (e.g. sending signals) or using any specific implemented application interface (e.g. TCP socket, etc).
In your case, the simplest way is sending a message to close the application using the menu Tools->exit, but don't ask me how ;-)

d_stranz
19th September 2017, 16:31
You started an external application process that acts as a virtual keyboard.

Ah, this makes sense and it explains all the mysterious QProcess code in the constructor. There is a Qt Virtual Keyboard project (https://doc.qt.io/qt-5/qtvirtualkeyboard-index.html) that implements a plugin and QML UI for a multi-language virtual keyboard but I have not tried it.