Reputation: 6147
I'm coming from a background of python and pyside. So I apologize for anything that may appear as an odd practice in C++.
Two questions:
I want to populate a table with a set of data which is currently a list of dictionaries, as seen in my original python code. How would this be translated into my C++ Qt project so i can populate my tablewidget using this data? It doesn't appear that Qt uses dictionaries like python, it uses QMap.
It looks like Qt uses a QMap variant, not dictionaries. I'm open to formatting the data differently if there is a more efficient way of doing so. Whatever will make it easy to save to a text file and at the same time load from a file it populate the ui. It's important to mention that this data will eventually be saved to a file and loaded from a file. That's why my old python project uses json as the format.
My goal is to eventually extend my subclass of QTableWidget to add methods for loading from a file and saving to a file. Similar to presets.
List of Dictionaries:
[
{
"key":"SHOT",
"value":"",
"description":"Current Shot node name",
},
{
"key":"PASS",
"value":"",
"description":"Current Pass node name",
},
{
"key":"SELF",
"value":"",
"description":"Current node name",
},
{
"key":"MM",
"value":"",
"description":"Current month as integer ex: 12",
},
{
"key":"DD",
"value":"",
"description":"Current day as integer ex: 07",
}
]
cpp
#include "tokeneditor.h"
#include <QTableWidget>
#include <QVBoxLayout>
TokenEditor::TokenEditor(QWidget *parent) : QWidget(parent)
{
// Controls
QTableWidget *ui_userTokens = new QTableWidget();
// Layout
QVBoxLayout * layout = new QVBoxLayout();
layout->setMargin(0);
layout->setSpacing(0);
layout->addWidget(ui_userTokens);
setLayout(layout);
// populate table with data...
}
Upvotes: 2
Views: 1701
Reputation: 243887
As noted in the comments Qt supports Json, in the next part I show you an example, plus you should consider that C ++ gives freedom to handle memory so take into account in eliminating it if necessary, in the case of Qt many times it is give that responsibility to Qt through the kinship tree.
*.h
#ifndef TOKENEDITOR_H
#define TOKENEDITOR_H
#include <QWidget>
class QTableWidget;
class TokenEditor : public QWidget
{
Q_OBJECT
public:
explicit TokenEditor(QWidget *parent = nullptr);
~TokenEditor();
private:
QTableWidget *ui_userTokens;
};
#endif // TOKENEDITOR_H
*.cpp
#include "tokeneditor.h"
#include <QJsonDocument>
#include <QJsonObject>
#include <QJsonArray>
#include <QTableWidget>
#include <QVBoxLayout>
TokenEditor::TokenEditor(QWidget *parent) :
QWidget(parent),
ui_userTokens(new QTableWidget)
{
const std::string json = R"([{
"key": "SHOT",
"value": "",
"description": "Current Shot node name"
},
{
"key": "PASS",
"value": "",
"description": "Current Pass node name"
},
{
"key": "SELF",
"value": "",
"description": "Current node name"
},
{
"key": "MM",
"value": "",
"description": "Current month as integer ex: 12"
},
{
"key": "DD",
"value": "",
"description": "Current day as integer ex: 07"
}
])";
QJsonDocument doc = QJsonDocument::fromJson(QByteArray::fromStdString(json));
auto layout = new QVBoxLayout(this);
layout->setMargin(0);
layout->setSpacing(0);
layout->addWidget(ui_userTokens);
ui_userTokens->setRowCount(5);
ui_userTokens->setColumnCount(3);
ui_userTokens->setHorizontalHeaderLabels({"key", "value", "description"});
int r=0;
for(const QJsonValue & val : doc.array()){
QJsonObject obj = val.toObject();
int c=0;
for(const QString & key: obj.keys()){
auto *it = new QTableWidgetItem(obj[key].toString());
ui_userTokens->setItem(r, c, it);
c++;
}
r++;
}
}
TokenEditor::~TokenEditor()
{
}
Upvotes: 3