Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Updating QListView from child dialog

Updating QListView from child dialog

Scheduled Pinned Locked Moved General and Desktop
2 Posts 2 Posters 1.5k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    subrat17june
    wrote on last edited by
    #1

    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()

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      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

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      0

      • Login

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • Users
      • Groups
      • Search
      • Get Qt Extensions
      • Unsolved