QtLineEditor.cpp 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. #include "QtLineEditor.h"
  2. #include "preHeader.h"
  3. QtLineEditor::QtLineEditor(QWidget*parent)
  4. : QLineEdit(parent)
  5. {
  6. //set a slot to process the text changed
  7. connect(this, &QLineEdit::textChanged, this, &QtLineEditor::OnTextChanged);
  8. //set a slot to process the text edited
  9. connect(this, &QLineEdit::textChanged, this, &QtLineEditor::OnTextEdited);
  10. //init the target
  11. m_nTarget = nullptr;
  12. m_strTarget = nullptr;
  13. }
  14. QtLineEditor::~QtLineEditor()
  15. {
  16. }
  17. void QtLineEditor::init(QString strText, bool isIntOrNot, int nMax)
  18. {
  19. //set the text
  20. this->setText(strText);
  21. //set a branch to process the int or string
  22. if (isIntOrNot)
  23. {
  24. //set the flags
  25. m_bIsInt = true;
  26. //set the limit
  27. this->setMaxLength(5);
  28. //set the max value
  29. this->setValidator(new QIntValidator(-nMax, nMax, this));
  30. }
  31. else
  32. {
  33. //set the flags
  34. m_bIsInt = false;
  35. //set the limit
  36. this->setMaxLength(15);
  37. //set the max value
  38. this->setValidator(new QRegExpValidator(QRegExp("[0-9a-zA-Z]+$"), this));
  39. }
  40. }
  41. void QtLineEditor::SetEditTarget(int* nTarget)
  42. {
  43. if (m_bIsInt)
  44. {
  45. //init the target
  46. m_nTarget = nTarget;
  47. }
  48. }
  49. void QtLineEditor::SetEditTarget(QString* strTarget)
  50. {
  51. if (!m_bIsInt)
  52. {
  53. //init the target
  54. m_strTarget = strTarget;
  55. }
  56. }
  57. void QtLineEditor::OnTextChanged(const QString & text)
  58. {
  59. if (m_nTarget == nullptr && m_strTarget == nullptr )
  60. {
  61. return;
  62. }
  63. //set a branch to process the int or string
  64. if (m_bIsInt)
  65. {
  66. //set the value
  67. *m_nTarget = text.toInt();
  68. }
  69. else
  70. {
  71. //set the value
  72. *m_strTarget = text;
  73. }
  74. }
  75. void QtLineEditor::OnTextEdited(const QString& text)
  76. {
  77. //set a branch to process the int or string
  78. if (m_strTarget == nullptr)
  79. {
  80. return;
  81. }
  82. if (!m_bIsInt)
  83. {
  84. }
  85. }