QLineEdit setText gives segment fault



  • I wanted to have a font name entry in a LineEdit, initially displaying the current font, which the user could then overwrite with another font name. However the initial setText in the constructor segment faults the program. I have created a cut down version which exhibits the same behavior:
    LineEdit.cpp

    #include <QApplication>
    
    #include "Font.h"
    
    int main ( int argc , char * * argv )
    {
      QApplication app ( argc , argv ) ;
    
      Font window ;
      window . resize ( 250 , 150 ) ;
      window . setWindowTitle ( "LineEdit" ) ;
      window . show () ;
    
      return app . exec () ;
    }
    

    Font.h

    #pragma once
    
    #include <QWidget>
    #include <QLineEdit>
    
    class Font : public QWidget
    {
      public :
        Font ( QWidget * parent = 0 ) ;
    
      private :
        QLineEdit * edit_family ;
    } ;
    

    Font.cpp

    #include <QWidget>
    #include <QTextStream>  // debugging
    #include <QHBoxLayout>
    #include <QString>
    #include <QLineEdit>
    #include <QFontInfo>
    
    #include "Font.h"
    
    Font::Font ( QWidget * parent ) : QWidget ( parent )
    {
    QTextStream out ( stdout ) ; // debugging
      
      QFontInfo data_font = QWidget::fontInfo () ;
    out << "Font = " << data_font . family () << endl ;
      
      edit_family -> setText ( data_font . family () ) ;
    out << "Font loaded edit family" << endl ;
      
      QHBoxLayout * row = new QHBoxLayout ( this ) ;
      row -> addWidget ( edit_family ) ;
    
      setLayout ( row ) ;
    out << "Font made" << endl ;
    }
    

    The segment fault occurs on edit_family -> setText. The family message comes up as "Font = Noto Sans"
    I'm on Gentoo Linux, Qt version 5.9.4-r2
    Is this a bug?

    [koahnig: code tags added]


  • Moderators

    @Daiajo

    Hi and welcome in devnet forum

    Yes, it is a bug in your SW.

    You are defining a pointer as below which is only the address to memory, but you do not assign any memory. The pointer is typically pointing somewhere.

      private :
        QLineEdit * edit_family ;
    

    With

     edit_family -> setText ( data_font . family () ) ;
    

    you are simply assigning text to that arbitrary memory location. Very rarely programs are still functioning afterwards.

    I suggest that you are starting out with some C++ tutorials e.g. this one on pointers.

    Using Qt requires at least basic knowledge of C++. Therefore, you do yourself a favor to make sure that you have enough knowledge before you continue and try to understand Qt.



  • Thanks, returning to programming after a long absence and making some stupid mistakes.


  • Qt Champions 2017

    @Daiajo ok, but please mark this topic as SOLVED.


  • Moderators

    @Daiajo said in QLineEdit setText gives segment fault:

    Thanks, returning to programming after a long absence and making some stupid mistakes.

    Do not worry. Happened to all of us.
    Looking from outside makes it sometimes easier to spot the issue.
    Enjoy your restart in programming


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.