File indexing completed on 2024-04-28 03:53:11
0001 #include "klineedittest.h" 0002 0003 #include <QApplication> 0004 #include <QDebug> 0005 #include <QHBoxLayout> 0006 #include <QLabel> 0007 #include <QPushButton> 0008 #include <QRegularExpression> 0009 #include <QRegularExpressionValidator> 0010 #include <QTimer> 0011 0012 #include <klineedit.h> 0013 0014 KLineEditTest::KLineEditTest(QWidget *widget) 0015 : QWidget(widget) 0016 { 0017 QVBoxLayout *layout = new QVBoxLayout(this); 0018 0019 QStringList list; 0020 list << QStringLiteral("Tree") << QStringLiteral("Suuupa") << QStringLiteral("Stroustrup") << QStringLiteral("Stone") << QStringLiteral("Slick") 0021 << QStringLiteral("Slashdot") << QStringLiteral("Send") << QStringLiteral("Peables") << QStringLiteral("Mankind") << QStringLiteral("Ocean") 0022 << QStringLiteral("Chips") << QStringLiteral("Computer") << QStringLiteral("Sandworm") << QStringLiteral("Sandstorm") << QStringLiteral("Chops"); 0023 list.sort(); 0024 0025 m_lineedit = new KLineEdit(this); 0026 m_lineedit->setObjectName(QStringLiteral("klineedittest")); 0027 m_lineedit->completionObject()->setItems(list); 0028 m_lineedit->setSqueezedTextEnabled(true); 0029 m_lineedit->setClearButtonEnabled(true); 0030 connect(m_lineedit, &QLineEdit::returnPressed, this, &KLineEditTest::slotReturnPressed); 0031 connect(m_lineedit, &KLineEdit::returnKeyPressed, this, &KLineEditTest::slotReturnKeyPressed); 0032 0033 QHBoxLayout *restrictedHBox = new QHBoxLayout; 0034 m_restrictedLine = new KLineEdit(this); 0035 QRegularExpression regex(QStringLiteral("[aeiouyé]*")); 0036 QRegularExpressionValidator *validator = new QRegularExpressionValidator(regex, m_restrictedLine); 0037 m_restrictedLine->setValidator(validator); 0038 // connect(m_restrictedLine, SIGNAL(invalidChar(int)), this, SLOT(slotInvalidChar(int))); 0039 connect(m_restrictedLine, &QLineEdit::returnPressed, this, &KLineEditTest::slotReturnPressed); 0040 connect(m_restrictedLine, &KLineEdit::returnKeyPressed, this, &KLineEditTest::slotReturnKeyPressed); 0041 restrictedHBox->addWidget(new QLabel(QStringLiteral("Vowels only:"), this)); 0042 restrictedHBox->addWidget(m_restrictedLine); 0043 m_invalidCharLabel = new QLabel(this); 0044 restrictedHBox->addWidget(m_invalidCharLabel); 0045 0046 // horizontal button layout 0047 m_btnExit = new QPushButton(QStringLiteral("E&xit"), this); 0048 connect(m_btnExit, &QAbstractButton::clicked, this, &KLineEditTest::quitApp); 0049 0050 m_btnReadOnly = new QPushButton(QStringLiteral("&Read Only"), this); 0051 m_btnReadOnly->setCheckable(true); 0052 connect(m_btnReadOnly, &QAbstractButton::toggled, this, &KLineEditTest::slotReadOnly); 0053 0054 m_btnPassword = new QPushButton(QStringLiteral("&Password"), this); 0055 m_btnPassword->setCheckable(true); 0056 connect(m_btnPassword, &QAbstractButton::toggled, this, &KLineEditTest::slotPassword); 0057 0058 m_btnEnable = new QPushButton(QStringLiteral("Dis&able"), this); 0059 m_btnEnable->setCheckable(true); 0060 connect(m_btnEnable, &QAbstractButton::toggled, this, &KLineEditTest::slotEnable); 0061 0062 m_btnHide = new QPushButton(QStringLiteral("Hi&de"), this); 0063 connect(m_btnHide, &QAbstractButton::clicked, this, &KLineEditTest::slotHide); 0064 0065 m_btnPlaceholderText = new QPushButton(QStringLiteral("Place Holder Text"), this); 0066 m_btnPlaceholderText->setCheckable(true); 0067 connect(m_btnPlaceholderText, &QAbstractButton::toggled, this, &KLineEditTest::slotPlaceholderText); 0068 0069 QPushButton *btnStyle = new QPushButton(QStringLiteral("Stylesheet"), this); 0070 connect(btnStyle, &QAbstractButton::clicked, this, &KLineEditTest::slotSetStyleSheet); 0071 0072 QHBoxLayout *buttonLayout = new QHBoxLayout(); 0073 buttonLayout->addWidget(m_btnExit); 0074 buttonLayout->addWidget(m_btnReadOnly); 0075 buttonLayout->addWidget(m_btnPassword); 0076 buttonLayout->addWidget(m_btnEnable); 0077 buttonLayout->addWidget(m_btnHide); 0078 buttonLayout->addWidget(m_btnPlaceholderText); 0079 buttonLayout->addWidget(btnStyle); 0080 0081 layout->addWidget(m_lineedit); 0082 layout->addLayout(restrictedHBox); 0083 layout->addLayout(buttonLayout); 0084 setWindowTitle(QStringLiteral("KLineEdit Unit Test")); 0085 } 0086 0087 KLineEditTest::~KLineEditTest() 0088 { 0089 } 0090 0091 void KLineEditTest::quitApp() 0092 { 0093 qApp->closeAllWindows(); 0094 } 0095 0096 void KLineEditTest::slotSetStyleSheet() 0097 { 0098 m_lineedit->setStyleSheet(QStringLiteral("QLineEdit{ background-color:#baf9ce }")); 0099 } 0100 0101 void KLineEditTest::show() 0102 { 0103 if (m_lineedit->isHidden()) { 0104 m_lineedit->show(); 0105 } 0106 0107 m_btnHide->setEnabled(true); 0108 0109 QWidget::show(); 0110 } 0111 0112 void KLineEditTest::slotReturnPressed() 0113 { 0114 qDebug() << "Return pressed"; 0115 } 0116 0117 void KLineEditTest::slotReturnKeyPressed(const QString &text) 0118 { 0119 qDebug() << "Return pressed: " << text; 0120 } 0121 0122 void KLineEditTest::resultOutput(const QString &text) 0123 { 0124 qDebug() << "KlineEditTest Debug: " << text; 0125 } 0126 0127 void KLineEditTest::slotReadOnly(bool ro) 0128 { 0129 m_lineedit->setReadOnly(ro); 0130 QString text = (ro) ? "&Read Write" : "&Read Only"; 0131 m_btnReadOnly->setText(text); 0132 } 0133 0134 void KLineEditTest::slotPassword(bool pw) 0135 { 0136 m_lineedit->setEchoMode(pw ? QLineEdit::Password : QLineEdit::Normal); 0137 QString text = (pw) ? "&Normal Text" : "&Password"; 0138 m_btnPassword->setText(text); 0139 } 0140 0141 void KLineEditTest::slotEnable(bool enable) 0142 { 0143 m_lineedit->setEnabled(!enable); 0144 QString text = (enable) ? "En&able" : "Dis&able"; 0145 m_btnEnable->setText(text); 0146 } 0147 0148 void KLineEditTest::slotPlaceholderText(bool click) 0149 { 0150 if (click) { 0151 m_lineedit->setText(QLatin1String("")); // Clear before to add message 0152 m_lineedit->setPlaceholderText(QStringLiteral("Click in this lineedit")); 0153 } 0154 } 0155 0156 void KLineEditTest::slotHide() 0157 { 0158 m_lineedit->hide(); 0159 m_btnHide->setEnabled(false); 0160 m_lineedit->setText( 0161 "My dog ate the homework, whaaaaaaaaaaaaaaaaaaaaaaa" 0162 "aaaaaaaaaaaaaaaaaaaaaaaaa! I want my mommy!"); 0163 QTimer::singleShot(1000, this, &KLineEditTest::show); 0164 } 0165 0166 void KLineEditTest::slotInvalidChar(int key) 0167 { 0168 m_invalidCharLabel->setText(QStringLiteral("Invalid char: %1").arg(key)); 0169 } 0170 0171 int main(int argc, char **argv) 0172 { 0173 QApplication a(argc, argv); 0174 KLineEditTest *t = new KLineEditTest(); 0175 // t->lineEdit()->setTrapReturnKey( true ); 0176 // t->lineEdit()->completionBox()->setTabHandling( false ); 0177 t->lineEdit()->setSqueezedTextEnabled(true); 0178 t->lineEdit()->setText( 0179 "This is a really really really really really really " 0180 "really really long line because I am a talkative fool!" 0181 "I mean ... REALLY talkative. If you don't believe me, ask my cousin."); 0182 t->show(); 0183 return a.exec(); 0184 } 0185 0186 #include "moc_klineedittest.cpp"