Fetch values from QWidgets added to QTreeWidgets via QTreeWidgetItems

58 Views Asked by At

I've searched many places and found lots of interesting information, but none of that seems to work for what I want. I've tried to follow the solution shown at https://stackoverflow.com/a/9986293/11035837 to no avail.

Basics of my structure: I have a QTreeWidget. I add top level QTreeWidgetItems dynamically (upon the push of a button in a header button box). Each top level QTreeWidgetItem then gets other widgets added to it using:

    QTreeWidget* treeWidget = new QTreeWidget;
    QTreeWidgetItem* new_record = new QTreeWidgetItem;
    QPushButton* add_child = new QPushButton;
    QLineEdit* user_input = new QLineEdit;
    treeWidget->setItemWidget(new_record,1,add_child);
    treeWidget->setItemWidget(new_record,2,user_input);

The add_child button works perfectly. I have a display that inserts all my QLabels, QLineEdits, and QPushButtons in a tree tiered fashion. My buttons work for adding and removing the visual display of items even triggering the visibility of various other elements.

However, I cannot get the user input data out of the QLineEdits to process for anything (such as writing to an output file).

I have my output function iterate through the QTreeWidget:

    QTreeWidgetItemIterator iter(treeWidget);
    while (*iter) 
    {
        stream.writeStartElement("record");
        if ((*iter) != nullptr)
        {
            for (int i = 0; i < 12; i++)
            {
                if((*iter)->text(i) != nullptr) stream.writeAttribute("record_name", (*iter)->text(i));
            }
        }
        for (int i = 0; i < 12; i++)
        {
            if ((*iter) != nullptr && (*iter)->child(i) != nullptr)
            {
                for (int j = 0; j < 12; j++)
                {
                    if ((*iter)->child(i)->text(j) != nullptr)  stream.writeAttribute("record_name", (*iter)->child(i)->text(j));
                }
            }
        }
        ++iter;
    }

This prints as many records with record_name displayed as were created, but it doesn't display any of the other data, because the pointer defined by (*iter)->child(i) is nullptr regardless of i

I then tried using data();

    stream.writeAttribute("record_name ", (*iter)->data(2, Qt::UserRole).toString());

This doesn't err out because of nullptr, but it prints out record_name="" rather than record_name="<user_input>"

I'm able to get the user input for QLineEdit widgets that are not in the QTreeWidget, just not the ones in the tree. I assume if I can figure out how to get the data out of the QLineEdits within the tree that I should be able to adapt that to getting the QLineEdits out of a custom QWidget also within the tree.

1

There are 1 best solutions below

0
wolfsshield On

I found a solution using information from https://www.qtcentre.org/threads/23228-typecast-of-QWidget

stream.writeAttribute("record_name", qobject_cast<QLineEdit*>(treeWidget->itemWidget((*iter), 2))->text() );

The issue was that the QLineEdit widgets were being recalled as QWidgets and not as QLineEdit widgets and as such the text() method/function was not available to it until I cast it back as the desired type.