Qt 是目前最先進、最完整的跨平臺C++開發工具。它不僅完全實現了一次編寫,所有平臺無差別運行,更提供了幾乎所有開發過程中需要用到的工具。如今,Qt已被運用於超過70個行業、數千家企業,支持數百萬設備及應用。
Line Edits(行編輯)示例演示了QLineEdit的多種使用方式,並顯示了各種屬性和驗證器對用戶提供的輸入和輸出的影響。
該示例由單個Window類組成,其中包含具有不同輸入約束和顯示屬性的行編輯選擇,這些屬性可以通過從組合框中選擇項來更改。將它們放在一起可以幫助開發人員選擇合適的屬性用於行編輯,並且可以很容易地比較每個驗證器對用戶輸入的影響。
Window類定義
Window類繼承了QWidget幷包含一個構造函數和幾個槽:
class Window : public QWidget { Q_OBJECT public: Window(QWidget *parent = nullptr); public slots: void echoChanged(int); void validatorChanged(int); void alignmentChanged(int); void inputMaskChanged(int); void accessChanged(int); private: QLineEdit *echoLineEdit; QLineEdit *validatorLineEdit; QLineEdit *alignmentLineEdit; QLineEdit *inputMaskLineEdit; QLineEdit *accessLineEdit; };
當在關聯的組合框中選擇了新的驗證器時,這些槽用於更新給定行編輯的驗證器類型,行編輯保存在窗口中,以便在這些槽中使用。
Window類實現
Window構造函數用於設置行編輯器、驗證器和組合框,將來自組合框的信號連接到Window類中的槽,並在佈局中安排子部件。
我們首先構建一個組框來保存標籤、組合框和行編輯器,這樣就可以演示QLineEdit::echoMode屬性:
Window::Window(QWidget *parent) : QWidget(parent) { QGroupBox *echoGroup = new QGroupBox(tr("Echo")); QLabel *echoLabel = new QLabel(tr("Mode:")); QComboBox *echoComboBox = new QComboBox; echoComboBox->addItem(tr("Normal")); echoComboBox->addItem(tr("Password")); echoComboBox->addItem(tr("PasswordEchoOnEdit")); echoComboBox->addItem(tr("No Echo")); echoLineEdit = new QLineEdit; echoLineEdit->setPlaceholderText("Placeholder Text"); echoLineEdit->setFocus();
在這一點上,這些小部件都沒有被安排在佈局中。最後echoLabel、echoComboBox和echoLineEdit將被放置在echoGroup組框內的垂直佈局中。
類似地,我們構造組框和小部件集合來顯示QIntValidator 和QDoubleValidator對行編輯器內容的影響:
QGroupBox *validatorGroup = new QGroupBox(tr("Validator")); QLabel *validatorLabel = new QLabel(tr("Type:")); QComboBox *validatorComboBox = new QComboBox; validatorComboBox->addItem(tr("No validator")); validatorComboBox->addItem(tr("Integer validator")); validatorComboBox->addItem(tr("Double validator")); validatorLineEdit = new QLineEdit; validatorLineEdit->setPlaceholderText("Placeholder Text");
文本對齊由另一組小部件演示:
QGroupBox *alignmentGroup = new QGroupBox(tr("Alignment")); QLabel *alignmentLabel = new QLabel(tr("Type:")); QComboBox *alignmentComboBox = new QComboBox; alignmentComboBox->addItem(tr("Left")); alignmentComboBox->addItem(tr("Centered")); alignmentComboBox->addItem(tr("Right")); alignmentLineEdit = new QLineEdit; alignmentLineEdit->setPlaceholderText("Placeholder Text");
QLineEdit 支持使用輸入掩碼,它們只允許用戶在行編輯中輸入遵循簡單規範的字符,我們構建了一組小部件來演示預定義掩碼的選擇:
QGroupBox *inputMaskGroup = new QGroupBox(tr("Input mask")); QLabel *inputMaskLabel = new QLabel(tr("Type:")); QComboBox *inputMaskComboBox = new QComboBox; inputMaskComboBox->addItem(tr("No mask")); inputMaskComboBox->addItem(tr("Phone number")); inputMaskComboBox->addItem(tr("ISO date")); inputMaskComboBox->addItem(tr("License key")); inputMaskLineEdit = new QLineEdit; inputMaskLineEdit->setPlaceholderText("Placeholder Text");
QLineEdit的另一個有用特性是使其內容只讀的能力,此屬性用於控制對以下小部件組中的行編輯訪問:
QGroupBox *accessGroup = new QGroupBox(tr("Access")); QLabel *accessLabel = new QLabel(tr("Read-only:")); QComboBox *accessComboBox = new QComboBox; accessComboBox->addItem(tr("False")); accessComboBox->addItem(tr("True")); accessLineEdit = new QLineEdit; accessLineEdit->setPlaceholderText("Placeholder Text");
現在所有的子部件都已經構造好了,我們將來自組合框的信號連接到Window對象中的槽:
connect(echoComboBox, &QComboBox::activated, this, &Window::echoChanged); connect(validatorComboBox, &QComboBox::activated, this, &Window::validatorChanged); connect(alignmentComboBox, &QComboBox::activated, this, &Window::alignmentChanged); connect(inputMaskComboBox, &QComboBox::activated, this, &Window::inputMaskChanged); connect(accessComboBox, &QComboBox::activated, this, &Window::accessChanged);
這些連接中的每一個都使用QComboBox::activated()信號,該信號向插槽提供一個整數,這將用於有效地更改每個槽中的適當行編輯。
我們將每個組合框、行編輯和標籤放置在每個組框的佈局中,從echoGroup組框的佈局開始:
QGridLayout *echoLayout = new QGridLayout; echoLayout->addWidget(echoLabel, 0, 0); echoLayout->addWidget(echoComboBox, 0, 1); echoLayout->addWidget(echoLineEdit, 1, 0, 1, 2); echoGroup->setLayout(echoLayout);
其他佈局的構造方式相同:
QGridLayout *validatorLayout = new QGridLayout; validatorLayout->addWidget(validatorLabel, 0, 0); validatorLayout->addWidget(validatorComboBox, 0, 1); validatorLayout->addWidget(validatorLineEdit, 1, 0, 1, 2); validatorGroup->setLayout(validatorLayout); QGridLayout *alignmentLayout = new QGridLayout; alignmentLayout->addWidget(alignmentLabel, 0, 0); alignmentLayout->addWidget(alignmentComboBox, 0, 1); alignmentLayout->addWidget(alignmentLineEdit, 1, 0, 1, 2); alignmentGroup-> setLayout(alignmentLayout); QGridLayout *inputMaskLayout = new QGridLayout; inputMaskLayout->addWidget(inputMaskLabel, 0, 0); inputMaskLayout->addWidget(inputMaskComboBox, 0, 1); inputMaskLayout->addWidget(inputMaskLineEdit, 1, 0, 1, 2); inputMaskGroup->setLayout(inputMaskLayout); QGridLayout *accessLayout = new QGridLayout; accessLayout->addWidget(accessLabel, 0, 0); accessLayout->addWidget(accessComboBox, 0, 1); accessLayout->addWidget(accessLineEdit, 1, 0, 1, 2); accessGroup->setLayout(accessLayout);
最後我們將每個組框放置在Window對象的網格佈局中,並設置窗口標題:
QGridLayout *layout = new QGridLayout; layout->addWidget(echoGroup, 0, 0); layout->addWidget(validatorGroup, 1, 0); layout->addWidget(alignmentGroup, 2, 0); layout->addWidget(inputMaskGroup, 0, 1); layout->addWidget(accessGroup, 1, 1); setLayout(layout); setWindowTitle(tr("Line Edits")); }
槽響應用戶更改組合框時發出的信號。
當Echo分組框的組合框被改變時,echoChanged()槽被調用:
void Window::echoChanged(int index) { switch (index) { case 0: echoLineEdit->setEchoMode(QLineEdit::Normal); break; case 1: echoLineEdit->setEchoMode(QLineEdit::Password); break; case 2: echoLineEdit->setEchoMode(QLineEdit::PasswordEchoOnEdit); break; case 3: echoLineEdit->setEchoMode(QLineEdit::NoEcho); break; } }
槽更新同一分組框中的行編輯,來使用與分組合框中描述的條目對應的回顯模式。
當Validator分組框的組合框被改變時,validatorChanged()槽被調用:
void Window::validatorChanged(int index) { switch (index) { case 0: validatorLineEdit->setValidator(nullptr); break; case 1: validatorLineEdit->setValidator(new QIntValidator( validatorLineEdit)); break; case 2: validatorLineEdit->setValidator(new QDoubleValidator(-999.0, 999.0, 2, validatorLineEdit)); break; } validatorLineEdit->clear(); }
槽或者爲要使用的行編輯創建一個新的驗證器,或者通過調用帶有零指針的QLineEdit::setValidator()來刪除正在使用的驗證器。在本例中,我們清除了行編輯,以確保最初爲新的驗證器提供了有效的輸入。
當對齊分組框的組合框被改變時,alignmentChanged()槽被調用:
void Window::alignmentChanged(int index) { switch (index) { case 0: alignmentLineEdit->setAlignment(Qt::AlignLeft); break; case 1: alignmentLineEdit->setAlignment(Qt::AlignCenter); break; case 2: alignmentLineEdit->setAlignment(Qt::AlignRight); break; } }
這會改變文本在行編輯器中的顯示方式,使其與組合框中選擇的描述相對應。
inputMaskChanged()插槽處理輸入掩碼分組框中組合框的更改:
void Window::inputMaskChanged(int index) { switch (index) { case 0: inputMaskLineEdit->setInputMask(""); break; case 1: inputMaskLineEdit->setInputMask("+99 99 99 99 99;_"); break; case 2: inputMaskLineEdit->setInputMask("0000-00-00"); inputMaskLineEdit->setText("00000000"); inputMaskLineEdit->setCursorPosition(0); break; case 3: inputMaskLineEdit->setInputMask(">AAAAA-AAAAA-AAAAA-AAAAA-AAAAA;#"); break; } }
相關組合框中的每個條目都與一個輸入掩碼相關聯,我們通過使用合適的字符串調用QLineEdit::setInputMask()函數來設置一個新的掩碼;如果使用空字符串,則禁用掩碼。
accessChanged()槽處理Access分組框中組合框的更改:
void Window::accessChanged(int index) { switch (index) { case 0: accessLineEdit->setReadOnly(false); break; case 1: accessLineEdit->setReadOnly(true); break; } }
這裏,我們簡單地將分組合框中的False和True條目與傳遞給QLineEdit::setReadOnly()的False和True值關聯起來,這允許用戶啓用和禁用對行編輯的輸入。
Qt Widget組件推薦
- QtitanRibbon - Ribbon UI組件:是一款遵循Microsoft Ribbon UI Paradigm for Qt技術的Ribbon UI組件,QtitanRibbon致力於爲Windows、Linux和Mac OS X提供功能完整的Ribbon組件。
- QtitanChart - Qt類圖表組件:是一個C ++庫,代表一組控件,這些控件使您可以快速地爲應用程序提供漂亮而豐富的圖表。
- QtitanDataGrid - Qt網格組件:提供了一套完整的標準 QTableView 函數和傳統組件無法實現的獨特功能。使您能夠將不同來源的各類數據加載到一個快速、靈活且功能強大的可編輯網格中,支持排序、分組、報告、創建帶狀列、拖放按鈕和許多其他方便的功能。
- QtitanDocking:允許您像 Visual Studio 一樣爲您的偉大應用程序配備可停靠面板和可停靠工具欄。黑色、白色、藍色調色板完全支持 Visual Studio 2019 主題!