PDA

View Full Version : Segmentation Fault accessing QListWidget



ljshap
20th December 2008, 00:29
I am a total QT noob !!!!!

I am receiving a SIGSEGV error when I try adding an item to a QListWidget object. The objected was created in the class constructor and is part of the constructor layout. It crashes when I try adding an item in a seperate function. I have no problems adding to the object in the constructor or adding items to an object created in the function but can not add to the constructor object from the separate function:

Any suggestions would be appreciated especially since it may help explain a lot of other problems.

Thanks
------------------------------

The following is an extract of the code:


//direct.h

#include <QPushButton>
//
class direct : public QDialog
{
Q_OBJECT


public:


QListWidget * listWidget ; //= new QListWidget;
QPushButton * button1 ;

public slots:
void getSelectedNames();

public:
direct();

};
#endif


//================================================== =====
// S o u r c e F i l e
//++++++++++++++++++++++++++++++++++++++++++++++++++ +++++
#include "direct.h"

direct::direct( ) : QDialog()
{

QVBoxLayout * mainLayout = new QVBoxLayout(this);

QPushButton * button1 = new QPushButton("List Files");
QPushButton * quit = new QPushButton("Quit");

// mainLayout->addWidget(listWidget);
mainLayout->addWidget(button1);


// Conections
connect (button1 , SIGNAL (clicked () ), this, SLOT (getSelectedNames () ) );
connect (quit, SIGNAL (clicked() ), this, SLOT (accept() ) );
}




void direct::getSelectedNames()
{


for (int i1 =0; i1<45;i1++)
{
//listWidget->setCurrentRow(i1);
newList->addItem("xxx"); // SEGMENTATION FAULT - but not if in the constructor

}

munna
20th December 2008, 08:31
Add the following line in your constructor


listWidget = new QListWidget(this);

Also, I see that the following line is not correct



newList->addItem("xxx"); // SEGMENTATION FAULT - but not if in the constructor


There is no variable called newList

ljshap
20th December 2008, 13:00
Add the following line in your constructor


listWidget = new QListWidget(this);

Including "this" did the trick. // Thanks


[QUOTE]There is no variable called newList

That was just a mistake in trimming the code, I tried a lot of different things.

Thanks again for your help!