Qt调用系统软键盘

Qt调用系统软键盘
在一些环境下,运行的环境下没有外界键盘,这样必须要使用虚拟键盘。
方法有两种:
1.调用电脑本身自带的软键盘。方便。
2.自己造车,自己写一个。
本文。只是这针对第一个,调用系统自带的软键盘。第二种可以自己尝试。
本文重点是在开启键盘方法
源代码如下
.ui文件
在这里插入图片描述
.h文件

#include <QMainWindow>

namespace Ui {
class MainWindow;
}

class MainWindow : public QMainWindow
{
    Q_OBJECT
public:
    explicit MainWindow(QWidget *parent = 0);
    ~MainWindow();

protected:
    bool eventFilter(QObject *watched, QEvent *event);

private slots:
    void on_pushButton_clicked();
    void on_pushButton_2_clicked();

private:
    Ui::MainWindow *ui;
};

.cpp 文件

#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QMessageBox>
#include <QProcess>
#include <QDebug>
#include <QDesktopServices>
#include <QUrl>

#include <Windows.h>
#pragma comment(lib, "user32.lib")

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    ui->lineEdit_name->clear();    //清除显示
    ui->lineEdit_class->clear();
    //事件绑定
    ui->lineEdit_name->installEventFilter(this);
    ui->lineEdit_class->installEventFilter(this);

}

MainWindow::~MainWindow()
{
    delete ui;
}

//开启系统软键盘
void MainWindow::on_pushButton_clicked()
{
    //只能在win7下使用
    //QDesktopServices::openUrl(QUrl("osk.exe", QUrl::TolerantMode));

    //win8.1   win10 下使用
    PVOID OldValue = nullptr;
    BOOL bRet = Wow64DisableWow64FsRedirection(&OldValue);
    QString csProcess = "C:\\Windows\\System32\\osk.exe";
    QString params="";
    ShellExecute(nullptr, L"open", (LPCWSTR)csProcess.utf16(), (LPCWSTR)params.utf16(), nullptr, SW_SHOWNORMAL);
    if (bRet)
    {
        Wow64RevertWow64FsRedirection(OldValue);
    }
}

//事件过滤器
bool MainWindow::eventFilter(QObject *watched, QEvent *event)
{
    if(watched==ui->lineEdit_name && event->type()==QEvent::MouseButtonPress){
        ui->lineEdit_name->setFocus();
    }else if(watched==ui->lineEdit_class && event->type()==QEvent::MouseButtonPress){
        ui->lineEdit_class->setFocus();
    }
    return QWidget::eventFilter(watched,event);
}


//提交用户信息
void MainWindow::on_pushButton_2_clicked()
{
    QString name,banji,gender,str;
    name=ui->lineEdit_name->text().trimmed();
    banji=ui->lineEdit_class->text().trimmed();
    gender=ui->comboBox_gender->currentText();
    if(name=="" && banji==""){
        QMessageBox::information(this,"提示","学信信息不能为空!",QMessageBox::Ok);
    }else{
        str=name+","+banji+","+gender;
        ui->label_studentInfo->setText(str);
    }
}

运行结果如下:
在这里插入图片描述

  • 2
    点赞
  • 35
    收藏
    觉得还不错? 一键收藏
  • 5
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值