PDA

View Full Version : Encountering segmentation fault while accessing QLineEdit



qtzcute
10th August 2009, 06:00
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:


Program received signal SIGSEGV, Segmentation fault.
[Switching to Thread 0xb6e8e8e0 (LWP 4644)]
QLineEditPrivate::setText (this=0x0, txt=@0xbff2c200, pos=-1, edited=false) at widgets/qlineedit.cpp:2909
2909 q->resetInputContext();
(gdb) bt
#0 QLineEditPrivate::setText (this=0x0, txt=@0xbff2c200, pos=-1, edited=false) at widgets/qlineedit.cpp:2909
#1 0xb7b9018b in QLineEdit::setText (this=0x80ce468, text=@0xbff2c200) at widgets/qlineedit.cpp:388
#2 0x08057e3f in sqMap::paint (this=0x80f6278, painter=0xbff2d3ac, option=0x81b7d78, widget=0x8123170) at sqMap.cpp:95
#3 0xb7d8122d in _q_paintItem (item=0x80f6280, painter=0xbff2d3ac, option=0x81b7d78, widget=0x8123170, useWindowOpacity=true,
painterStateProtection=true) at graphicsview/qgraphicsscene.cpp:3754
#4 0xb7d83eca in QGraphicsScenePrivate::drawItemHelper (item=0x80f6280, painter=0xbff2d3ac, option=0x81b7d78, widget=0x8123170,
painterStateProtection=<value optimized out>) at graphicsview/qgraphicsscene.cpp:3810
#5 0xb7d85049 in QGraphicsScene::drawItems (this=0x81a6fb0, painter=0xbff2d3ac, numItems=2, items=0x8140440, options=0x81b7cf4,
widget=0x8123170) at graphicsview/qgraphicsscene.cpp:4036
#6 0xb7d98aac in QGraphicsView::drawItems (this=0x80dcf28, painter=0xbff2d3ac, numItems=2, items=0x8140440, options=0x81b7cf4)
---Type <return> to continue, or q <return> to quit---bt
at graphicsview/qgraphicsview.cpp:3351
#7 0xb7da20c4 in QGraphicsView::paintEvent (this=0x80dcf28, event=0xbff2d8fc) at graphicsview/qgraphicsview.cpp:3096
#8 0xb784384b in QWidget::event (this=0x80dcf28, event=0xbff2d8fc) at kernel/qwidget.cpp:7301
#9 0xb7b7aec3 in QFrame::event (this=0x80dcf28, e=0xbff2d8fc) at widgets/qframe.cpp:651
#10 0xb7c119ef in QAbstractScrollArea::viewportEvent (this=0x80dcf28, e=0x80f9f68) at widgets/qabstractscrollarea.cpp:943
#11 0xb7d9f70f in QGraphicsView::viewportEvent (this=0x80dcf28, event=0xbff2d8fc) at graphicsview/qgraphicsview.cpp:2337
#12 0xb7c13f95 in QAbstractScrollAreaFilter::eventFilter (this=0x80d9710, o=0x8123170, e=0xbff2d8fc)
at widgets/qabstractscrollarea_p.h:96
#13 0xb739cc4a in QCoreApplicationPrivate::sendThroughObjectEventFil ters (this=0x805f1d8, receiver=0x8123170, event=0xbff2d8fc)
at kernel/qcoreapplication.cpp:694



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.


class sqMap : public QObject, public QGraphicsItem, public Ui_MainWindow
{
Q_OBJECT

public:
sqMap(double Width, double Height, ps_info* processInformation, QGraphicsItem *parent = 0);
QRectF boundingRect() const;
void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget = 0);
void updateLocation(int size);
double getWidth();
double getHeight();

protected:
void hoverEnterEvent(QGraphicsSceneHoverEvent *e);
void hoverLeaveEvent(QGraphicsSceneHoverEvent *e);
void mousePressEvent(QGraphicsSceneMouseEvent *e);
void mouseReleaseEvent(QGraphicsSceneMouseEvent *e);
private:
QColor color;
int width;
int height;
bool m_isHovered;
bool m_mouseIsDown; //caters for whether an item has been clicked

};

Definition of paint function for sqMap

void sqMap::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
{
QPen pen;
bool isSelected = (option->state & QStyle::State_Selected) == QStyle::State_Selected;
painter->setBrush(m_isHovered ? Qt::red : color);
painter->drawRect(0,-265,width,height);
if (m_mouseIsDown)
{
lineEdit->setText("something");
}
}


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

franz
10th August 2009, 06:31
QLineEditPrivate::setText (this=0x0, txt=@0xbff2c200, pos=-1, edited=false) at widgets/qlineedit.cpp:2909

There's your problem. The QLineEditPrivate doesn't exist (this == 0x0). Most likely cause is that you don't initialize your QLineEdit.


lineEdit = new QLineEdit(this);

walmac
16th June 2010, 23:58
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


#include <QLabel>
#include <QTextLine>
#include <QtGui>
//#include <QLineEdit>
#include "ojimetro.h"



Window::Window()
{
setFixedSize(800, 600);


QPushButton *salir = new QPushButton(tr("Salir"), this);
// salir->setGeometry(1, 1, 75, 30);
salir->setFont(QFont("Times", 18, QFont::Bold));
QPushButton *probar = new QPushButton(tr("Probar"), this);
//probar->setGeometry(76, 1, 75, 30);
probar->setFont(QFont("Times", 18, QFont::Bold));
QLabel *viendo=new QLabel(tr("viendo"),this);
//viendo->setGeometry(150,1,40,40);
QLineEdit *lineat=new QLineEdit("casa",this);




connect(salir, SIGNAL(clicked()), qApp, SLOT(quit()));
connect(probar,SIGNAL(clicked()),this,SLOT(copia() ));
//renderArea->setGeometry(90,100,350,200);
QGridLayout *mainLayout = new QGridLayout;
mainLayout->setColumnStretch(0, 1);
mainLayout->setColumnStretch(3, 1);
mainLayout->addWidget(renderArea, 0, 0, 1, 4);
mainLayout->setRowMinimumHeight(1, 6);
mainLayout->addWidget(viendo, 2, 1, Qt::AlignRight);
mainLayout->addWidget(lineat, 2, 2);
mainLayout->addWidget(salir, 3, 1, Qt::AlignRight);
mainLayout->addWidget(probar, 3, 2);



setLayout(mainLayout);
//renderArea->adjustSize();
//dato=lineat->text();


setWindowTitle(tr("Ojimetro v0.1"));


}

void Window::copia()
{
int a,b,c;
QString texto1;
int poin;



a=1;

//texto1=lineat->text();
//this->lineat->setText("oo");
//qDebug() << this->lineat->text();
b=2;
c=0;

lineat->setText("hola!!!");
c=a+b;
a=2;



}
when im debugging, the debugger stops at lineat->setText("hola!!!") and shows the segmentation fault
ojimetro.h


#ifndef OJIMETRO_H
#define OJIMETRO_H
#include <QTextLine>
#include <QWidget>
#include <QObject>
QT_BEGIN_NAMESPACE
class QCheckBox;
class QLineEdit;
class QComboBox;
class QLabel;
class QSpinBox;
class QTextLine;
QT_END_NAMESPACE



class Window : public QWidget
{
Q_OBJECT
public:

Window();

public slots:
void copia();


public:
QLineEdit *lineat;
#endif // OJIMETRO_H
};




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!