Advertisement
Guest User

Untitled

a guest
Feb 21st, 2017
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.42 KB | None | 0 0
  1. QCustomLineEdit::QCustomLineEdit(QWidget *parent) :
  2. QLineEdit(parent)
  3. {
  4. connect(this, SIGNAL(textChanged(QString)), this, SLOT(onTextChanged(QString)));
  5. connect(this, SIGNAL(cursorPositionChanged(int,int)), this, SLOT(onCursorPositionChanged(int,int)));
  6.  
  7. setEchoMode(QLineEdit::Password); // Echo mode in your case..
  8.  
  9. m_echoMode = echoMode(); // Member variable to store original echo mode..
  10. m_placeHolderText = "Password"; // Member variable..
  11. m_isPlaceHolderActive = true; // Member varible..
  12.  
  13. // Default case..
  14. setPlaceholderText("");
  15. setStyleSheet("QCustomLineEdit{color: gray;}");
  16. setEchoMode(QLineEdit::Normal);
  17. setText(__placeHolderText);
  18. }
  19.  
  20. void QCustomLineEdit::keyPressEvent(QKeyEvent *e)
  21. {
  22. if(m_isPlaceHolderActive)
  23. {
  24. if(e->key() == Qt::Key_Delete || e->key() == Qt::Key_Backspace)
  25. e->accept();
  26. else
  27. QLineEdit::keyPressEvent(e);
  28.  
  29. return;
  30. }
  31.  
  32. QLineEdit::keyPressEvent(e);
  33. }
  34.  
  35. void QCustomLineEdit::onCursorPositionChanged(int /*oldPos*/, int newPos)
  36. {
  37. if(m_isPlaceHolderActive)
  38. {
  39. if(newPos != 0)
  40. setCursorPosition(0);
  41. }
  42. }
  43.  
  44. void QCustomLineEdit::onTextChanged(const QString &text)
  45. {
  46. if(m_isPlaceHolderActive)
  47. {
  48. if(text.compare(m_placeHolderText) != 0)
  49. {
  50. m_isPlaceHolderActive = false;
  51.  
  52. // Remove the 'placeHolderText' from 'text' itself..
  53. QString temp = text;
  54. temp = temp.mid(0, text.lastIndexOf(m_placeHolderText));
  55.  
  56. setStyleSheet("QCustomLineEdit{color: black;}");
  57. setEchoMode(m_echoMode);
  58. setText(temp);
  59. }
  60. else
  61. {
  62. setEchoMode(QLineEdit::Normal);
  63. setText(m_placeHolderText);
  64. setStyleSheet("QCustomLineEdit{color: gray;}");
  65. setCursorPosition(0);
  66. }
  67. }
  68. else
  69. {
  70. if(text.isEmpty())
  71. {
  72. m_isPlaceHolderActive = true;
  73. setStyleSheet("QCustomLineEdit{color: gray;}");
  74. setEchoMode(QLineEdit::Normal);
  75. setText(m_placeHolderText);
  76. }
  77. }
  78. }
  79.  
  80. ui->lineEdit->setPlaceholderText("password");
  81. ui->lineEdit->setReadOnly(1);
  82.  
  83. ui->lineEdit->setText("");
  84. ui->lineEdit->setEchoMode(QLineEdit::Password);
  85. ui->lineEdit->setReadOnly(0);
  86.  
  87. self.searchEditText = QtGui.QLineEdit()
  88. self.searchEditText.setPlaceholderText("Search for word")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement