Results 1 to 10 of 10

Thread: How to disable a QLineEdit?

  1. #1
    Join Date
    May 2011
    Posts
    10
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Windows

    Default How to disable a QLineEdit?

    Hi everybody!

    I´m using 3 QLineEdits that someone can enter a number in each.

    Idea: 1) QLineEdit No. 1 is enabled, and QLE No. 2 and No. 3 are disabled. --> If someone enters a number in QLE No.1, than No. 2 is enabled. No. 3 stays disabled. --> If someone enters a number in QLE No. 2, than even No. 3 is enabled.

    I´m not sure at all, if this is possible. So far I haven´t found a thread, where someone was disabling a QLineEdit. In the Qt-Helpfile I only found "dragEnabled" within the properties...

    If someone has got an idea, please let me know...

    Thanks in advance.

  2. #2
    Join Date
    May 2010
    Location
    Romania
    Posts
    1,021
    Thanks
    62
    Thanked 260 Times in 246 Posts
    Qt products
    Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: How to disable a QLineEdit?

    You can use the setDisabled(...); slot.

  3. #3
    Join Date
    May 2011
    Posts
    10
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: How to disable a QLineEdit?

    Thanks :-)! I try it!

  4. #4
    Join Date
    Apr 2010
    Location
    Sudan
    Posts
    46
    Thanks
    7
    Thanked 4 Times in 4 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: How to disable a QLineEdit?

    use signal and slot :
    the signal is when the text of the lineEdit change
    the slot is to check if the lineEdit is not empty , then set the lineEdit enabled.

    example:

    signals:
    Qt Code:
    1. connect(ui->lineEdit1,SIGNAL(textChanged(QString)),this,SLOT(lineEdit1TextChanged()));
    2. connect(ui->lineEdit2,SIGNALtextChanged(QString)),this,SLOT(lineEdit2TextChanged()));
    To copy to clipboard, switch view to plain text mode 

    slots:
    Qt Code:
    1. void MainWindow::lineEdit1TextChanged()
    2. {
    3. if(ui->lineEdit1->text()=="")
    4. ui->lineEdit2->setDisabled(1);
    5. else ui->lineEdit2->setEnabled(1);
    6. }
    7.  
    8. void MainWindow::lineEdit2TextChanged()
    9. {
    10. if(ui->lineEdit2->text()=="")
    11. ui->lineEdit3->setDisabled(1);
    12. else ui->lineEdit3->setEnabled(1);
    13. }
    To copy to clipboard, switch view to plain text mode 

  5. #5
    Join Date
    May 2011
    Posts
    10
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: How to disable a QLineEdit?

    Hi, thanks for your code-snippet.

    I tried (with validator):
    -------------
    Qt Code:
    1. ....
    2. in1->setValidator(validator);
    3. in2->setValidator(validator);
    4. .....
    5.  
    6. connect(in1,SIGNAL(textChanged(String)),this,SLOT(in1TextChanged()));
    7. }
    8.  
    9. void Tab::in1TextChanged()
    10. {
    11. if(in1->text()=="")
    12. {
    13. in2->setDisabled(1);
    14. }
    15. else
    16. {
    17. in2->setEnabled(1);
    18. }
    19. }
    To copy to clipboard, switch view to plain text mode 
    -------------
    with
    -------------
    Qt Code:
    1. private slots:
    2. void in1TextChanged();
    3.  
    4. private:
    5. ...
    6. QLineEdit *in1;
    7. QLineEdit *in2;
    To copy to clipboard, switch view to plain text mode 
    -------------
    set as pointers in the headerfile;

    and:
    --------
    Qt Code:
    1. void Tab::calculate()
    2. {
    3. .....
    4. bool ok;
    5. double number1 = in1->text().toDouble(&ok);
    6. double number2 = in2->text().toDouble(&ok);
    7. .......
    To copy to clipboard, switch view to plain text mode 
    --------------

    But the "disable"-Function does not work.

    Do you have an idea?
    Last edited by wysota; 18th May 2011 at 18:00.

  6. #6
    Join Date
    Apr 2010
    Location
    Sudan
    Posts
    46
    Thanks
    7
    Thanked 4 Times in 4 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: How to disable a QLineEdit?

    every thing seems fine ...

    make sure that you set the in2 disabled at the bigging of the Tab class after declaration ( in2=new QLineEdit; )

    also make sure that the connect statement exist at the same place.

  7. #7
    Join Date
    May 2011
    Posts
    10
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: How to disable a QLineEdit?

    Hi!

    Thanks tinysoft for your reply!

    I tried now everything new from scratch.

    Qt Code:
    1. #include "mainwindow.h"
    2. #include <QObject>
    3. #include <QtGui>
    4. #include <QIntValidator>
    5. #include <QWidget>
    6.  
    7. MainWindow::MainWindow(QWidget *parent) :
    8. QMainWindow(parent)
    9. {
    10. QGridLayout *grid = new QGridLayout;
    11. grid->addWidget(input(), 0, 0);
    12. setLayout(grid);
    13.  
    14. setWindowTitle(tr("QLineEdit example"));
    15. setMinimumSize(500, 400);
    16.  
    17. }
    18.  
    19. QGroupBox *MainWindow::input()
    20. {
    21. QGroupBox *groupBox = new QGroupBox(tr("Input"));
    22. QLabel *fwerl = new QLabel(tr("<font>Insert your values:</font>"));
    23. in1 = new QLineEdit;
    24. in2 = new QLineEdit;
    25. in3 = new QLineEdit;
    26.  
    27. QFormLayout *formLayout = new QFormLayout;
    28. formLayout->addRow(fwerl);
    29. formLayout->addRow(tr("1st value:"), in1);
    30. formLayout->addRow(tr("2nd value:"), in2);
    31. formLayout->addRow(tr("3rd value:"), in3);
    32. groupBox->setLayout(formLayout);
    33.  
    34. connect(in1,SIGNAL(textChanged(QString)),this,SLOT(in1TextChanged()));
    35. connect(in2,SIGNAL(textChanged(QString)),this,SLOT(in2TextChanged()));
    36.  
    37. QValidator *validator = new QDoubleValidator(0.0, 1.0, 10, in1);
    38. in1->setValidator(validator);
    39. in2->setValidator(validator);
    40. in3->setValidator(validator);
    41. return groupBox;
    42. }
    43.  
    44. void MainWindow::in1TextChanged()
    45. {
    46. if(in1->text()=="")
    47. {
    48. in2->setDisabled(1);
    49. }
    50. else
    51. {
    52. in2->setEnabled(1);
    53. }
    54. }
    55.  
    56. void MainWindow::in2TextChanged()
    57. {
    58. if(in2->text()=="")
    59. {
    60. in3->setDisabled(1);
    61. }
    62. else
    63. {
    64. in3->setEnabled(1);
    65. }
    66. }
    67.  
    68. MainWindow::~MainWindow()
    69. {
    70. delete mainWindow;
    71. }
    To copy to clipboard, switch view to plain text mode 

    Now the problem is, the QLineEdits are not displayed. But I don´t even get a warning - everything compiles fine...

    Do you know why? If I use "setCentralWidget(grid)" instead of setLayout(grid) it doesn´t run anymore...

  8. #8
    Join Date
    Apr 2010
    Location
    Sudan
    Posts
    46
    Thanks
    7
    Thanked 4 Times in 4 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: How to disable a QLineEdit?

    i am confused .. is it possible to return QGroupBox ?
    Qt Code:
    1. QGroupBox *MainWindow::input();
    To copy to clipboard, switch view to plain text mode 
    you can use the designer you know .. its much more easier .i wrote your example using the designer to create the widgets and the layout .. i uploaded it ( here )

    i hope its helpful

  9. #9
    Join Date
    May 2011
    Posts
    10
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: How to disable a QLineEdit?

    Hi tinysoft!

    Thank you very much. With your file and some work I found the problems:
    1) I was missing:
    Qt Code:
    1. in1->setDisabled(1);....
    To copy to clipboard, switch view to plain text mode 
    2)
    Qt Code:
    1. QWidget *widget = new QWidget;
    2. setCentralWidget(widget);
    To copy to clipboard, switch view to plain text mode 
    because of QMainWindow. QWidget, for example, is a member of it.
    and
    3) I removed the GroupBox and just used:
    Qt Code:
    1. QGridLayout *formLayout = new QGridLayout;
    2. ...
    3. widget->setLayout(formLayout);
    To copy to clipboard, switch view to plain text mode 

    Now everything works fine :-)!

    Thanks for your hints and have a nice day!

  10. #10
    Join Date
    Apr 2010
    Location
    Sudan
    Posts
    46
    Thanks
    7
    Thanked 4 Times in 4 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: How to disable a QLineEdit?

    i'm glad that you solved it

    have a nice day u 2

Similar Threads

  1. Replies: 1
    Last Post: 12th October 2010, 22:20
  2. disable alt+tab
    By ahmdsd_ostora in forum Qt Programming
    Replies: 24
    Last Post: 11th July 2010, 09:40
  3. How to disable ALT + F4
    By elizabeth.h1 in forum Qt Programming
    Replies: 19
    Last Post: 22nd June 2010, 22:05
  4. disable myself when clicked
    By nifei in forum Qt Programming
    Replies: 6
    Last Post: 19th February 2009, 10:01
  5. Disable Tab Key
    By otortos in forum Newbie
    Replies: 6
    Last Post: 25th March 2006, 16:27

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.