PDA

View Full Version : Updating QListView from child dialog



subrat17june
27th May 2013, 11:46
Hi All,
I am new to QT programming and this forum also.
So please spare any misinterpretations.

I have a main window application. when launched, the main application calls a function to search system drives, find particular files and update in the list view. works fine.
i am launching a child dialog from the main window. and a task is to update the main window list view. when i am calling the main window update function to update the list view i am getting a segmentation fault.

i am pasting the code below...
please can i get some help on how to do it properly, or what is the error i am doing.


myapp::myapp(QWidget *parent):QMainWindow(parent),ui(new Ui::myapp)
{
ui->setupUi(this);
ui->listView->setViewMode(QListView::IconMode);
// setting the model to the current dialog , value declared in header
model = new QStandardItemModel(this);
// function to enumerate and update volumes to list
updateList();
}

Void myapp::updateList()
{
// function gets specific files from all the local drives and calls
The updateListView(passing file name as parameter)
updateListView(filename);
}

// function to launch the dialog from main application
void myapp::onbtnclicked()
{
mydialog mdiaog;
mdialog.setModal(true);
mdialog.exec();
}


// function to insert the value of file into the QFileList
void myapp:: updateListView (QString file)
{
int row = model->rowCount();
model->insertRows(row,1);

QStandardItem * item = new QStandardItem(QIcon("C:\\Users\\Test\\Desktop\\mediaa\\red.bmp"),file);

model->appendRow(item);
ui->listView->setIconSize(QSize(70,80));
ui->listView->setModel(model);

}

WORKS FINE ON APPLICATION INITIALIZATION

But when called from a child dialog, i.e. I am calling the updateListView(QString fie) from the child dialog, which is launched by the above main application, segmentation error is caused.

Code I am using to call the main application function from child dialog is

bool myDialog::updateGui(QString path)
{
((myapp*)parent())->updateListView (fileName);
return true;
}

Function is called OK, but throws segmentation error in Main Applications, updateListView()

Santosh Reddy
27th May 2013, 11:52
You missed to set the parent.


void myapp:nbtnclicked()
{
mydialog mdiaog(this); //<<<<<<<<<<
mdialog.setModal(true);
mdialog.exec();
}


EDIT: Make sure the parent is being passed on to the QDialog in the mydialog ctor.

subrat17june
27th May 2013, 11:59
Hello santosh,
I did not get exactly what to modify ?

should i declare the parent for the child dialog in the constructor or should i pass the parent while creating the child dialog ??

any code help would be great.

Santosh Reddy
27th May 2013, 12:04
Show mydialog ctor

subrat17june
27th May 2013, 12:43
mydialog::mydialog(Qwidget *parent) : Qdialog (parent), ui(new Ui::myDialog)
{
ui->setupUi(this);

}

Santosh Reddy
27th May 2013, 13:08
Do this modification


void myapp:nbtnclicked()
{
mydialog mdiaog(this); //<<<<<<<<<< add "this"
mdialog.setModal(true);
mdialog.exec();
}