Updating QListView from child dialog
-
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:nbtnclicked()
{
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\med iaa\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()
-
Hi and welcome to devnet.
There are several things that are wrong here:
- you don't check that parent is valid (and in your case it won't be since you don't give a parent to your dialog)
- you use c style cast rather than at least dynamic_cast and since it's a QObject use qobject_cast
- you are trying to modify a parent from a children (this is bad design)
Cleaner approach:
- Give your dialog a getter for the path
- Update myapp once your returned from the dialog.
Since it's a path, I presume it's file system related, thus did you had a look at QFileDialog ?
Hope it helps