自己始终都无法实现点击数字,在文本框里面出现数字,求指导一下。
mainwindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <QLineEdit>
QT_BEGIN_NAMESPACE
namespace Ui { class MainWindow; }
QT_END_NAMESPACE
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
MainWindow(QWidget *parent = nullptr);
~MainWindow();
private:
Ui::MainWindow *ui;
QLineEdit *lineEdit;
void keyPressed(int num);
};
#endif // MAINWINDOW_H
mainwindow.cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QPushButton>
#include <QSignalMapper>
#include <QFont>
#include <QLineEdit>
#include <QString>
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
//设置窗口大小
this->setFixedSize(200,300);
QFont font;
font.setPointSize(22);
//数字键
ui->setupUi(this);
QSignalMapper *m =new QSignalMapper(this);
QPushButton *a=new QPushButton("0",this);
connect(a, SIGNAL(clicked()), m, SLOT(map()));
m->setMapping(a, 0);
connect(m, SIGNAL(mapped(int)), this, SLOT(keyPressed(int)));
a->setGeometry(QRect(70, 250, 50, 50));
a->setFont(font);
QPushButton *b=new QPushButton("1",this);
connect(b, SIGNAL(clicked()), m, SLOT(map()));
m->setMapping(b, 1);
connect(m, SIGNAL(mapped(int)), this, SLOT(keyPressed(int)));
b->setGeometry(QRect(10, 70, 50, 50));
b->setFont(font);
QPushButton *c=new QPushButton("2",this);
connect(c, SIGNAL(clicked()), m, SLOT(map()));
m->setMapping(c, 2);
connect(m, SIGNAL(mapped(int)), this, SLOT(keyPressed(int)));
c->setGeometry(QRect(70, 70, 50, 50));
c->setFont(font);
QPushButton *d=new QPushButton("3",this);
connect(b, SIGNAL(clicked()), m, SLOT(map()));
m->setMapping(d, 3);
connect(m, SIGNAL(mapped(int)), this, SLOT(keyPressed(int)));
d->setGeometry(QRect(130, 70, 50, 50));
d->setFont(font);
QPushButton *e=new QPushButton("4",this);
connect(e, SIGNAL(clicked()), m, SLOT(map()));
m->setMapping(e, 4);
connect(m, SIGNAL(mapped(int)), this, SLOT(keyPressed(int)));
e->setGeometry(QRect(10, 130, 50, 50));
e->setFont(font);
QPushButton *f=new QPushButton("5",this);
connect(b, SIGNAL(clicked()), m, SLOT(map()));
m->setMapping(f, 5);
connect(m, SIGNAL(mapped(int)), this, SLOT(keyPressed(int)));
f->setGeometry(QRect(70, 130, 50, 50));
f->setFont(font);
QPushButton *g=new QPushButton("6",this);
connect(g, SIGNAL(clicked()), m, SLOT(map()));
m->setMapping(g, 6);
connect(m, SIGNAL(mapped(int)), this, SLOT(keyPressed(int)));
g->setGeometry(QRect(130, 130, 50, 50));
g->setFont(font);
QPushButton *h=new QPushButton("7",this);
connect(h, SIGNAL(clicked()), m, SLOT(map()));
m->setMapping(h, 7);
connect(m, SIGNAL(mapped(int)), this, SLOT(keyPressed(int)));
h->setGeometry(QRect(10, 190, 50, 50));
h->setFont(font);
QPushButton *i=new QPushButton("8",this);
connect(i, SIGNAL(clicked()), m, SLOT(map()));
m->setMapping(i, 8);
connect(m, SIGNAL(mapped(int)), this, SLOT(keyPressed(int)));
i->setGeometry(QRect(70, 190, 50, 50));
i->setFont(font);
QPushButton *j=new QPushButton("9",this);
connect(j, SIGNAL(clicked()), m, SLOT(map()));
m->setMapping(j, 9);
connect(m, SIGNAL(mapped(int)), this, SLOT(keyPressed(int)));
j->setGeometry(QRect(130, 190, 50, 50));
j->setFont(font);
//清除按钮
QPushButton *remove=new QPushButton("delete",this);
remove->setGeometry(QRect(130, 250, 50, 50));
remove->resize(50,50);
//显示框
QLineEdit *lineEdit= new QLineEdit(this);
lineEdit->setGeometry(QRect(10, 10, 290, 50));
lineEdit->resize(170,50);
lineEdit->setText(QString());
//实现清除按钮的清除功能
connect(remove,&QPushButton::clicked,lineEdit,[lineEdit](){lineEdit->setText("");});
}
//将数字放入lineEdit
void MainWindow::keyPressed(int i)
{
QString string_temp = QString::number(i,10);//类型转换
/*int idx = lineEdit->cursorPosition();//光标位置
QString strContent;
strContent.insert(idx, string_temp);//插入数据
lineEdit->setText(strContent);
lineEdit->setCursorPosition(idx+1);
lineEdit->setFocus();*/
lineEdit->setText(string_temp);
}
MainWindow::~MainWindow()
{
delete ui;
}
设计一个数字键盘,代码可以运行,就是最关键的功能无论如何都实现不了。