Search code examples
c++qtqt6

Qt6: "Unable to read Memory" when pointing to a QLineEdit from a QFormLayout


I want to get the text from a QLineEdit, which is in a QFormLayout, to save it to a File. The saving works fine, but I am not able to get the text form the QLineEdit and when I look at it from the Debugger it says "Unable to read Memory". I can´t figure out how to correctly point to the QLineEdit, so that I can get the text.

With this code I want to get the text:

QJsonArray Kegelbuch::saveSettings() {
    QFormLayout* formLayout = (QFormLayout*)ui.einstellungenTab->layout();
    QJsonArray data;
    QLineEdit* settingsEdit;

    for (int i = 0; i < formLayout->rowCount(); i++) {
        settingsEdit = (QLineEdit*)formLayout->itemAt(i, QFormLayout::ItemRole::FieldRole);
    }

    return data;
}

How the window looks: EinstellungentTab


Solution

  • Replace

    settingsEdit = (QLineEdit*)formLayout->itemAt(i, QFormLayout::ItemRole::FieldRole);

    with

    settingsEdit = (QLineEdit*)(formLayout->itemAt(i, FormLayout::ItemRole::FieldRole)->widget());

    Background: itemAt() returns a QLayoutItem*, so you need to call QWidget *QLayoutItem::widget() to get the widget.