Error when trying to edit the json information in a file and then repopulating a list
-
Code:
This function populates the Device Listvoid MainWindow::populateDeviceList(bool bUpdate) { if (bUpdate == true) { QLayoutItem* LS; while ((LS = ui->verticalLayoutLedStripDeviceContainer->takeAt(0)) != 0) { if (LS->widget() != NULL) { delete (LS->widget()); } delete LS; } QLayoutItem* LM; while ((LM = ui->verticalLayoutLedMatrixDeviceContainer->takeAt(0)) != 0) { if (LM->widget() != NULL) { delete (LM->widget()); } delete LM; } debugger.LevelInfo("DEVICELIST", "UPDATE", "Update device list", ""); } else { debugger.LevelInfo("DEVICELIST", "INIT", "Initialize device list", ""); } QFile file(save_path + data_folder + device_json); QByteArray jsonArray; if ( file.open(QIODevice::ReadOnly | QIODevice::Text) ) { jsonArray = file.readAll(); debugger.LevelSuccess("DEVICELIST", "OPEN", "File was opened and read", ""); file.close(); } QJsonParseError error; QJsonDocument doc = QJsonDocument::fromJson(jsonArray, &error); QJsonObject object = doc.object(); QJsonArray value = object.find("Device")->toArray(); int cc=0; for (auto v : value) { QPushButton *button = new QPushButton; QMetaEnum metaEnum; metaEnum = QMetaEnum::fromType<Fa5::Glyph>(); button->setObjectName("DeviceButton" + QString::number(cc++)); button->setMinimumHeight(46); button->setMaximumHeight(46); // Convert QString to Char* --------------------------- QString string = v.toObject().value("Icon").toString(); std::string str = string.toStdString(); const char* p = str.c_str(); // ---------------------------------------------------- button->setIcon(ZFontIcon::icon(Fa5::FAMILY, Fa5::SOLID, metaEnum.keyToValue(p), QColor(100, 100, 100), 0.50)); button->setIconSize(QSize(32, 32)); button->setCursor(Qt::PointingHandCursor); #if defined(Q_OS_ANDROID) button->setFont( QFont( QStringList("Bahnschrift Light"), 14, -1, false ) ); #elif defined(Q_OS_WIN32) button->setFont( QFont( QStringList("Bahnschrift Light"), 10, -1, false ) ); #endif button->setCheckable(true); button->setAutoExclusive(true); button->setText(v.toObject().value("Name").toString()); button->setProperty( "orgtext", button->text() ); deviceButtonList.append(button); connect(button, &QPushButton::clicked, [=, ipa = v.toObject().value("IPA").toString(), name = v.toObject().value("Name").toString()]() { connectSocket(ipa); buttonDeviceName = button->objectName(); ui->labelConnectedDevice->setText(name); qDebug() << "Button that was last clicked : " << buttonDeviceName; }); if (v.toObject().value("Type").toString() == "Strip") { ui->verticalLayoutLedStripDeviceContainer->addWidget(button); connectedDeviceType = "Strip"; } else if (v.toObject().value("Type").toString() == "Matrix") { ui->verticalLayoutLedMatrixDeviceContainer->addWidget(button); connectedDeviceType = "Matrix"; } if ( bUpdate == true ) { if ( button->objectName() == buttonDeviceName ) { button->setChecked(true); } } } if ( bUpdate == true ) { debugger.LevelSuccess("DEVICELIST", "UPDATE", "Device list was updated!", "\n"); } else { debugger.LevelSuccess("DEVICELIST", "DONE", "Device list was populated", "\n"); } }
Here is the code that toggles the size of the List Menu
connect(ui->pushButtonToggleMenu, &QPushButton::clicked, [=]() { if (ui->pushButtonToggleMenu->isChecked() == true) { ui->frameDeviceMenu->setMinimumWidth( 52 ); ui->frameDeviceMenu->setMaximumWidth( 52 ); ui->labelLedStripDevices->setText("---"); ui->labelLedStripDevices->setAlignment(Qt::AlignCenter); ui->labelLedMatrixDevices->setText("---"); ui->labelLedMatrixDevices->setAlignment(Qt::AlignCenter); ui->pushButtonDeviceListSettings->setVisible(false); for (QPushButton * dButtons : deviceButtonList) { dButtons->setText(""); } } else { ui->frameDeviceMenu->setMinimumWidth( 264 ); ui->frameDeviceMenu->setMaximumWidth( 264 ); ui->labelLedStripDevices->setText("LED Strip Devices"); ui->labelLedStripDevices->setAlignment(Qt::AlignVCenter); ui->labelLedMatrixDevices->setText("LED Matrix Devices"); ui->labelLedMatrixDevices->setAlignment(Qt::AlignVCenter); ui->pushButtonDeviceListSettings->setVisible(true); for (QPushButton * dButtons : deviceButtonList) { dButtons->setText( dButtons->property("orgtext").toString() ); } } });
now when i update anything from
icon
orname
orip adress
orType
via this functionvoid MainWindow::updateDeviceInfo(int row, QString entryName, QString Value) { QFile file(save_path + data_folder + device_json); file.open(QIODevice::ReadOnly | QIODevice::Text); QJsonParseError JsonParseError; QJsonDocument JsonDocument = QJsonDocument::fromJson(file.readAll(), &JsonParseError); file.close(); QJsonObject RootObject = JsonDocument.object(); QJsonObject RootObject2 = JsonDocument.object(); QJsonArray Array = RootObject.find("Device")->toArray(); RootObject = Array[row].toObject(); RootObject[entryName] = Value; Array[row] = RootObject; RootObject2["Device"] = Array; JsonDocument.setObject(RootObject2); file.open(QFile::WriteOnly | QFile::Text | QFile::Truncate); file.write(JsonDocument.toJson()); file.close(); updateDeviceInfo(true); }
and then try to toggle the list container to minimize it crashes (i ran it in debugger) but im terrible at reading it so here is a image
what and where is the error causing this?
-
i got it working! :)
when i repopulated the list container i only deleted
QLayoutItem* LS; while ((LS = ui->verticalLayoutLedStripDeviceContainer->takeAt(0)) != 0) { if (LS->widget() != NULL) { delete (LS->widget()); } delete LS; } QLayoutItem* LM; while ((LM = ui->verticalLayoutLedMatrixDeviceContainer->takeAt(0)) != 0) { if (LM->widget() != NULL) { delete (LM->widget()); } delete LM; }
i needed to clear the
Qlist
aswelldeviceButtonList.clear();
-
hmmm so i did this
connect(ui->pushButtonToggleMenu, &QPushButton::clicked, [=]() { if (ui->pushButtonToggleMenu->isChecked() == true) { ui->frameDeviceMenu->setMinimumWidth( 52 ); ui->frameDeviceMenu->setMaximumWidth( 52 ); ui->labelLedStripDevices->setText("---"); ui->labelLedStripDevices->setAlignment(Qt::AlignCenter); ui->labelLedMatrixDevices->setText("---"); ui->labelLedMatrixDevices->setAlignment(Qt::AlignCenter); ui->pushButtonDeviceListSettings->setVisible(false); /* for (QPushButton * dButtons : deviceButtonList) { dButtons->setText(""); } */ } else { ui->frameDeviceMenu->setMinimumWidth( 264 ); ui->frameDeviceMenu->setMaximumWidth( 264 ); ui->labelLedStripDevices->setText("LED Strip Devices"); ui->labelLedStripDevices->setAlignment(Qt::AlignVCenter); ui->labelLedMatrixDevices->setText("LED Matrix Devices"); ui->labelLedMatrixDevices->setAlignment(Qt::AlignVCenter); ui->pushButtonDeviceListSettings->setVisible(true); /* for (QPushButton * dButtons : deviceButtonList) { dButtons->setText( dButtons->property("orgtext").toString() ); } */ } });
I blanked out
/* for (QPushButton * dButtons : deviceButtonList) { dButtons->setText(""); } */ // and /* for (QPushButton * dButtons : deviceButtonList) { dButtons->setText( dButtons->property("orgtext").toString() ); } */
and now when i change anything from
icon
orname
orip adress
orType
via this function it just works :S i can toggle the menu without it crashing :S but why?everytime i change something i ALWAYS run this function
updateDeviceInfo(true);
that deletes all entries in thetableWidget
and repopulates it... -
i got it working! :)
when i repopulated the list container i only deleted
QLayoutItem* LS; while ((LS = ui->verticalLayoutLedStripDeviceContainer->takeAt(0)) != 0) { if (LS->widget() != NULL) { delete (LS->widget()); } delete LS; } QLayoutItem* LM; while ((LM = ui->verticalLayoutLedMatrixDeviceContainer->takeAt(0)) != 0) { if (LM->widget() != NULL) { delete (LM->widget()); } delete LM; }
i needed to clear the
Qlist
aswelldeviceButtonList.clear();