Qt之自定义输入框(度分秒、经纬度、格式化显示)-No21-QtCustomEdit

相关资料:

http://www.manongjc.com/detail/15-grpefyhtwdpbehh.html   Qt自定义文本输入框实现支持输入度分秒和度两种格式(简易无限制输入)

PS:重要的文件我用粗体标注了。

 

实例代码:

.pro

 1 QT       += core gui
 2 
 3 greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
 4 
 5 CONFIG += c++11
 6 
 7 # The following define makes your compiler emit warnings if you use
 8 # any Qt feature that has been marked deprecated (the exact warnings
 9 # depend on your compiler). Please consult the documentation of the
10 # deprecated API in order to know how to port your code away from it.
11 DEFINES += QT_DEPRECATED_WARNINGS
12 
13 # You can also make your code fail to compile if it uses deprecated APIs.
14 # In order to do so, uncomment the following line.
15 # You can also select to disable deprecated APIs only up to a certain version of Qt.
16 #DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000    # disables all the APIs deprecated before Qt 6.0.0
17 
18 SOURCES += \
19     CustomEdit.cpp \
20     main.cpp \
21     mainwindow.cpp
22 
23 HEADERS += \
24     CustomEdit.h \
25     mainwindow.h
26 
27 FORMS += \
28     mainwindow.ui
29 
30 # Default rules for deployment.
31 qnx: target.path = /tmp/$${TARGET}/bin
32 else: unix:!android: target.path = /opt/$${TARGET}/bin
33 !isEmpty(target.path): INSTALLS += target
View Code

main.cpp

 1 #include "mainwindow.h"
 2 
 3 #include <QApplication>
 4 
 5 int main(int argc, char *argv[])
 6 {
 7     QApplication a(argc, argv);
 8     MainWindow w;
 9     w.show();
10     return a.exec();
11 }
View Code

mainwindow.h

 1 #ifndef MAINWINDOW_H
 2 #define MAINWINDOW_H
 3 
 4 #include <QMainWindow>
 5 #include "CustomEdit.h"
 6 
 7 QT_BEGIN_NAMESPACE
 8 namespace Ui { class MainWindow; }
 9 QT_END_NAMESPACE
10 
11 class MainWindow : public QMainWindow
12 {
13     Q_OBJECT
14 
15 public:
16     MainWindow(QWidget *parent = nullptr);
17     ~MainWindow();
18 
19 private:
20     Ui::MainWindow *ui;
21 };
22 #endif // MAINWINDOW_H
View Code

mainwindow.cpp

 1 #include "mainwindow.h"
 2 #include "ui_mainwindow.h"
 3 
 4 MainWindow::MainWindow(QWidget *parent)
 5     : QMainWindow(parent)
 6     , ui(new Ui::MainWindow)
 7 {
 8     ui->setupUi(this);
 9 
10     setWindowTitle(QStringLiteral("Qt之自定义输入框(度分秒、经纬度、格式化显示)"));
11 
12     TCustomEdit *m_pCustomEdit = new TCustomEdit(this);
13     m_pCustomEdit->setPlaceholderText(QStringLiteral("普通输入框"));
14     m_pCustomEdit->setGeometry(ui->textEdit->x(), ui->textEdit->y() + ui->textEdit->height(), 200, 30);
15 
16     TCustomEdit *m_pCustomEdit1 = new TCustomEdit(this);
17     m_pCustomEdit1->setMode(1);
18     m_pCustomEdit1->setPlaceholderText(QStringLiteral("度分秒转经纬度"));
19     m_pCustomEdit1->setGeometry(m_pCustomEdit->x(), m_pCustomEdit->y() + m_pCustomEdit->height(), 200, 30);
20 
21     TCustomEdit *m_pCustomEdit2 = new TCustomEdit(this);
22     m_pCustomEdit2->setMode(2);
23     m_pCustomEdit2->setPlaceholderText(QStringLiteral("经纬转度分秒"));
24     m_pCustomEdit2->setGeometry(m_pCustomEdit1->x(), m_pCustomEdit1->y() + m_pCustomEdit1->height(), 200, 30);
25 
26     TCustomEdit *m_pCustomEdit3 = new TCustomEdit(this);
27     m_pCustomEdit3->setMode(3);
28     m_pCustomEdit3->setPlaceholderText(QStringLiteral("', '、' '号分隔"));
29     m_pCustomEdit3->setGeometry(m_pCustomEdit2->x(), m_pCustomEdit2->y() + m_pCustomEdit2->height(), 200, 30);
30     }
31 
32 MainWindow::~MainWindow()
33 {
34     delete ui;
35 }
View Code

mainwindow.ui

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <ui version="4.0">
 3  <class>MainWindow</class>
 4  <widget class="QMainWindow" name="MainWindow">
 5   <property name="geometry">
 6    <rect>
 7     <x>0</x>
 8     <y>0</y>
 9     <width>601</width>
