Results 1 to 6 of 6

Thread: Dynamical LineEdit and signals

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    May 2009
    Posts
    14
    Qt products
    Qt4
    Platforms
    Windows
    Thanks
    7

    Default Dynamical LineEdit and signals

    Hi all,

    i have a problem, with a loop I create several LineEdits and signals, depending of the content of a list that stores data from a XML.

    The problem is that the user can modify the content of the different LineEdits, but i do not know how to recognize which LineEdit has been modified, because it was created by a loop.

    Qt Code:
    1. void Controller_run::update_running_list(Q3ListViewItem *s){
    2.  
    3. QLabel *name = new QLabel;
    4. QLineEdit *value = new QLineEdit;
    5. connect (value, SIGNAL (editingFinished()),this, SLOT (Actualize()));
    6. value->setAlignment(Qt::AlignRight|Qt::AlignTrailing|Qt::AlignVCenter);
    7. value->setMinimumSize(QSize(71, 20));
    8. value->setMaximumSize(QSize(71, 20));
    9. name->setText(s->text(0));
    10. value->setText(s->text(4));
    11. if( s->text(1) == "DISCRETE_IN" || s->text(1) == "ANALOG_IN"){
    12. value->setDisabled("true");
    13. gridlayout->addWidget(name,row_input, 2);
    14. gridlayout->addWidget(value, row_input, 3);
    15. }
    16. else{
    17. value->setEnabled("true");
    18. gridlayout->addWidget(name,row_output, 0);
    19. gridlayout->addWidget(value, row_output, 1);
    20. }
    21. }
    To copy to clipboard, switch view to plain text mode 

    Please, any idea? Thank you!

  2. #2
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows
    Thanks
    21
    Thanked 418 Times in 411 Posts

    Default Re: Dynamical LineEdit and signals

    in update_running_list() make a list of your QLineEdits, you need it also because you want to be able to delete them , otherwise you get potential memory leak.
    Qt Code:
    1. //in Controller_run.h
    2. QVector<QLineEdit*> m_vecLineEdit;
    To copy to clipboard, switch view to plain text mode 
    Qt Code:
    1. void Controller_run::update_running_list(Q3ListViewItem *s){
    2.  
    3. QLabel *name = new QLabel;
    4. QLineEdit *value = new QLineEdit;
    5. m_vecLineEdit.push_back(value );
    6. .....
    To copy to clipboard, switch view to plain text mode 

    in the slot Actualize() you can do something like:
    Qt Code:
    1. QLineEdit *pLineEdit = sender();
    2. for(int i=0; i< m_vecLineEdit.size(); i++){
    3. if(m_vecLineEdit[i] == pLineEdit){
    4. //now you know which line edit you have and you can do what you want with it.
    5. }
    6. }
    To copy to clipboard, switch view to plain text mode 

    and, clean up:
    Qt Code:
    1. Controller_run::~Controller_run()
    2. {
    3. for(int i=0; i< m_vecLineEdit.size(); i++){
    4. delete m_vecLineEdit[i];
    5. }
    6. }
    To copy to clipboard, switch view to plain text mode 
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  3. The following user says thank you to high_flyer for this useful post:

    martisho (13th November 2009)

  4. #3
    Join Date
    May 2009
    Posts
    14
    Qt products
    Qt4
    Platforms
    Windows
    Thanks
    7

    Default Re: Dynamical LineEdit and signals

    Ok, than you very much high_flyer, when a i compile this errors occurs refered to QLineEdit *pLineEdit = sender();

    Controller_run.cpp(257) : error C2440: 'initializing' : cannot convert from 'QObject *' to 'QLineEdit *'
    1> Cast from base to derived requires dynamic_cast or static_cast



    Thank you very much again.

  5. #4
    Join Date
    Oct 2009
    Posts
    22
    Thanked 1 Time in 1 Post

    Default Re: Dynamical LineEdit and signals

    Qt Code:
    1. QLineEdit *pLineEdit = qobject_cast<QLineEdit*>(sender());
    To copy to clipboard, switch view to plain text mode 

    That should do the trick. You need to cast the return value of sender() which is a generic QObject* to the type of object you are expecting the sender to be - QLineEdit*

  6. The following user says thank you to danc81 for this useful post:

    martisho (13th November 2009)

  7. #5
    Join Date
    Sep 2009
    Location
    UK
    Posts
    2,447
    Qt products
    Qt4
    Platforms
    Windows
    Thanks
    6
    Thanked 348 Times in 333 Posts

    Default Re: Dynamical LineEdit and signals

    Another way would be to use QSignalMapper, so you assign the event you want to catch to the mapper along with an id, and when such an event occurs, the mapper will call your slot with that id. That way you don't need to use a for loop or do any casting to find out which object fired the signal.

    Makes your code easier to read too, and is more OO.

  8. The following user says thank you to squidge for this useful post:

    martisho (13th November 2009)

  9. #6
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows
    Thanks
    21
    Thanked 418 Times in 411 Posts

    Default Re: Dynamical LineEdit and signals

    I agree with fatjuicymole in this case.
    QSignalMapper would fit better in here.
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  10. The following user says thank you to high_flyer for this useful post:

    martisho (13th November 2009)

Similar Threads

  1. Lineedit text change signal
    By zgulser in forum Qt Tools
    Replies: 2
    Last Post: 19th January 2009, 13:38

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
  •  
Qt is a trademark of The Qt Company.