qt制作软键盘(虚拟键盘)

步骤
1.制作界面

在这里插入图片描述
QToolbutton
在这里插入图片描述

2.为需要使用到软键盘的控件添加事件过滤器
 ui->userEdit->installEventFilter(this);
 ui->passwordEdit->installEventFilter(this);

2、重写eventFilter事件,判断当前触发对象是否是添加了过滤器的控件,且事件是否是鼠标按钮点击事件。是的话,显示软键盘,并将焦点设置到当前控件上

bool softkey::eventFilter(QObject *watched, QEvent *event)
{

    if ( (watched == ui->userEdit) && (event->type() == QEvent::MouseButtonPress) )
    {
        ui->myWidget->show();
        ui->userEdit->setFocus();
    }
    else if ( (watched == ui->passwordEdit) && (event->type() == QEvent::MouseButtonPress) )
    {
        ui->myWidget->show();
        ui->passwordEdit->setFocus();
    }

    return QMainWindow::eventFilter(watched,event);
}
3.若是当前控件编辑完毕则隐藏软键盘

重写类mylabel自定义信号close_key,当判断当前事件是鼠标点击,则发散close_key信号,并且返回true,Qt 会认为这个事件已经处理完毕,不会再将这个事件发送给其它对象。

class mylabel : public QLabel
{
   Q_OBJECT
public:
   explicit mylabel(QWidget *parent = Q_NULLPTR);

   bool event(QEvent *e) override
   {
       if(e->type() == QEvent :: MouseButtonPress){
           emit close_key();
           return true;
       }
       return QLabel::event(e);
   }

signals:

   void close_key();

};
 connect(ui->backgroundlabel,&mylabel::close_key,this,&softkey::hide_widget);

void softkey::hide_widget()
{
    ui->myWidget->hide();
}
4、在自定义按钮类softKey的槽函数中判断当前点击的按钮文本,定义一个按键事件,通过postEvent将事件key发送出去
softkeys::softkeys(QWidget *parent) : QToolButton(parent)
{
   connect(this,SIGNAL(clicked(bool)),this,SLOT(button_clicked(bool)));
}
void softkeys::button_clicked(bool)
{

   //this->text() ----获得控件上的文本内容,比如,删除
   if(this->text() == "<-")
   {
       //定义一个事件,Key_Backspace撤消按钮
       QKeyEvent* key = new QKeyEvent(QEvent::KeyPress,Qt::Key_Backspace,Qt::NoModifier, this->text());
       //通过postEvent将事件key发送出去给QApplication::focusWidget(),
       //在当前窗口界面中有焦点的控件
       QApplication::postEvent(QApplication::focusWidget(),key);
   }else
   {
       QKeyEvent* key = new QKeyEvent(QEvent::KeyPress,'w',Qt::NoModifier, this->text());
       //通过postEvent将事件key的文本内容发送出去,QApplication::focusWidget()当前窗口中,有光标的
       //控件上
       QApplication::postEvent(QApplication::focusWidget(),key);
   }
}
5.更改控件属性

需要将QToolButton的属性改成softkeys,因为自定义了软键盘的类,因此需要将按钮属性设置为你自定义的类,才能有效果。在这里插入图片描述

同理,自定义了mylabel的类,需要将背景label的属性从QLabel改为mylabel
在这里插入图片描述
具体操作示例
右键
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

最终软键盘效果图

在这里插入图片描述

附上github源代码:
https://github.com/blackyue/QtUi

参考链接:
https://blog.csdn.net/bangtanhui/article/details/116497414
https://www.jianshu.com/p/48f007c2de09

  • 11
    点赞
  • 87
    收藏
    觉得还不错? 一键收藏
  • 5
    评论
好的,我可以为您提供一个简单的Qt虚拟中文键盘的设计方案。以下是一个基本的实现步骤: 1.创建一个QWidget作为虚拟键盘的容器,设置其大小和背景颜色。 2.在容器中添加QGridLayout布局,用于放置键盘按钮。 3.添加键盘按钮,并使用QSignalMapper类将每个按钮的clicked()信号映射到槽函数。 4.在槽函数中,使用QInputMethod类将键盘输入转发到当前的目标QWidget控件。 5.添加一个清除按钮,用于清除目标控件中的文本。 6.使用QInputMethod类控制虚拟键盘的显示和隐藏。 以下是一个简单的代码示例: ```cpp #include <QtWidgets> class VirtualKeyboard : public QWidget { Q_OBJECT public: VirtualKeyboard(QWidget *parent = nullptr); private slots: void buttonClicked(const QString &text); void clearClicked(); private: QSignalMapper *mapper; QInputMethod *inputMethod; void createButtons(); }; VirtualKeyboard::VirtualKeyboard(QWidget *parent) : QWidget(parent) { // Set background color QPalette pal; pal.setColor(QPalette::Background, Qt::lightGray); setPalette(pal); // Create signal mapper mapper = new QSignalMapper(this); connect(mapper, SIGNAL(mapped(QString)), this, SLOT(buttonClicked(QString))); // Create input method inputMethod = QGuiApplication::inputMethod(); // Create buttons createButtons(); // Add clear button QPushButton *clearButton = new QPushButton(tr("Clear")); connect(clearButton, SIGNAL(clicked()), this, SLOT(clearClicked())); layout()->addWidget(clearButton, 4, 0, 1, 2); } void VirtualKeyboard::createButtons() { // Button texts QStringList buttonLabels; buttonLabels << "1" << "2" << "3" << "4" << "5" << "6" << "7" << "8" << "9" << "0" << "Q" << "W" << "E" << "R" << "T" << "Y" << "U" << "I" << "O" << "P" << "A" << "S" << "D" << "F" << "G" << "H" << "J" << "K" << "L" << "Z" << "X" << "C" << "V" << "B" << "N" << "M"; // Add buttons to grid layout int row = 0; int col = 0; int maxCols = 10; for (const QString &text : buttonLabels) { QPushButton *button = new QPushButton(text); connect(button, SIGNAL(clicked()), mapper, SLOT(map())); mapper->setMapping(button, text); layout()->addWidget(button, row, col); col++; if (col == maxCols) { row++; col = 0; } } } void VirtualKeyboard::buttonClicked(const QString &text) { inputMethod->commit(text, 1); } void VirtualKeyboard::clearClicked() { inputMethod->commit("", -1); } int main(int argc, char *argv[]) { QApplication app(argc, argv); QLineEdit *lineEdit = new QLineEdit; VirtualKeyboard *keyboard = new VirtualKeyboard; QGridLayout *layout = new QGridLayout; layout->addWidget(lineEdit, 0, 0); layout->addWidget(keyboard, 1, 0); QWidget window; window.setLayout(layout); window.show(); return app.exec(); } #include "main.moc" ``` 这是一个简单的示例,您可以根据需要进行修改和扩展,以实现更完整的虚拟中文键盘

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值