10     <height>291</height>
11    </rect>
12   </property>
13   <property name="windowTitle">
14    <string>MainWindow</string>
15   </property>
16   <widget class="QWidget" name="centralwidget">
17    <widget class="QTextEdit" name="textEdit">
18     <property name="geometry">
19      <rect>
20       <x>10</x>
21       <y>0</y>
22       <width>421</width>
23       <height>61</height>
24      </rect>
25     </property>
26     <property name="markdown">
27      <string>29°4416.2529.73784595   103°3511.02103.5863933
28 
29 </string>
30     </property>
31     <property name="html">
32      <string>&lt;!DOCTYPE HTML PUBLIC &quot;-//W3C//DTD HTML 4.0//EN&quot; &quot;http://www.w3.org/TR/REC-html40/strict.dtd&quot;&gt;
33 &lt;html&gt;&lt;head&gt;&lt;meta name=&quot;qrichtext&quot; content=&quot;1&quot; /&gt;&lt;style type=&quot;text/css&quot;&gt;
34 p, li { white-space: pre-wrap; }
35 &lt;/style&gt;&lt;/head&gt;&lt;body style=&quot; font-family:'SimSun'; font-size:9pt; font-weight:400; font-style:normal;&quot;&gt;
36 &lt;p style=&quot; margin-top:6px; margin-bottom:6px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;&quot;&gt;29°4416.2529.73784595   103°3511.02103.5863933&lt;/p&gt;&lt;/body&gt;&lt;/html&gt;</string>
37     </property>
38     <property name="placeholderText">
39      <string/>
40     </property>
41    </widget>
42   </widget>
43   <widget class="QMenuBar" name="menubar">
44    <property name="geometry">
45     <rect>
46      <x>0</x>
47      <y>0</y>
48      <width>601</width>
49      <height>22</height>
50     </rect>
51    </property>
52   </widget>
53   <widget class="QStatusBar" name="statusbar"/>
54  </widget>
55  <resources/>
56  <connections/>
57 </ui>
View Code

CustomEdit.h

 1 #ifndef CUSTOMEDIT_H
 2 #define CUSTOMEDIT_H
 3 
 4 #include <QLineEdit>
 5 
 6 class TCustomEdit : public QLineEdit
 7 {
 8   Q_OBJECT
 9 
10 public:
11     TCustomEdit(QWidget *parent = 0);
12     ~TCustomEdit();
13 
14     void setMode(int mode);
15 private:
16     void focusOutEvent(QFocusEvent *event) Q_DECL_OVERRIDE;
17 
18     int m_nMode = 0;
19 };
20 
21 #endif // CUSTOMEDIT_H
View Code

CustomEdit.cpp

 1 #include "CustomEdit.h"
 2 
 3 TCustomEdit::TCustomEdit(QWidget *parent)
 4  : QLineEdit(parent)
 5 {
 6 }
 7 
 8 TCustomEdit::~TCustomEdit()
 9 {
10 }
11 
12 void TCustomEdit::setMode(int mode)
13 {
14     m_nMode = mode;
15 }
16 
17 void TCustomEdit::focusOutEvent(QFocusEvent *event)
18 {
19     QLineEdit::focusOutEvent(event);
20 
21     switch (m_nMode)
22     {
23     case 0:
24         break;
25     case 1:  // 显示成经纬度(度分秒转经纬度)
26     {
27         QString curText = text();
28         QString str = curText.replace(QStringLiteral("°"), ",").replace(QStringLiteral(""), ",").replace(QStringLiteral(""), "");
29         QStringList listStr = str.split(",");
30         if (listStr.size() != 3) return;
31         bool isD = false, isF = false, isM = false;
32         float d = listStr[0].toFloat(&isD);
33         float f = listStr[1].toFloat(&isF);
34         float m = listStr[2].toFloat(&isM);
35         if (!isD || !isF || !isM) return;
36         setText(QString::number(d + f / 60.0 + m / 3600.0));
37         break;
38     }
39     case 2:// 显示成度分秒(经纬转度分秒)
40     {
41         QString curText = text();
42         QString str = curText.replace(QStringLiteral("."), ",");
43         QStringList listStr = str.split(",");
44         if (listStr.size() != 2) return;
45         bool isZ = false, isX = false;
46         int d = listStr[0].toInt(&isZ);
47         float f = listStr[1].toFloat(&isX);
48         if (!isZ || !isX) return;
49         //
50         for (int i = 0 ; i < listStr[1].length(); ++i)
51             f = f/10;
52         int nF = QString::number(f*60).split(".")[0].toInt();
53         //
54         float m = QString::number(f*60).split(".")[1].toFloat();
55         for (int i = 0 ; i < QString::number(f*60).split(".")[1].length(); ++i)
56             m = m/10;
57         int nM = QString::number(m*60).split(".")[0].toInt();
58 
59         setText(QStringLiteral("%1°%2′%3″").arg(d).arg(nF).arg(nM));
60         break;
61     }
62     case 3:// 显示成度分秒("," " "号分隔)
63     {
64         QString curText = text().trimmed();
65         QString str = curText.replace(QStringLiteral(","), ",").replace(QStringLiteral(" "), ",");
66         QStringList listStr = str.split(",");
67         if (listStr.size() != 3) return;
68         bool isD = false, isF = false, isM = false;
69         int d = listStr[0].toInt(&isD);
70         int f = listStr[1].toInt(&isF);
71         float m = listStr[2].toFloat(&isM);
72         if (!isD || !isF || !isM) return;
73         setText(QStringLiteral("%1°%2′%3″").arg(d).arg(f).arg(m));
74         break;
75     }
76 
77 
78     }
79 
80 }
View Code

 

 

 

posted on 2022-12-15 15:33  疯狂delphi  阅读(333)  评论(0编辑  收藏  举报

导航