Results 1 to 3 of 3

Thread: Encountering segmentation fault while accessing QLineEdit

  1. #1
    Join Date
    Jun 2009
    Posts
    38
    Thanks
    8
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Encountering segmentation fault while accessing QLineEdit

    Hi,

    I am encountering a Segmentation fault while trying to write somethiing in a LineEdit widget (defined in Ui_MainWindow) from another class sqMap which inherits QGraphicsItem and Ui_MainWindow. The output of gdb's backtracking is as under:
    Qt Code:
    1. Program received signal SIGSEGV, Segmentation fault.
    2. [Switching to Thread 0xb6e8e8e0 (LWP 4644)]
    3. QLineEditPrivate::setText (this=0x0, txt=@0xbff2c200, pos=-1, edited=false) at widgets/qlineedit.cpp:2909
    4. 2909 q->resetInputContext();
    5. (gdb) bt
    6. #0 QLineEditPrivate::setText (this=0x0, txt=@0xbff2c200, pos=-1, edited=false) at widgets/qlineedit.cpp:2909
    7. #1 0xb7b9018b in QLineEdit::setText (this=0x80ce468, text=@0xbff2c200) at widgets/qlineedit.cpp:388
    8. #2 0x08057e3f in sqMap::paint (this=0x80f6278, painter=0xbff2d3ac, option=0x81b7d78, widget=0x8123170) at sqMap.cpp:95
    9. #3 0xb7d8122d in _q_paintItem (item=0x80f6280, painter=0xbff2d3ac, option=0x81b7d78, widget=0x8123170, useWindowOpacity=true,
    10. painterStateProtection=true) at graphicsview/qgraphicsscene.cpp:3754
    11. #4 0xb7d83eca in QGraphicsScenePrivate::drawItemHelper (item=0x80f6280, painter=0xbff2d3ac, option=0x81b7d78, widget=0x8123170,
    12. painterStateProtection=<value optimized out>) at graphicsview/qgraphicsscene.cpp:3810
    13. #5 0xb7d85049 in QGraphicsScene::drawItems (this=0x81a6fb0, painter=0xbff2d3ac, numItems=2, items=0x8140440, options=0x81b7cf4,
    14. widget=0x8123170) at graphicsview/qgraphicsscene.cpp:4036
    15. #6 0xb7d98aac in QGraphicsView::drawItems (this=0x80dcf28, painter=0xbff2d3ac, numItems=2, items=0x8140440, options=0x81b7cf4)
    16. ---Type <return> to continue, or q <return> to quit---bt
    17. at graphicsview/qgraphicsview.cpp:3351
    18. #7 0xb7da20c4 in QGraphicsView::paintEvent (this=0x80dcf28, event=0xbff2d8fc) at graphicsview/qgraphicsview.cpp:3096
    19. #8 0xb784384b in QWidget::event (this=0x80dcf28, event=0xbff2d8fc) at kernel/qwidget.cpp:7301
    20. #9 0xb7b7aec3 in QFrame::event (this=0x80dcf28, e=0xbff2d8fc) at widgets/qframe.cpp:651
    21. #10 0xb7c119ef in QAbstractScrollArea::viewportEvent (this=0x80dcf28, e=0x80f9f68) at widgets/qabstractscrollarea.cpp:943
    22. #11 0xb7d9f70f in QGraphicsView::viewportEvent (this=0x80dcf28, event=0xbff2d8fc) at graphicsview/qgraphicsview.cpp:2337
    23. #12 0xb7c13f95 in QAbstractScrollAreaFilter::eventFilter (this=0x80d9710, o=0x8123170, e=0xbff2d8fc)
    24. at widgets/qabstractscrollarea_p.h:96
    25. #13 0xb739cc4a in QCoreApplicationPrivate::sendThroughObjectEventFilters (this=0x805f1d8, receiver=0x8123170, event=0xbff2d8fc)
    26. at kernel/qcoreapplication.cpp:694
    To copy to clipboard, switch view to plain text mode 

    Here are some details of the classes used:

    Ui_MainWindow.h
    Generated with the help of Qt Designer but it was defined and used as a simple class instead of a namespace.

    sqMap.h
    My defined class used for displaying a map of squares.
    Qt Code:
    1. class sqMap : public QObject, public QGraphicsItem, public Ui_MainWindow
    2. {
    3. Q_OBJECT
    4.  
    5. public:
    6. sqMap(double Width, double Height, ps_info* processInformation, QGraphicsItem *parent = 0);
    7. QRectF boundingRect() const;
    8. void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget = 0);
    9. void updateLocation(int size);
    10. double getWidth();
    11. double getHeight();
    12.  
    13. protected:
    14. void hoverEnterEvent(QGraphicsSceneHoverEvent *e);
    15. void hoverLeaveEvent(QGraphicsSceneHoverEvent *e);
    16. void mousePressEvent(QGraphicsSceneMouseEvent *e);
    17. void mouseReleaseEvent(QGraphicsSceneMouseEvent *e);
    18. private:
    19. QColor color;
    20. int width;
    21. int height;
    22. bool m_isHovered;
    23. bool m_mouseIsDown; //caters for whether an item has been clicked
    24.  
    25. };
    To copy to clipboard, switch view to plain text mode 

    Definition of paint function for sqMap
    Qt Code:
    1. void sqMap::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
    2. {
    3. QPen pen;
    4. bool isSelected = (option->state & QStyle::State_Selected) == QStyle::State_Selected;
    5. painter->setBrush(m_isHovered ? Qt::red : color);
    6. painter->drawRect(0,-265,width,height);
    7. if (m_mouseIsDown)
    8. {
    9. lineEdit->setText("something");
    10. }
    11. }
    To copy to clipboard, switch view to plain text mode 

    My Objective:
    When a square is clicked 'something' may display in the LineEdit

    Problem:
    Squares show up finely but a click on it causes Segmetation fault.

    I am struggling for a while to figure out whats wrong and your help may save me a lot of time.

    Thanks
    Last edited by qtzcute; 10th August 2009 at 06:05. Reason: missing [code] tags

  2. #2
    Join Date
    Jul 2009
    Location
    Enschede, Netherlands
    Posts
    462
    Thanked 69 Times in 67 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Encountering segmentation fault while accessing QLineEdit

    Qt Code:
    1. QLineEditPrivate::setText (this=0x0, txt=@0xbff2c200, pos=-1, edited=false) at widgets/qlineedit.cpp:2909
    To copy to clipboard, switch view to plain text mode 

    There's your problem. The QLineEditPrivate doesn't exist (this == 0x0). Most likely cause is that you don't initialize your QLineEdit.
    Qt Code:
    1. lineEdit = new QLineEdit(this);
    To copy to clipboard, switch view to plain text mode 
    Last edited by franz; 10th August 2009 at 06:34. Reason: updated contents
    Horse sense is the thing that keeps horses from betting on people. --W.C. Fields

    Ask Smart Questions

  3. #3
    Join Date
    Jun 2010
    Posts
    1
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Encountering segmentation fault while accessing QLineEdit

    Hi, im having the same sort of problem with a QLineEdit. The compiler succesfully compile the project and when you try to change a property of QLineEdit it ends in a segmentation fault... here is my code
    ojimetro.cpp
    Qt Code:
    1. #include <QLabel>
    2. #include <QTextLine>
    3. #include <QtGui>
    4. //#include <QLineEdit>
    5. #include "ojimetro.h"
    6.  
    7.  
    8.  
    9. Window::Window()
    10. {
    11. setFixedSize(800, 600);
    12.  
    13.  
    14. QPushButton *salir = new QPushButton(tr("Salir"), this);
    15. // salir->setGeometry(1, 1, 75, 30);
    16. salir->setFont(QFont("Times", 18, QFont::Bold));
    17. QPushButton *probar = new QPushButton(tr("Probar"), this);
    18. //probar->setGeometry(76, 1, 75, 30);
    19. probar->setFont(QFont("Times", 18, QFont::Bold));
    20. QLabel *viendo=new QLabel(tr("viendo"),this);
    21. //viendo->setGeometry(150,1,40,40);
    22. QLineEdit *lineat=new QLineEdit("casa",this);
    23.  
    24.  
    25.  
    26.  
    27. connect(salir, SIGNAL(clicked()), qApp, SLOT(quit()));
    28. connect(probar,SIGNAL(clicked()),this,SLOT(copia()));
    29. //renderArea->setGeometry(90,100,350,200);
    30. QGridLayout *mainLayout = new QGridLayout;
    31. mainLayout->setColumnStretch(0, 1);
    32. mainLayout->setColumnStretch(3, 1);
    33. mainLayout->addWidget(renderArea, 0, 0, 1, 4);
    34. mainLayout->setRowMinimumHeight(1, 6);
    35. mainLayout->addWidget(viendo, 2, 1, Qt::AlignRight);
    36. mainLayout->addWidget(lineat, 2, 2);
    37. mainLayout->addWidget(salir, 3, 1, Qt::AlignRight);
    38. mainLayout->addWidget(probar, 3, 2);
    39.  
    40.  
    41.  
    42. setLayout(mainLayout);
    43. //renderArea->adjustSize();
    44. //dato=lineat->text();
    45.  
    46.  
    47. setWindowTitle(tr("Ojimetro v0.1"));
    48.  
    49.  
    50. }
    51.  
    52. void Window::copia()
    53. {
    54. int a,b,c;
    55. QString texto1;
    56. int poin;
    57.  
    58.  
    59.  
    60. a=1;
    61.  
    62. //texto1=lineat->text();
    63. //this->lineat->setText("oo");
    64. //qDebug() << this->lineat->text();
    65. b=2;
    66. c=0;
    67.  
    68. lineat->setText("hola!!!");
    69. c=a+b;
    70. a=2;
    71.  
    72.  
    73.  
    74. }
    To copy to clipboard, switch view to plain text mode 
    when im debugging, the debugger stops at lineat->setText("hola!!!") and shows the segmentation fault
    ojimetro.h

    Qt Code:
    1. #ifndef OJIMETRO_H
    2. #define OJIMETRO_H
    3. #include <QTextLine>
    4. #include <QWidget>
    5. #include <QObject>
    6. QT_BEGIN_NAMESPACE
    7. class QCheckBox;
    8. class QLineEdit;
    9. class QComboBox;
    10. class QLabel;
    11. class QSpinBox;
    12. class QTextLine;
    13. QT_END_NAMESPACE
    14.  
    15.  
    16.  
    17. class Window : public QWidget
    18. {
    19. Q_OBJECT
    20. public:
    21.  
    22. Window();
    23.  
    24. public slots:
    25. void copia();
    26.  
    27.  
    28. public:
    29. QLineEdit *lineat;
    30. #endif // OJIMETRO_H
    31. };
    To copy to clipboard, switch view to plain text mode 




    gbd reports this...

    Program received signal SIGSEGV, Segmentation fault.
    0x009b54e5 in QLineEdit::setText (this=0x3e4be8, text=@0x22cee8)
    at widgets\qlineedit.cpp:383
    383 widgets\qlineedit.cpp: No such file or directory.
    in widgets\qlineedit.cpp


    i forgot to say what im trying to do.. The idea is just to get the text of the QLineEdit, in this code, im setting the text but it does the same error. thanks in advance!

Similar Threads

  1. Segmentation Fault accessing QListWidget
    By ljshap in forum Newbie
    Replies: 2
    Last Post: 20th December 2008, 13:00
  2. Pointer Question related to QLineEdit
    By ChrisReath in forum Qt Programming
    Replies: 1
    Last Post: 23rd May 2008, 15:13
  3. Replies: 2
    Last Post: 19th May 2007, 18:25
  4. Segmentation fault running any QT4 executables
    By jellis in forum Installation and Deployment
    Replies: 7
    Last Post: 19th May 2007, 16:35
  5. Icons missing => segmentation fault
    By antonio.r.tome in forum Qt Programming
    Replies: 4
    Last Post: 8th March 2006, 16:30

Tags for this Thread

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.