Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Solved LineEdit .isEmpty() Problem

    General and Desktop
    line edit empty problem qt 5.4.1
    3
    4
    3628
    Loading More Posts
    • 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.
    • M4RZB4Ni
      M4RZB4Ni last edited by

      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 Reply Quote 0
      • M
        Mitchell last edited by

        @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 Reply Quote 2
        • SGaist
          SGaist Lifetime Qt Champion last edited by

          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

          M4RZB4Ni 1 Reply Last reply Reply Quote 2
          • M4RZB4Ni
            M4RZB4Ni @SGaist last edited by

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

            Thanks
            M4RZB4Ni

            1 Reply Last reply Reply Quote 0
            • First post
              Last post