您好,登錄后才能下訂單哦!
學習了上一篇博客關于QLineEdit的一些重要的成員函數和信號的用法之后,我們寫一個小程序來熟練下這些函數.
這里是程序完成后的圖片.
首先,我們現在ui設計師里拖拽出以下的布局:
注意箭頭處還有個QLabel部件.
以下是”c.cpp”下的代碼:
#include "c.h"c::c(QWidget *parent) : QWidget(parent) { ui.setupUi(this); //設置標題為"QLineEdit" this->setWindowTitle("QQ"); //設置圖標. this->setWindowIcon(QIcon("pixmap.jpg")); //設置為伙伴. ui.accountLabel->setBuddy(ui.accountLineEdit); ui.passwordLabel->setBuddy(ui.passwordLineEdit); //設置頭像. ui.pixmapLabel->setPixmap(QPixmap("pixmap.jpg")); //讓label完整的放下圖片.根據比例縮減. ui.pixmapLabel->setScaledContents(true); //設置密碼輸入框的顯示模式為:密碼顯示模式. ui.passwordLineEdit->setEchoMode(QLineEdit::Password); //設置激活兩個輸入框的清空按鈕. ui.accountLineEdit->setClearButtonEnabled(true); ui.passwordLineEdit->setClearButtonEnabled(true); //設置兩個輸入框的最大長度 ui.accountLineEdit->setMaxLength(10); ui.passwordLineEdit->setMaxLength(10); //設置賬號輸入框允許拖拽. ui.accountLineEdit->setDragEnabled(true); //設置密碼輸入框不接受拖拽的文本. ui.passwordLineEdit->setAcceptDrops(false); //設置兩個輸入框的占位符. ui.accountLineEdit->setPlaceholderText(QString::fromLocal8Bit("必須為純數字")); ui.passwordLineEdit->setPlaceholderText(QString::fromLocal8Bit("兩位字母+純數字")); //設置賬號輸入框的整數驗證器,并且設定輸入的范圍. QIntValidator *account = new QIntValidator; account->setBottom(0); ui.accountLineEdit->setValidator(account); //設置密碼輸入框的驗證器,用了正則表達式來規定:前面1-2個必須是字母,后面8-9個必須是0-9內的數字. QRegExp password("[A-Za-z]{2}[0-9]{8,9}"); ui.passwordLineEdit->setValidator(new QRegExpValidator(password, this)); //設置密碼輸入框中,按鼠標右鍵無菜單顯示.和QQ一樣.有心的小伙伴可以去試一下. ui.passwordLineEdit->setContextMenuPolicy(Qt::NoContextMenu); //但是對于Ctrl+V粘貼的快捷操作還沒有屏蔽.有興趣的可以去自己試試.這里不展開了. //連接信號與槽. connect(ui.showAccountButton, SIGNAL(clicked()), this, SLOT(showAccountNameSlot())); connect(ui.showPasswordButton, SIGNAL(clicked()), this, SLOT(showPasswordSlot())); connect(ui.pushButton, SIGNAL(clicked()), this, SLOT(showEntryDialogSlot())); connect(ui.passwordLineEdit, SIGNAL(returnPressed()), ui.pushButton, SIGNAL(clicked())); } c::~c() { }void c::showAccountNameSlot() { QMessageBox::information(this, QString::fromLocal8Bit("賬號:"), ui.accountLineEdit->text()); }void c::showPasswordSlot() { //注意text()返回的是本身的文本,而displayText()返回的是顯示的文本. QMessageBox::information(this, QString::fromLocal8Bit("密碼:"), ui.passwordLineEdit->text()); }void c::showEntryDialogSlot() { //這里只是為了演示QLineEdit的用法,就沒有實現注冊,登錄等功能.所以這是簡單的登陸下. if ((ui.accountLineEdit->text() == "123456") && (ui.passwordLineEdit->text() == "qw123456")) { QMessageBox::information(this, QString::fromLocal8Bit("登錄"), QString::fromLocal8Bit("登錄成功")); } else { QMessageBox::warning(this, QString::fromLocal8Bit("登錄"), QString::fromLocal8Bit("登錄失敗")); } }12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
以下是”c.h”內的代碼:
#ifndef C_H#define C_H#include <QtWidgets/QWidget>#include "ui_c.h"#include <QMessageBox>#include <QLineEdit>#include <QLabel>#include <QPushButton>#include <QValidator>#include <QRegExp>#include <QMessageBox>#include <QPixmap>class c : public QWidget{ Q_OBJECTpublic: c(QWidget *parent = 0); ~c();private slots: void showAccountNameSlot(); void showPasswordSlot(); void showEntryDialogSlot();private: Ui::cClass ui; };#endif // C_H1234567891011121314151617181920212223242526272829303132
最后是”main.cpp”內的代碼:
#include "c.h"#include <QtWidgets/QApplication>int main(int argc, char *argv[]) { QApplication a(argc, argv); c w; w.show(); return a.exec(); }
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。