PDA

View Full Version : Segment fault when using Q3ListBox and QFile



hiuao
11th May 2007, 06:36
Hi,all.
I create a window subwin in one dialog and define Q3ListBox listbox in it.Then I redefine the Q3ListBox in mainwindow as listbox = subwin.listbox,the QFile file is defined in the mainwindow driectly.there are some lines in my code when I add them the error is segment fault.
The lines are as follows:
char ReadData[100];
int i=0;
int point = listbox->numRows();
file.seek(0);
while(!file.atEnd()){
file.readLine(ReadData,100);
strTempSource = QString(ReadData);
list->insertItem(strTempSource,point+i);
i++;
}
Can anyone help me?

marcel
11th May 2007, 06:42
You may get a buffer overrun in ReadData because readLine also appends aNULL character at the end of what it reads. Try making it larger ( like 101 ).

Also, make sure list is a valid pointer. Print it's address with qDebug, or something like this.

Regards

hiuao
11th May 2007, 10:58
You may get a buffer overrun in ReadData because readLine also appends aNULL character at the end of what it reads. Try making it larger ( like 101 ).

Also, make sure list is a valid pointer. Print it's address with qDebug, or something like this.

Regards
Well,I have tested it line by line,and on the first line : while( !file.atEnd()),it can't run properly.
In a small programm for test,it can run properly.The only difference is I define the Q3ListBox in it directly.:(
I have no idea what to do next.Please help!Thank you very much!

marcel
11th May 2007, 11:13
Could you post the code where you declare and create the list box?

hiuao
11th May 2007, 11:34
Could you post the code where you declare and create the list box?
Draw the subwindow .h file
class QAddPlusSource : public QDialog{
public:
QAddPlusSource();
public:
Q3ListBox *m_lstSource;
QPushButton *m_btnSourceAdd;
QPushButton *m_btnSourcePlus;

};
subwindow's .cpp
QAddPlusSource::QAddPlusSource(){

setGeometry(690,560,568,348);
m_lstSource = new Q3ListBox(this);
m_lstSource->setGeometry(QRect(140,20,411,311));

m_btnSourceAdd = new QPushButton(this);
m_btnSourceAdd->setGeometry(QRect(40,100,80,27));
m_btnSourceAdd->setText("Add");
m_btnSourceAdd->setFont(QFont("Times",13,QFont::Bold));

m_btnSourcePlus = new QPushButton(this);
m_btnSourcePlus->setGeometry(QRect(40,180,80,27));
m_btnSourcePlus->setText("Plus");
m_btnSourcePlus->setFont(QFont("Times",13,QFont::Bold));
}
define in the mainwindow's .h file
.....
QAddPlusSource AddPlusSource;
Q3ListBox *m_lstSource;
.......
in the mainwindow's .cpp file
m_lstSource = new Q3ListBox;
m_lstSource = AddPlusSource.m_lstSource;
Thank you marcel very much!

marcel
11th May 2007, 12:13
OK. I believe that the dialog is deleted and the list from the main window remains invalid.

Could you post the part where you show the dialog? How is it declared? As a member or somewhere local.

Regards

hiuao
11th May 2007, 12:31
OK. I believe that the dialog is deleted and the list from the main window remains invalid.

Could you post the part where you show the dialog? How is it declared? As a member or somewhere local.

Regards
The dialog's declaration is as I have said above.
There is a pushbutton on the mainwindow,when the pushbutton is clicked,then the dialog is shown,just as:
void Mainwindow::ShowAddPlusSource(){
AddPlusSource.show();
.....
tranform the data on it to some parameters which defined in mainwindow
show some data on parts of the mainwindow
}

I don't use the dialog too much in mainwindow.

hiuao
12th May 2007, 12:42
I know my mistake now.In
file.readLine(ReadData,200);
I define ReadData as char *ReadData,it is wrong! I should define it as char ReadData[200].
Thank you marcel for your help!:)

marcel
12th May 2007, 12:51
But in the first post was defined as char ReadData[100]...
It is pk to define it as char *ReadData, but you should also allocate it with ReadData = new char[200].

Then, when you don't need it anymore, you must delete it with delete[] ReadData.

Regards