原创

自制虚拟键盘Demo

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://shazhenyu.blog.csdn.net/article/details/56488685

最近调用系统键盘的时候有些时候系统的键盘并不满足需求,所以做了个小Demo

先看一下效果:

 

看一下核心代码:


#include "skeyboard.h"

SKeyboard::SKeyboard(QWidget *parent) : QWidget(parent)
{
    for(int i = 0; i < 43; i++) {
        QPushButton* tmp_btn = new QPushButton(this);
        tmp_btn->setStyleSheet("QPushButton{border-image:url(:/blue.png);} \
                                QPushButton::hover {border-image:url(:/blue_hover.png);} \
                                QPushButton::pressed {border-image:url(:/blue_press.png);}");
        connect(tmp_btn, &QPushButton::clicked, [=]{
            emit sigKeyPress(tmp_btn->text());
        });
        m_keys << tmp_btn;
    }
    this->resetKeyPosSize();
    this->resetText();
}

void SKeyboard::resetText()
{
    for(int i = 0; i < 10; i++) {
        m_keys.at(i)->setText(QString::number((i + 1) == 10 ? 0 : (i + 1)));
    }
    m_keys.at(10)->setText("Back Space");
    m_keys.at(11)->setText("Q");
    m_keys.at(12)->setText("W");
    m_keys.at(13)->setText("E");
    m_keys.at(14)->setText("R");
    m_keys.at(15)->setText("T");
    m_keys.at(16)->setText("Y");
    m_keys.at(17)->setText("U");
    m_keys.at(18)->setText("I");
    m_keys.at(19)->setText("O");
    m_keys.at(20)->setText("P");
    m_keys.at(21)->setText("A");
    m_keys.at(22)->setText("S");
    m_keys.at(23)->setText("D");
    m_keys.at(24)->setText("F");
    m_keys.at(25)->setText("G");
    m_keys.at(26)->setText("H");
    m_keys.at(27)->setText("J");
    m_keys.at(28)->setText("K");
    m_keys.at(29)->setText("L");
    m_keys.at(30)->setText(".");
    m_keys.at(31)->setText("Z");
    m_keys.at(32)->setText("X");
    m_keys.at(33)->setText("C");
    m_keys.at(34)->setText("V");
    m_keys.at(35)->setText("B");
    m_keys.at(36)->setText("N");
    m_keys.at(37)->setText("M");
    m_keys.at(38)->setText("@");
    m_keys.at(39)->setText("Left");
    m_keys.at(40)->setText("Right");
    m_keys.at(41)->setText(".com");
    m_keys.at(42)->setText("Enter");
}

void SKeyboard::resetKeyPosSize()
{
    //1
    for(int i = 0; i < 10; i++) {
        m_keys.at(i)->resize(55, 55);
        m_keys.at(i)->move(10 + i * 60, 10);
    }
    //2
    for(int i = 11; i < 21; i++) {
        m_keys.at(i)->resize(55, 55);
        m_keys.at(i)->move(10 + (i - 11) * 60, 75);
    }
    //3
    for(int i = 21; i < 31; i++) {
        m_keys.at(i)->resize(55, 55);
        m_keys.at(i)->move(10 + (i - 21) * 60, 140);
    }
    //4
    for(int i = 31; i < 41; i++) {
        m_keys.at(i)->resize(55, 55);
        m_keys.at(i)->move(10 + (i - 31) * 60, 205);
    }
    //right backspace-enter-com
    m_keys.at(10)->resize(125, 55);
    m_keys.at(10)->move(610, 10);
    m_keys.at(10)->setStyleSheet("QPushButton{border-image:url(:/blue_space.png);} \
                                  QPushButton::hover {border-image:url(:/blue_spaceHover.png);} \
                                  QPushButton::pressed {border-image:url(:/blue_spacePress.png);}");
    m_keys.at(41)->resize(125, 55);
    m_keys.at(41)->move(610, 205);
    m_keys.at(41)->setStyleSheet("QPushButton{border-image:url(:/blue_space.png);} \
                                  QPushButton::hover {border-image:url(:/blue_spaceHover.png);} \
                                  QPushButton::pressed {border-image:url(:/blue_spacePress.png);}");
    m_keys.at(42)->resize(125, 120);
    m_keys.at(42)->move(610, 75);
}

 

代码已经上传到CSDN,可以自行下载:

http://download.csdn.net/detail/u014597198/9760954

 

文章最后发布于: 2017-02-22 16:58:37
展开阅读全文

没有更多推荐了,返回首页

©️2019 CSDN 皮肤主题: 代码科技 设计师: Amelia_0503

分享到微信朋友圈

×

扫一扫,手机浏览