Skip to content

CopperSpice Journal

CopperSpice Journal

Source Code  (Gui Example 7)

Posted on January 26, 2022April 11, 2022 By Barbara Geller

This example will show how to configure a QLineEdit when the user needs to enter a password.

#include <QtCore>
#include <QtGui>

class MainWindow : public QWidget
{
 public:
   MainWindow();
};

The declaration for MainWindow is the same as for example 6.

MainWindow::MainWindow()
{
   setMinimumSize(700, 350);

   // text edit
   QLabel *label_1 = new QLabel(
        "<h3>Password - Normal Text</h3>");

   QLineEdit *text_1 = new QLineEdit();
   text_1->setText("password");
   text_1->setMinimumSize(400, 35);
   text_1->setEchoMode(QLineEdit::Normal);

   //
   QLabel *label_2 = new QLabel(
        "<h3>Password - Show Stars</h3>");

   QLineEdit *text_2 = new QLineEdit();
   text_2->setText("password");
   text_2->setMinimumSize(400, 35);
   text_2->setEchoMode(QLineEdit::Password);

   //
   QLabel *label_3 = new QLabel(
        "<h3>Password - No Echo, Invisible Text</h3>");

   QLineEdit *text_3 = new QLineEdit();
   text_3->setText("password");
   text_3->setMinimumSize(400, 35);
   text_3->setEchoMode(QLineEdit::NoEcho);

   QPushButton *pb_1 = new QPushButton();
   pb_1->setText("Close");

   QVBoxLayout *layout1 = new QVBoxLayout();
   layout1->addSpacing(75);
   layout1->addWidget(label_1);
   layout1->addWidget(text_1);
   layout1->addSpacing(35);
   layout1->addWidget(label_2);
   layout1->addWidget(text_2);
   layout1->addSpacing(35);
   layout1->addWidget(label_3);
   layout1->addWidget(text_3);
   layout1->addSpacing(75);

   QHBoxLayout *layout2 = new QHBoxLayout();
   layout2->addStretch();
   layout2->addWidget(pb_1);
   layout2->addStretch();

   QVBoxLayout *layoutMain = new QVBoxLayout(this);
   layoutMain->addLayout(layout1);
   layoutMain->addSpacing(75);
   layoutMain->addLayout(layout2);

   connect(pb_1, &QPushButton::clicked, 
         this, &QWidget::close);
}

On lines 12, 21, and 30 the QLineEdit::setEchoMode() method receives an enum value which defines what should happen when the user enters a password. In the first example the text is simply displayed on the screen. Line 21 is the most common use case where passing QLineEdit::Password will display stars for every character the user types.

The third example is a bit unusual. The echo is set so nothing will appear on the screen. When you test this example you will not see any data displayed.

Main Function

int main(int argc, char *argv[])
{
   QApplication app(argc, argv);

   MainWindow *window = new MainWindow();
   window->show();

   return app.exec();
}

The source code for main() is the same as shown in example 3.

Running the Example

To build and run this example use the same CMake build file and commands as we showed for the first example. The only suggested modification is on line 2 of the CMakeLists.txt file, change “example_1” to “example_7”.

https://download.copperspice.com/journal/example_07.zip

Uncategorized

Post navigation

Previous Post: Source Code  (Gui Example 6)
Next Post: Source Code  (Gui Example 8)
  • CopperSpice Journal Homepage
    • Table of Contents
    • Example Source Code
    • Discussion Forum
  • CopperSpice Homepage
  • Github Repository
  • Videos About C++

Post comments or questions on our CopperSpice Forum

Copyright © 2021-2025 CopperSpice