Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QLineEdit set color based on valid input?
Forum Update on Monday, May 27th 2025

QLineEdit set color based on valid input?

Scheduled Pinned Locked Moved Solved General and Desktop
3 Posts 2 Posters 976 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • C Offline
    C Offline
    Calicoder
    wrote on 20 Apr 2022, 22:00 last edited by
    #1

    Good day all, hope your day is going well. Got a question and any advice would be appreciated. I have a QLineEdit that accepts a 2 digit ID number. I have it currently set to display a red background color if there's no ID and a green color whenever I type in 2 digits. Problem is at the start of the application, the QLineEdit box is white. Can I somehow set the textEdited() signal on start to true so that it triggers my checkID() method?

    On app start, it should be red background as there's no valid input.
    b39cfd1f-327a-43df-a623-620b74d01bc6-image.png

    mainwindow.h

    void checkID(const QString &id);
    

    mainwindow.cpp

    MainWindow::MainWindow(QWidget *parent)
        : QMainWindow(parent)
        , ui(new Ui::MainWindow)
    {
        ui->setupUi(this);
        connect(ui->IDLineEdit, &QLineEdit::textEdited, this, &MainWindow::checkID);
        ui->IDLineEdit->setValidator(new QRegularExpressionValidator(QRegularExpression("^[1-9]{2}$"), this));
    }
    
    void MainWindow::checkID(const QString &id)
    {
        bool empty = ui->IDLineEdit->hasAcceptableInput();
        ui->IDLineEdit->setStyleSheet(empty ? "background-color: green" : "background-color: red");
    }
    
    J 1 Reply Last reply 20 Apr 2022, 22:03
    0
    • C Calicoder
      20 Apr 2022, 22:00

      Good day all, hope your day is going well. Got a question and any advice would be appreciated. I have a QLineEdit that accepts a 2 digit ID number. I have it currently set to display a red background color if there's no ID and a green color whenever I type in 2 digits. Problem is at the start of the application, the QLineEdit box is white. Can I somehow set the textEdited() signal on start to true so that it triggers my checkID() method?

      On app start, it should be red background as there's no valid input.
      b39cfd1f-327a-43df-a623-620b74d01bc6-image.png

      mainwindow.h

      void checkID(const QString &id);
      

      mainwindow.cpp

      MainWindow::MainWindow(QWidget *parent)
          : QMainWindow(parent)
          , ui(new Ui::MainWindow)
      {
          ui->setupUi(this);
          connect(ui->IDLineEdit, &QLineEdit::textEdited, this, &MainWindow::checkID);
          ui->IDLineEdit->setValidator(new QRegularExpressionValidator(QRegularExpression("^[1-9]{2}$"), this));
      }
      
      void MainWindow::checkID(const QString &id)
      {
          bool empty = ui->IDLineEdit->hasAcceptableInput();
          ui->IDLineEdit->setStyleSheet(empty ? "background-color: green" : "background-color: red");
      }
      
      J Online
      J Online
      JonB
      wrote on 20 Apr 2022, 22:03 last edited by
      #2

      @Calicoder said in QLineEdit set color based on valid input?:

      . Can I somehow set the textEdited() signal on start to true so that it triggers my checkID() method?

      No, but your slot method checkID() can be called explicitly, e.g. at the end of your MainWindow constructor. Slot methods are like any method, you can just call them directly as well as via a signal.

      C 1 Reply Last reply 20 Apr 2022, 22:50
      2
      • J JonB
        20 Apr 2022, 22:03

        @Calicoder said in QLineEdit set color based on valid input?:

        . Can I somehow set the textEdited() signal on start to true so that it triggers my checkID() method?

        No, but your slot method checkID() can be called explicitly, e.g. at the end of your MainWindow constructor. Slot methods are like any method, you can just call them directly as well as via a signal.

        C Offline
        C Offline
        Calicoder
        wrote on 20 Apr 2022, 22:50 last edited by
        #3

        @JonB Perfect, that worked. Thanks so much.

        1 Reply Last reply
        0

        1/3

        20 Apr 2022, 22:00

        • Login

        • Login or register to search.
        1 out of 3
        • First post
          1/3
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved