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. [SOLVED]QModelIndex and signal from TreeView

[SOLVED]QModelIndex and signal from TreeView

Scheduled Pinned Locked Moved General and Desktop
3 Posts 1 Posters 1.3k 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.
  • JeroentjehomeJ Offline
    JeroentjehomeJ Offline
    Jeroentjehome
    wrote on last edited by
    #1

    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;
    

    };
    @

    Greetz, Jeroen

    1 Reply Last reply
    0
    • JeroentjehomeJ Offline
      JeroentjehomeJ Offline
      Jeroentjehome
      wrote on last edited by
      #2

      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.

      Greetz, Jeroen

      1 Reply Last reply
      0
      • JeroentjehomeJ Offline
        JeroentjehomeJ Offline
        Jeroentjehome
        wrote on last edited by
        #3

        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.

        Greetz, Jeroen

        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