1.概要
qt 6.7 创建一个自定义的输入框,当点击的时候,弹窗一个对话框,对话框上有10个按钮,分别对应0~9个数据,关闭和退格按钮,安排按照4行3,点击每个按钮会将相应的内容输入到输入框中,点击退格删除一个字符,点击确定按钮关闭弹窗。将头文件和源文件分开
2.代码
2.1 CustomLineEdit
#ifndef CUSTOMLINEEDIT_H
#define CUSTOMLINEEDIT_H
#include <QLineEdit>
#include <QDialog>
#include <QPushButton>
#include <QGridLayout>
class CustomLineEdit : public QLineEdit {
Q_OBJECT
public:
explicit CustomLineEdit(QWidget *parent = nullptr);
~CustomLineEdit();
protected:
void mousePressEvent(QMouseEvent *event) override;
private:
QDialog *numberDialog;
void setupNumberDialog();
void updateLineEdit(const QString &text);
private slots:
void onButtonClicked(int number);
void onBackspaceClicked();
void onDialogAccepted();
};
#endif // CUSTOMLINEEDIT_H
#include "CustomLineEdit.h"
CustomLineEdit::CustomLineEdit(QWidget *parent) : QLineEdit(parent) {
setupNumberDialog();
}
CustomLineEdit::~CustomLineEdit() {
delete numberDialog;
}
void CustomLineEdit::mousePressEvent(QMouseEvent *event) {
QLineEdit::mousePressEvent(event);
numberDialog->show();
}
void CustomLineEdit::setupNumberDialog() {
numberDialog = new QDialog(this);
QGridLayout *layout = new QGridLayout(numberDialog);
QPushButton *zeroButton = new QPushButton("0", numberDialog);
layout->addWidget(zeroButton, 3, 1);
connect(zeroButton, &QPushButton::clicked, this, [this]() {
onButtonClicked(0);
});
QPushButton *backspaceButton = new QPushButton("Backspace", numberDialog);
QPushButton *okButton = new QPushButton("OK", numberDialog);
layout->addWidget(backspaceButton, 3, 0);
layout->addWidget(okButton, 3, 2);
for (int i = 1; i <= 9; ++i) {
QPushButton *button = new QPushButton(QString::number(i), numberDialog);
int row = (i - 1) / 3;
int col = (i - 1) % 3;
layout->addWidget(button, row, col);
connect(button, &QPushButton::clicked, this, [this, i]() {
onButtonClicked(i);
});
}
connect(backspaceButton, &QPushButton::clicked, this, &CustomLineEdit::onBackspaceClicked);
connect(okButton, &QPushButton::clicked, numberDialog, &QDialog::accept);
connect(numberDialog, &QDialog::accepted, this, &CustomLineEdit::onDialogAccepted);
}
void CustomLineEdit::onButtonClicked(int number) {
QString currentText = this->text();
this->setText(currentText + QString::number(number));
}
void CustomLineEdit::onBackspaceClicked() {
QString currentText = this->text();
if (!currentText.isEmpty()) {
this->setText(currentText.left(currentText.length() - 1));
}
}
void CustomLineEdit::onDialogAccepted() {
numberDialog->hide();
}
2.2 MainWindow
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
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;
};
#endif // MAINWINDOW_H
#include "mainwindow.h"
#include "./ui_mainwindow.h"
#include "CustomLineEdit.h"
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
{
ui->setupUi(this);
CustomLineEdit* cle = new CustomLineEdit(this);
}
MainWindow::~MainWindow()
{
delete ui;
}
2.3 主函数
#include "mainwindow.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();
return a.exec();
}