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. LineEdit .isEmpty() Problem
Forum Updated to NodeBB v4.3 + New Features

LineEdit .isEmpty() Problem

Scheduled Pinned Locked Moved Solved General and Desktop
line editemptyproblemqt 5.4.1
4 Posts 3 Posters 4.9k Views 2 Watching
  • 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.
  • M Offline
    M Offline
    M4RZB4Ni
    wrote on 26 May 2016, 01:44 last edited by
    #1

    hello,
    i have a lineEdit on My form and i want set a if on it
    that when my lineedit are empty my pushButton set disable(true)
    and when line edit are not empty my push button set disable(false)
    i wrote this codes but not Worked!
    what i must to do?

    if(ui->lineEdit_47->text().isEmpty()){
               ui->pushButton_8->setDisabled(true);
    
            }else{
                ui->pushButton_8->setDisabled(false);
    }
    

    Thanks
    M4RZB4Ni

    1 Reply Last reply
    0
    • M Offline
      M Offline
      Mitchell
      wrote on 26 May 2016, 17:15 last edited by
      #2

      @M4RZB4Ni
      The code you wrote is correct. What you need to do is make sure it is connected to a slot. The slot will call a function whenever the lineEdit gets changed and you can check to see if you need to disable or enable the button. Here is some sample code. It has a lineEdit and a button. In the constructor you will do the check to see if you need to enable or disable the button. And then when ever some one changes the text it will call a function and update the state of the button.
      Hope this helps out.

      mainWindow.h

      #ifndef MAINWINDOW_H
      #define MAINWINDOW_H
      
      #include <QMainWindow>
      
      namespace Ui {
      	class MainWindow;
      	}
      
      class MainWindow : public QMainWindow
      {
      	Q_OBJECT
      
      public:
      	explicit MainWindow(QWidget *parent = 0);
      	~MainWindow();
      
      private slots:
      	void	on_pushButton_clicked();
      	void	onTextChanged(QString a_strString);
      
      private:
      	Ui::MainWindow *ui;
      };
      
      #endif // MAINWINDOW_H
      

      mainWindow.cpp

      #include "mainwindow.h"
      #include "ui_mainwindow.h"
      
      #include <QSettings>
      #include <QDebug>
      
      MainWindow::MainWindow(QWidget *parent) :
      	QMainWindow(parent),
      	ui(new Ui::MainWindow)
      {
      	ui->setupUi(this);
      
      	//set slots to see if the line edit has chanegd
      	connect(ui->lineEdit,
      			SIGNAL(textChanged(QString)),
      			this,
      			SLOT(onTextEdited(QString)));
      
      	if(ui->lineEdit->text().isEmpty()) {
      		qDebug() << "THis works";
      		ui->pushButton->setDisabled(true);
      	} else {
      		ui->pushButton->setDisabled(false);
      	}
      }
      
      MainWindow::~MainWindow()
      {
      	delete ui;
      }
      
      void MainWindow::on_pushButton_clicked()
      {
      
      }
      
      void MainWindow::onTextChanged(QString a_strString)
      {
      	//the text has been edited so you need to see if it is empty
      	if(ui->lineEdit->text().isEmpty()) {
      		ui->pushButton->setDisabled(true);
      	} else {
      		ui->pushButton->setDisabled(false);
      	}
      }
      
      1 Reply Last reply
      2
      • S Offline
        S Offline
        SGaist
        Lifetime Qt Champion
        wrote on 26 May 2016, 22:54 last edited by
        #3

        Hi,

        A shorter version:

        void MainWindow::onTextChanged(const QString &a_strString)
        {
            ui->pushButton->setDisabled(a_strString.isEmpty());
        }
        

        One some small thing: use a const reference to your QString parameter, that will avoid a useless copy.

        Interested in AI ? www.idiap.ch
        Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

        M 1 Reply Last reply 27 May 2016, 03:30
        2
        • S SGaist
          26 May 2016, 22:54

          Hi,

          A shorter version:

          void MainWindow::onTextChanged(const QString &a_strString)
          {
              ui->pushButton->setDisabled(a_strString.isEmpty());
          }
          

          One some small thing: use a const reference to your QString parameter, that will avoid a useless copy.

          M Offline
          M Offline
          M4RZB4Ni
          wrote on 27 May 2016, 03:30 last edited by
          #4

          @SGaist
          Thank You so much ,
          My problem solved! :)

          Thanks
          M4RZB4Ni

          1 Reply Last reply
          0

          3/4

          26 May 2016, 22:54

          • Login

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