weixin_58535207 2022-08-30 21:37 采纳率: 41.7%
浏览 59
已结题

用QT实现,如何在文本框里面出现数字?

自己始终都无法实现点击数字,在文本框里面出现数字,求指导一下。

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;
}

设计一个数字键盘,代码可以运行,就是最关键的功能无论如何都实现不了。

  • 写回答

1条回答 默认 最新

  • 不知怎么说 2022-08-31 11:03
    关注

    你那个map函数做了什么?怎么所有的都连到了一个槽?而且你的m对象怎么连了那么多次信号槽。最后你的d 那一段代码,connect写成了b

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

问题事件

  • 已结题 (查看结题原因) 9月2日
  • 已采纳回答 9月2日
  • 创建了问题 8月30日

悬赏问题

  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料