Qt 点击QLineEdit弹出系统键盘

参考文章:Qt5 QLineEdit实现点击调用软键盘

//在需要使用的类中添加关于软键盘的头文件
#include <Windows.h>
#pragma comment(lib, "user32.lib")
 /* 构造函数中给LineEdit控件添加事件过滤器 */
  ui->lineEdit1->installEventFilter(this);
   ui->lineEdit2->installEventFilter(this);//可以写多个
/* 事件过滤器函数重写 */
bool ClassName::eventFilter(QObject *watched, QEvent *event)
{
    if ( (watched == ui->lineEdit1||watched == ui->lineEdit2) && (event->type() == QEvent::MouseButtonPress) )
    {
        callKeyBoard();// 调用软键盘的方法 
    }
    return QWidget::eventFilter(watched,event);
}

/* 调用软键盘 */
void ClassName::callKeyBoard()
{	PVOID OldValue;
    BOOL bRet = Wow64DisableWow64FsRedirection (&OldValue);
    QString csProcess="C:\\Windows\\System32\\osk.exe";
    QString params="";
    ShellExecute(NULL, L"open", (LPCWSTR)csProcess.utf16(), (LPCWSTR)params.utf16(), NULL, SW_SHOWNORMAL);
    if ( bRet )
    {
        Wow64RevertWow64FsRedirection(OldValue);
    }
}
  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
Qt中,要实现在点击文本框时弹出键盘,可以通过以下步骤实现: 1. 创建一个文本框控件,并设置使其可编辑。 2. 为该文本框控件关联一个事件,例如 `mousePressEvent` 事件。 3. 在事件处理函数中,调用 `QWidget::setFocus()` 方法来使文本框控件获得焦点。 4. 利用 `QGuiApplication` 类的静态方法 `inputMethod()` 来获取键盘对象。 5. 调用键盘对象的 `show()` 方法,以显示软键盘。 示例代码如下: ``` #include <QApplication> #include <QLineEdit> #include <QMouseEvent> #include <QInputMethod> class MyLineEdit : public QLineEdit { public: MyLineEdit(QWidget *parent = nullptr) : QLineEdit(parent) {} protected: void mousePressEvent(QMouseEvent *event) override { QLineEdit::mousePressEvent(event); setFocus(); // 激活文本框焦点 QInputMethod *inputMethod = QGuiApplication::inputMethod(); inputMethod->show(); // 显示软键盘 } }; int main(int argc, char *argv[]) { QApplication app(argc, argv); MyLineEdit lineEdit; lineEdit.setPlaceholderText("点击这里弹出键盘"); lineEdit.show(); return app.exec(); } ``` 以上代码中,我们自定义了一个继承自QLineEdit的MyLineEdit类,并重写了鼠标点击事件。在鼠标点击事件中,我们首先调用`setFocus()`方法使文本框获得焦点,然后通过`QGuiApplication::inputMethod()`获取软键盘对象,并调用`show()`方法显示软键盘。 注意,软键盘的显示和隐藏取决于操作系统和设备的设置,可能在某些设备上不起作用。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值