您好,登錄后才能下訂單哦!
學(xué)習(xí)了上一篇博客關(guān)于QLineEdit的一些重要的成員函數(shù)和信號的用法之后,我們寫一個小程序來熟練下這些函數(shù).
這里是程序完成后的圖片.
首先,我們現(xiàn)在ui設(shè)計師里拖拽出以下的布局:
注意箭頭處還有個QLabel部件.
以下是”c.cpp”下的代碼:
#include "c.h"c::c(QWidget *parent) : QWidget(parent) { ui.setupUi(this); //設(shè)置標(biāo)題為"QLineEdit" this->setWindowTitle("QQ"); //設(shè)置圖標(biāo). this->setWindowIcon(QIcon("pixmap.jpg")); //設(shè)置為伙伴. ui.accountLabel->setBuddy(ui.accountLineEdit); ui.passwordLabel->setBuddy(ui.passwordLineEdit); //設(shè)置頭像. ui.pixmapLabel->setPixmap(QPixmap("pixmap.jpg")); //讓label完整的放下圖片.根據(jù)比例縮減. ui.pixmapLabel->setScaledContents(true); //設(shè)置密碼輸入框的顯示模式為:密碼顯示模式. ui.passwordLineEdit->setEchoMode(QLineEdit::Password); //設(shè)置激活兩個輸入框的清空按鈕. ui.accountLineEdit->setClearButtonEnabled(true); ui.passwordLineEdit->setClearButtonEnabled(true); //設(shè)置兩個輸入框的最大長度 ui.accountLineEdit->setMaxLength(10); ui.passwordLineEdit->setMaxLength(10); //設(shè)置賬號輸入框允許拖拽. ui.accountLineEdit->setDragEnabled(true); //設(shè)置密碼輸入框不接受拖拽的文本. ui.passwordLineEdit->setAcceptDrops(false); //設(shè)置兩個輸入框的占位符. ui.accountLineEdit->setPlaceholderText(QString::fromLocal8Bit("必須為純數(shù)字")); ui.passwordLineEdit->setPlaceholderText(QString::fromLocal8Bit("兩位字母+純數(shù)字")); //設(shè)置賬號輸入框的整數(shù)驗證器,并且設(shè)定輸入的范圍. QIntValidator *account = new QIntValidator; account->setBottom(0); ui.accountLineEdit->setValidator(account); //設(shè)置密碼輸入框的驗證器,用了正則表達(dá)式來規(guī)定:前面1-2個必須是字母,后面8-9個必須是0-9內(nèi)的數(shù)字. QRegExp password("[A-Za-z]{2}[0-9]{8,9}"); ui.passwordLineEdit->setValidator(new QRegExpValidator(password, this)); //設(shè)置密碼輸入框中,按鼠標(biāo)右鍵無菜單顯示.和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的用法,就沒有實現(xiàn)注冊,登錄等功能.所以這是簡單的登陸下. 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”內(nèi)的代碼:
#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”內(nèi)的代碼:
#include "c.h"#include <QtWidgets/QApplication>int main(int argc, char *argv[]) { QApplication a(argc, argv); c w; w.show(); return a.exec(); }
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。