引言
- 开发环境
- 项目结构
- ui界面设计
- 示例代码
- 运行效果
- 总结
qt中原有的行编辑器无法满足当前的需要,所以需要自定义行编辑器。
通过上下按键切换到不同的行编辑器,在选中的行编辑器中输入数字,编辑器呈现边框,编辑后按下回车键保存之前编辑的数值,没有按下回车键直接切换上下键之前编辑的数字没有被保存,编辑器中继续显示之前的数值。于此同时会根据不同的位数在数值前自动补齐。
直接上效果图:
20241213_182339
开发环境
Ubuntu系统下QtCreator。
项目结构
项目的结构如下:
ui界面设计
dialog.ui
对应的结构:
form1.ui
对应的结构:
form2.ui
对应的结构
结构太长一图没法截,接着看下图:
自己对着看吧。
示例代码
main.cpp
#include "dialog.h"#include <QApplication>int main(int argc, char *argv[])
{QApplication a(argc, argv);Dialog w;w.show();return a.exec();
}
dialog.h
#ifndef DIALOG_H
#define DIALOG_H#include "form1.h"
#include "form2.h"
#include <QDialog>QT_BEGIN_NAMESPACE
namespace Ui { class Dialog; }
QT_END_NAMESPACEclass Dialog : public QDialog
{Q_OBJECTpublic:Dialog(QWidget *parent = nullptr);~Dialog();
private:void initInterfaceParameters();
private slots:void on_pushButton_clicked();void on_pushButton_2_clicked();private:Ui::Dialog *ui;Form1* m_pForm1;Form2* m_pForm2;};
#endif // DIALOG_H
dialog.cpp
#include "dialog.h"
#include "ui_dialog.h"Dialog::Dialog(QWidget *parent): QDialog(parent), ui(new Ui::Dialog)
{ui->setupUi(this);m_pForm1 = new Form1;m_pForm2 = new Form2;initInterfaceParameters();
}Dialog::~Dialog()
{delete ui;
}void Dialog::initInterfaceParameters()
{FaultFindParamsInfo struParams;struParams.strBand = "GSM900";struParams.fBsPOwer = -45.0;struParams.msPower.nPower = 35;struParams.msPower.powerLevel = 4;struParams.bcchChannel.channel = 63;struParams.bcchChannel.rx = 1.5;struParams.bcchChannel.tx = 1.5;m_pForm1->setValues(struParams);FaultFindLimit struLimit;struLimit.struPower900.fLevel1 = 3.0;struLimit.struPower900.fLevel2 = 4.0;struLimit.struPower900.fmax = 2.0;struLimit.struPower1800_1900.fLevel1 = 3.0;struLimit.struPower1800_1900.fLevel2 = 4.0;struLimit.struPower1800_1900.fLevel3 = 5.0;struLimit.struPower1800_1900.fmax = 2.0;struLimit.nFreqErr900 = 90;struLimit.nFreqErr1800_1900 = 180;struLimit.nPeakPhase = 20;struLimit.fRmsPhase = 5.0;struLimit.fBerFer = 2.0;m_pForm2->setLimits(struLimit);
}void Dialog::on_pushButton_clicked()
{if(!m_pForm2->isHidden()){m_pForm2->hide();}m_pForm1->show();
}void Dialog::on_pushButton_2_clicked()
{if(!m_pForm1->isHidden()){m_pForm1->hide();}m_pForm2->show();
}
form1.h
#ifndef FORM1_H
#define FORM1_H#include "customlineedit.h"
#include <QWidget>namespace Ui {
class Form1;
}class Form1 : public QWidget
{Q_OBJECTpublic:explicit Form1(QWidget *parent = nullptr);~Form1();void setValues(const FaultFindParamsInfo ¶ms);
protected:void keyPressEvent(QKeyEvent *event) override;
signals:void signWarningOutOfRange(const QString& strPromptText);
private slots:void slotUpdateMsPower(const int& nGap,const bool& bIsAdd);
private:void initInterface();void setLabelDisplayText();void setLineEditTextAlignment();void setMaxInputCharacter();void connectSignalSlot();void setLineEditRangeAndType(const QString& strBand);void setRange(QVector<FaultFindParamsRange>& rangeVec,const int& nLow,const int& nUp);void setLineEditRangeAndTypeByGsm900();void setLineEditRangeAndTypeByGsm1800();void setLineEditRangeAndTypeByGsm1900();void initFormat(QString& strText,const int& digit);void saveCtrl();void setCurLineEditIsSelected(const bool& bIsSelected);CustomLineEdit* curSelectedLineEditByIndex();void setEditedTextAfterEditing();
private:Ui::Form1 *ui;QMap<int,CustomLineEdit*> m_pCustomLineEditMap;int m_nCurSelectedIndex;
};#endif // FORM1_H
form1.cpp
#include "form1.h"
#include "ui_form1.h"#include <QKeyEvent>#define SPEECH_BSPOWER_SPEC 1
Form1::Form1(QWidget *parent) :QWidget(parent),m_nCurSelectedIndex(0),ui(new Ui::Form1)
{ui->setupUi(this);initInterface();saveCtrl();
}Form1::~Form1()
{delete ui;
}void Form1::setValues(const FaultFindParamsInfo ¶ms)
{ui->labelBand->setText(params.strBand);QString strChannel = QString::number(params.bcchChannel.channel);initFormat(strChannel,4);ui->lineEditChannel->setText(strChannel);ui->lineEditBsPowerLevel->setText(QString::number(params.fBsPOwer,'f',SPEECH_BSPOWER_SPEC));QString strMsPower = QString::number(params.msPower.powerLevel);initFormat(strMsPower,2);ui->lineEditMsPowerLevel->setText(strMsPower);ui->labelPower->setText(QString::number(params.msPower.nPower));QString strRx = QString::number(params.bcchChannel.rx,'f',SPEECH_BSPOWER_SPEC);initFormat(strRx,5);ui->lineEditRX->setText(strRx);QString strTx = QString::number(params.bcchChannel.tx,'f',SPEECH_BSPOWER_SPEC);initFormat(strTx,5);ui->lineEditTX->setText(strTx);setLineEditRangeAndType(params.strBand);ui->lineEditChannel->setSelected(true,ui->lineEditChannel->text().length());
}void Form1::keyPressEvent(QKeyEvent *event)
{if(event->key() == Qt::Key_Up){setEditedTextAfterEditing();setCurLineEditIsSelected(false);if(m_nCurSelectedIndex > 0){m_nCurSelectedIndex -= 1;}else{m_nCurSelectedIndex = m_pCustomLineEditMap.lastKey();}setCurLineEditIsSelected(true);}else if(event->key() == Qt::Key_Down){setEditedTextAfterEditing();setCurLineEditIsSelected(false);if(m_nCurSelectedIndex < m_pCustomLineEditMap.lastKey()){m_nCurSelectedIndex += 1;}else{m_nCurSelectedIndex = 0;}setCurLineEditIsSelected(true);}else{QWidget::keyPressEvent(event);}
}void Form1::slotUpdateMsPower(const int& nGap,const bool& bIsAdd)
{QString strMsPower = ui->labelPower->text();int nStep = 2 * nGap;int nPower;if(bIsAdd){nPower = strMsPower.toInt() + nStep;}else{nPower = strMsPower.toInt() - nStep;}ui->labelPower->setText(QString::number(nPower)