使用QSignalMapper类实现一个数字软键盘
基本使用如下:
-
QSignalMapper *m = QSignalMapper(this);
-
QPushButton *b;
-
b=new QPushButton("1");
-
connect(b, SIGNAL(clicked()),m, SLOT(map()));
-
m->setMapping(b, 1);
-
...
-
connect(m, SIGNAL(mapped(int)), this, SLOT(keyPressed(int)));
示例程序:
-
SoftKeyboardDlg::SoftKeyboardDlg(QWidget *parent) :
-
QDialog(parent),
-
ui(new Ui::SoftKeyboardDlg)
-
{
-
ui->setupUi(this);
-
-
SetWindowBackColour(this);
-
m = new QSignalMapper(this);
-
-
connect(ui->buttonZero,SIGNAL(clicked()),m,SLOT(map()));
-
connect(ui->buttonOne,SIGNAL(clicked()),m,SLOT(map()));
-
connect(ui->buttonTwo,SIGNAL(clicked()),m,SLOT(map()));
-
connect(ui->buttonThree,SIGNAL(clicked()),m,SLOT(map()));
-
connect(ui->buttonFour,SIGNAL(clicked()),m,SLOT(map()));
-
connect(ui->buttonFive,SIGNAL(clicked()),m,SLOT(map()));
-
connect(ui->buttonSix,SIGNAL(clicked()),m,SLOT(map()));
-
connect(ui->buttonEight,SIGNAL(clicked()),m,SLOT(map()));
-
connect(ui->buttonSeven,SIGNAL(clicked()),m,SLOT(map()));
-
connect(ui->buttonNine,SIGNAL(clicked()),m,SLOT(map()));
-
connect(ui->pushButtonX,SIGNAL(clicked()),m,SLOT(map()));
-
connect(ui->pushButtonDel,SIGNAL(clicked()),m,SLOT(map()));
-
-
m->setMapping(ui->buttonZero,0);
-
m->setMapping(ui->buttonOne,1);
-
m->setMapping(ui->buttonTwo,2);
-
m->setMapping(ui->buttonThree,3);
-
m->setMapping(ui->buttonFour,4);
-
m->setMapping(ui->buttonFive,5);
-
m->setMapping(ui->buttonSix,6);
-
m->setMapping(ui->buttonSeven,7);
-
m->setMapping(ui->buttonEight,8);
-
m->setMapping(ui->buttonNine,9);
-
m->setMapping(ui->pushButtonX,10);
-
m->setMapping(ui->pushButtonDel,-1);
-
-
connect(m,SIGNAL(mapped(int)),this,SLOT(keyPress(int)));
-
ui->buttonOne->setFocus();
-
setStyleSheet(BTN_FOCUS_BACK_COLOR);
-
}
-
-
SoftKeyboardDlg::~SoftKeyboardDlg()
-
{
-
delete ui;
-
}
-
-
void SoftKeyboardDlg::keyPress(int i)
-
{
-
emit keyNum(i);
-
}
阅读(7751) | 评论(0) | 转发(1) |