Qt - Nested QTableView/QTableView displaying QStandardItemModel w/ column entries



  • Hi all, I've been working on this for awhile while learning Qt and I haven't figured it out via the online documentation or forums, so I thought I'd ask. I'm basically trying to create a nested table.

    I have a .csv file being read into a QStandardItemModel and being displayed by a QTableView. The .csv file has a '|' separated field (call it flags) which I want to expand vertically in the view. Condensened, the .csv looks something like this:
    NAME, FLAGS
    item1, a|b
    item2, c

    And I would like something like this displayed:
    @
    NAME, FLAGS
    item1, a
    b
    item2, c
    @

    So, I create a QStandardItem and add the values 'a','b','c','d' in a QList<QStandardItem*> using appendColumn, and put this returned item into the QStandardItemModel. (This is the code below).

    What happens, is the flags entries are blank. What I think is happening, is the value of QStandardItem in the Model is empty, the children of QStandardItem in the column I created are there, and QTableView doesn't support showing the children.

    I spent a lot of time looking for the function that controls this behavior so I could override it for this particular column, but I can't find it.
    I also thought about just putting a blank entry in all the other columns, but this doesn't seem as clean as nesting the flags into a QStandardItem.

    I'm still learning Qt, so explanations of what Qt is doing are appreciated. Suggested solutions also appreciated. I'm not set on the QTableView and QStandardItemModel format, these just seemed most appropriate to me at the time. Thanks in advance!

    @
    QStandardItem* ParseFlags(QString flagString) {
    QStandardItem* ret = new QStandardItem();
    QChar charIn; QString tempString;
    QList<QStandardItem*> list;
    for (int i=0; i<flagsString.length(); i++) {
    charIn = flagsString[i]; // Get char
    if (charIn == flagSeparator) { // If we hit the separator
    list.append(new QStandardItem(tempString)); // add the string to the list
    tempString.clear();
    continue;
    }
    tempString.append(charIn);
    }
    // If we have a final string, add it to the list
    if (!tempString.isEmpty()) {
    list.append(new QStandardItem(tempString));
    }

    ret->appendColumn(list); // append the list we created to the return object
    return(ret);
    

    }
    @



  • Update:
    I came up with a pretty easy way to do this, but its basically a workaround for insufficient Qt knowledge.

    I create a display string with "\n" between flags and use the setData function to display it. I marked the added lines in the code below for anyone needing a similar solution.

    I think having the data in columns will make it easier to sort/filter for specific flags later, with a little extra work of modifying the display string each time. I'm not sure though, I haven't dived into that yet.

    If anyone can enlighten me on Qt's display behavior, or a different way to do this I'd still be interested in hearing it.

    @
    QStandardItem* ParseFlags(QString flagString) {
    QStandardItem* ret = new QStandardItem();
    QChar charIn; QString tempString;
    QString displayString; // NEW
    QList<QStandardItem*> list;
    for (int i=0; i<flagsString.length(); i++) {
    charIn = flagsString[i]; // Get char
    if (charIn == flagSeparator) { // If we hit the separator
    list.append(new QStandardItem(tempString)); // add the string to the list
    displayString.append(tempString); // NEW
    displayString.append("\n"); // newline char
    tempString.clear();
    continue;
    }
    tempString.append(charIn);
    }
    // If we have a final string, add it to the list
    if (!tempString.isEmpty()) {
    list.append(new QStandardItem(tempString));
    displayString.append(tempString); // NEW
    }

    ret->appendColumn(list); // append the list we created to the return object
    ret->setData(displayString, 0); // NEW 
    return(ret);
    

    }
    @


Log in to reply
 

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