Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QLineEdit setText gives segment fault
Forum Updated to NodeBB v4.3 + New Features

QLineEdit setText gives segment fault

Scheduled Pinned Locked Moved Solved General and Desktop
5 Posts 3 Posters 959 Views 1 Watching
  • 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.
  • D Offline
    D Offline
    Daiajo
    wrote on last edited by koahnig
    #1

    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]

    K 1 Reply Last reply
    0
    • D Daiajo

      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]

      K Offline
      K Offline
      koahnig
      wrote on last edited by
      #2

      @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.

      Vote the answer(s) that helped you to solve your issue(s)

      1 Reply Last reply
      7
      • D Offline
        D Offline
        Daiajo
        wrote on last edited by
        #3

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

        aha_1980A K 2 Replies Last reply
        1
        • D Daiajo

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

          aha_1980A Offline
          aha_1980A Offline
          aha_1980
          Lifetime Qt Champion
          wrote on last edited by
          #4

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

          Qt has to stay free or it will die.

          1 Reply Last reply
          1
          • D Daiajo

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

            K Offline
            K Offline
            koahnig
            wrote on last edited by koahnig
            #5

            @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

            Vote the answer(s) that helped you to solve your issue(s)

            1 Reply Last reply
            1

            • Login

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • Users
            • Groups
            • Search
            • Get Qt Extensions
            • Unsolved