Results 1 to 4 of 4

Thread: segmentation fault with QLineEdit Sender

  1. #1
    Join Date
    Feb 2012
    Location
    Armenia/Yerevan
    Posts
    400
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11
    Thanks
    15
    Thanked 16 Times in 15 Posts

    Default segmentation fault with QLineEdit Sender

    I have a few QLineEdit-s; When the Edit QPushButton is clicked, I want to know which QLineEdit was selected beforehand.
    Qt Code:
    1. QLineEdit *clickedLine = qobject_cast<QLineEdit *>(sender());
    2. clickedLine->setText(";)");
    To copy to clipboard, switch view to plain text mode 

    However, in the case I have several push button and want to know which button was clicked, this is accomplished successfully with the same code snippet:

    Qt Code:
    1. QPushButton *clickedButton = qobject_cast<QPushButton *>(sender());
    2. int digitValue = clickedButton->text().toInt();
    3. qDebug() << digitValue;
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts

    Default Re: segmentation fault with QLineEdit Sender

    Check if sender() returns a QLineEdit pointer. a simple qDebug() << sender() should to.

    Cheers,
    _

  3. #3
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Wiki edits
    17

    Default Re: segmentation fault with QLineEdit Sender

    If clickedLine == 0, as it will when the sender is not a QLineEdit, then your code crashes. Hardly a surprise there.

    What saman_artorious seems to want though is to know which of many QLineEdits had the focus before the user clicked the button so that the slot can read that line edit and not the others. Of course, none of the line edits might have focus when the button is pressed or an unrelated line edit/widget might have focus etc. I don't know that it is generally possible to do this based on the current focussed widget (i.e. QApplication::focusWidget()). Even setting the push button to NoFocus and clicking with a line edit active may result in another widget having the focus by the time the clicked() slot processes. Seems you would have to track the last used QLineEdit using the editingFinished() or similar signal (or subclass and use the focusInEvent()).

    Seems like a terrible UI design to me but then it is not my project.

  4. #4
    Join Date
    Sep 2009
    Location
    Wroclaw, Poland
    Posts
    1,394
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows Android
    Thanked 342 Times in 324 Posts

    Default Re: segmentation fault with QLineEdit Sender

    In case each lineedit has its own "Edit" button, you can wrap the line edit and push button into one class and pass through the clicked() signal of QPushButton:
    Qt Code:
    1. class Wrapper : public QWidget{
    2. Q_OBJECT
    3. public:
    4. Wrapper(QWidget * parent = NULL) : QWidget(parent)
    5. {
    6. // initialize button and line edit, prepare layout etc...
    7. connect(_button, SIGNAL(clicked()), this, SIGNAL(buttonClicked()));
    8. }
    9. QLineEdit * lineEdit() const{
    10. return _lineEdit;
    11. }
    12. signals:
    13. void buttonClicked();
    14.  
    15. private:
    16. QLineEdit * _lineEdit;
    17. QPushButton * _button;
    18. };
    19.  
    20.  
    21. //...
    22.  
    23. void Class::onClickedSignal(){
    24. Wrapper * wrap = qobject_cast<Wrapper*>(sender());
    25. if (wrap){
    26. QLineEdit * lineEdit = wrap->lineEdit();
    27. //...
    28. }
    29. }
    To copy to clipboard, switch view to plain text mode 

    Another way is to connect to QLineEdit::editingFinished() signal instead.

Similar Threads

  1. Replies: 2
    Last Post: 17th June 2010, 00:58
  2. Segmentation Fault
    By jmc in forum Qt Tools
    Replies: 4
    Last Post: 24th February 2010, 21:08
  3. Segmentation fault
    By Schimanski in forum Qt Programming
    Replies: 20
    Last Post: 31st August 2009, 17:26
  4. Segmentation fault
    By MarkoSan in forum Qt Programming
    Replies: 23
    Last Post: 19th October 2008, 23:40
  5. QLineEdit Segmentation fault (core dumped)
    By cristiano in forum Qt Programming
    Replies: 1
    Last Post: 12th October 2006, 06:18

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.