[SOLVED]QModelIndex and signal from TreeView



  • Hi all who read this.
    For a Wizard I created a wizardpage that contains a QTreeView and a QFileSystemModel for the selection of a file to read. This all goes pretty well, but now I want to implement a "file clicked" and enable the next button. I read that you need to overwrite the isCompete function. Done this. No problem, works, but now I need to validate the selected file before I enable the next button.
    I tried to connect the QTreeView clicked signal with the QModelIndex to a slot in the same wizardpage checkClicked(const QModelIndex Index).
    Somehow the Index I receive is invalid (debugger), but the Index.isValid gives true anyway. Reading the data from it then crashes. Very strange if you ask me. Any thoughts?? Here is my snippet of code:
    @
    //============================================================================
    // Select File page
    //============================================================================
    OpenFilePage::OpenFilePage(QWidget parent)
    : QWizardPage(parent)
    {
    setTitle(tr("File Selection"));
    setSubTitle(tr("Select the memory map file (
    .map)"));

    // Insert the File open View/Model
    m_View = new QTreeView;
    m_SysModel = new QFileSystemModel;
    m_SysModel->setRootPath(QDir::currentPath());
    m_SysModel->setNameFilters(QStringList("*.map"));
    
    m_View->setModel(m_SysModel);
    for (int Column_i = 1; Column_i < m_SysModel->columnCount(); Column_i++)
    {
        m_View->hideColumn(Column_i);
    }
    
    QVBoxLayout *layout = new QVBoxLayout;
    layout->addWidget(m_View);
    
    m_FileName = new QLabel(this);
    registerField("FileName*", m_FileName);
    layout->addWidget(m_FileName);
    setLayout(layout);
    
    // Set the next button gray until file selected
    connect(m_View, &QTreeView::clicked,
            this, &OpenFilePage::checkClicked);
    

    }

    /*****************************************************************************

    • Function Name:
    • Return :
    • Description :
      //*
    • \brief

    ******************************************************************************/
    void OpenFilePage::checkClicked(const QModelIndex &Index)
    {
    QString FileName_str;

    if (Index.isValid() == true)
    {
        QVariant Data_var = Index.data(Qt::DisplayRole);
        FileName_str = Data_var.toString();
    }
    
    // When the item returned is a file with .map in the end:
    if (FileName_str.contains(".map") == true)
    {
        // Set the FileName field
        m_FileName->setText(m_SysModel->data(Index).toString());
        // Enable next button
         completeChanged();
    }
    

    }

    // And the class definition:
    class OpenFilePage : public QWizardPage
    {
    Q_OBJECT
    public:
    explicit OpenFilePage(QWidget * parent = 0);
    void checkClicked(const QModelIndex &Index);

    private:
    bool isComplete(void);

    QTreeView * m_View;
    QFileSystemModel * m_SysModel;
    QLabel  * m_FileName;
    

    };
    @



  • Btw:
    @QVariant Data_var = Index.data(Qt::DisplayRole);@
    is the line that gives the segfault. (no suprise with the index being invalid according the debugger.



  • Oke, strange,
    When setting the debugger below the index.data() function the read data is correct and it doesn't crash. So it's not the code that was faulty, but the debugger somehow.
    Greetz to all.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.