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 Updated to NodeBB v4.3 + New Features

QLineEdit set color based on valid input?

Scheduled Pinned Locked Moved Solved General and Desktop
3 Posts 2 Posters 984 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 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");
    }
    
    JonBJ 1 Reply Last reply
    0
    • C Calicoder

      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");
      }
      
      JonBJ Offline
      JonBJ Offline
      JonB
      wrote on 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
      2
      • JonBJ JonB

        @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 last edited by
        #3

        @JonB Perfect, that worked. Thanks so much.

        1 Reply Last reply
        0

        • Login

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