Is creating a custom model the way to access data in a table by name? - c++

Say I have a QTableWidget with 10 columns. I set data in the table with
QTableWidgetItem* textItem1 = new QTableWidgetItem;
textItem1->setData(Qt::DisplayRole, 20);
this->tableWidget->setItem(row, col, textItem1);
The problem is that if I change the order or titles of the columns in the table using QtDesigner, I have to go through my code and change all of the column numbers. I would rather do something like this:
QTableWidgetItem* textItem1 = new QTableWidgetItem;
textItem1->setData(Qt::DisplayRole, "Smith");
this->tableWidget->setLastName(row, textItem1);
to add "Smith" to the "LastName" column, without needing to know which column index LastName currently is. Is the way to do this with a custom Model? I looked into QAbstractTableModel, but I don't understand how to provide named access to data? Can anyone briefly explain how one would do this? And if this is a reasonable/common thing to want to do?
Thanks.

I posted an example of using QAbstractTableModel:
http://programmingexamples.net/index.php?title=Qt/ModelView/QAbstractTableModel
As you can see, the data is simply stored in a member variable, and can therefore be accessed however you would like.

Related

Power Query: How do I add a specific List/Vector as a Column

I have a simple problem. Let's assume I have the following
Now I want to add a specific vector or list for example (1,2,2,1) as an additional column and it should look like
When I add "Custom Column", how do I have to enter this list that it creates exactly this column?
It should not be a index column or a calculated column because I want to specify the values depending on another column manually. It seems like it is a very simple task, but I couldn't find any solutions yet! (I feel very stupid xD)
pls help
Thank you!
let Source = #table({"Column1"},{{"A"},{"B"},{"C"},{"D"}}),
Add = {{1,2,2,1}},
part1=Table.ToColumns(Source) & Add,
part2= Table.FromColumns(part1,Table.ColumnNames(Source)&{"Column2"})
in part2
or perhaps, if grabbing column from another table
let Source = #table({"Column1"},{{"A"},{"B"},{"C"},{"D"}}),
part1=Table.ToColumns(Source) & Table.ToColumns(Table.SelectColumns(SomeOtherTable,"Column9")),
part2 = Table.FromColumns(part1,Table.ColumnNames(Source)&{"Column2"})
in part2
Copy your Column1
Open the Enter Data / Create Table UI and paste Column1
Add your specific vector as Column2 and save the new table
Merge the new table into your original table using both Column1
Note that Column1 has to be a key column (unique values). If it's not, create a temporary index column and use that for merging.

How to add a new column with custom values, based on a WHERE clause from another table in PowerBi?

I am stuck while dynamically forming a new column based certain WHERE clause from another Table in PowerBi. To give more details, let's say I have a table with item numbers associated with a Customer Name. In another table, I have to add a new column, which will dynamically add the item numbers associated with a particular customer and append as a query parameter to a base url.
So, my first table looks like this:
The second table that I want is this:
The query parameter value in the URL, has to be dynamically based on a SELECT query with a WHERE clause and pick up the ItemNumbers using the Customer field which is common between both. So, how can this be done in PowerBi? Any help would be really appreciated :)
I have one table in my model "TableRol" if I want to summarize my Date as the string I can use CONCATENATEX;
URL = CONCATENATE(CONCATENATE("http:\\mysite.com\parametersHere\getitem?='",CONCATENATEX(VALUES('TableRol'[Date]), 'TableRol'[Date],";")),"'")

Converting JSON into Table (PowerQuery)

What would be a correct PowerQuery syntax to extract the information from this Web JSON into a table:
I'm not very familiar with PowerQuery, and this is probably the only time I'll need this, so I'd be grateful if someone would help me out without refering me to documentation. Thanks
[{"time_entry_group": {"minutes": 301,"time_entries_params": {"locked": "0","from": "2021-02-01","to": "2021-02-28","customer_id": "11223344","project_id": "223388","service_id": "435248"},"revenue": 57691.6666666667,"project_id": 223388,"project_name": "Scrb","service_id": 435248,"service_name": "Meetings","month": "202102"}}
, {"time_entry_group": {"minutes": 1175,"time_entries_params": {"locked": "1","from": "2021-01-01","to": "2021-01-31","customer_id": "11223344","project_id": "223388","service_id": "421393"},"revenue": 225208.333333333,"project_id": 223388,"project_name": "Scrb","service_id": 421393,"service_name": "Design","month": "202101"}}
, {"time_entry_group": {"minutes": 24,"time_entries_params": {"locked": "1","from": "2021-01-01","to": "2021-01-31","customer_id": "11223344","project_id": "3168911","service_id": "95033"},"revenue": 4600.0,"project_id": 3168911,"project_name": "youkn Dev","service_id": 95033,"service_name": "Reviews","month": "202101"}}]
For future reference, if you have a column that you need to expand, you can instead click this arrow icon to the right of the column name. Clicking it should display a menu that should then allow you to specify which nested columns you want to get expand or get at. To be clear, it will expand that column for all rows in that table, not just one.
The JSON you've included is basically an array of objects, so maybe use:
Json.Document to parse the JSON, which should give you a list of records
Table.FromRecords to turn the list of records into a table.
Table.ExpandRecordColumn to expand a nested record columns.
Example implementation:
let
json = "[{""time_entry_group"":{""minutes"":301,""time_entries_params"":{""locked"":""0"",""from"":""2021-02-01"",""to"":""2021-02-28"",""customer_id"":""11223344"",""project_id"":""223388"",""service_id"":""435248""},""revenue"":57691.6666666667,""project_id"":223388,""project_name"":""Scrb"",""service_id"":435248,""service_name"":""Meetings"",""month"":""202102""}},{""time_entry_group"":{""minutes"":1175,""time_entries_params"":{""locked"":""1"",""from"":""2021-01-01"",""to"":""2021-01-31"",""customer_id"":""11223344"",""project_id"":""223388"",""service_id"":""421393""},""revenue"":225208.333333333,""project_id"":223388,""project_name"":""Scrb"",""service_id"":421393,""service_name"":""Design"",""month"":""202101""}},{""time_entry_group"":{""minutes"":24,""time_entries_params"":{""locked"":""1"",""from"":""2021-01-01"",""to"":""2021-01-31"",""customer_id"":""11223344"",""project_id"":""3168911"",""service_id"":""95033""},""revenue"":4600,""project_id"":3168911,""project_name"":""youkn Dev"",""service_id"":95033,""service_name"":""Reviews"",""month"":""202101""}}]",
parsed = Json.Document(json),
initialTable = Table.FromRecords(List.Transform(parsed, each [time_entry_group])),
expanded = Table.ExpandRecordColumn(initialTable, "time_entries_params", {"locked", "from", "to", "customer_id"})
in
expanded
One thing about the code above is that it doesn't expand nested fields project_id and service_id (present within time_entries_params). This is because these columns already exist in the table (and having duplicate column names would cause an error). I've assumed this isn't a problem, as the nested values aren't different.

Filling a DataGridView ComboBox with values in C/C++

I'm creating a windows forms application for university. My data is stored in multible c arrays of abstract data types. Each array is displayed in it's own datagridview. This all works no problem.
Since the data is connected like in a database I added a new column to my parent table and set the ColumnType to DataGridViewComboBoxColumn. Now I need to fill this combobox with the data of a different array / datagridview.
I've already tried to add the options manually from my array as discribed in this question: Filling DataGridView ComboBox programatically in unbound mode?. Since I need to use C/C++ and this example uses vb the used functions are not availible. I can select my column dataGridView1->Columns[0] but the ->Items does not exist.
Is it possible to create a datasource from my arrays to link them directly? Or have I overlooked a way to add them manually from my array?
You don't provide any code, neither you describe well where did you ran into a problem. Assuming you're after unbound list to be filled into the DataGridViewComboBoxColumn, here is a simple example.
C#:
DataTable dt;
dt.Columns.Add("ID", typeof(Int32));
dt.Columns.Add("Category", typeof(string));
dt.Rows.Add({1, "Fruits"});
dt.Rows.Add({2, "Vegetables"});
dt.Rows.Add({3, "Trees"});
DataGridViewComboBoxColumn dgCol = this.DataGridView1.columns("Category");
dgCol.ValueMember = "ID";
dgCol.DisplayMember = "Category";
dgCol.DataSource = dt;
In this example, we create a DataTable, fill it with desired values and use it as a DataSource of the DataGridViewComboBoxColumn. Obviously, your values provided to this column in the DataGridView DataSource must match IDs in the list in new DataTable "dt".
You can convert an array or other data into DataTable or even use them directly as DataSource. It depends on what data you have. The principle will be very similar, though.
VB.NET:
Dim dt As DataTable
dt.Columns.Add("ID", GetType(Int32))
dt.Columns.Add("Category", GetType(String))
dt.Rows.Add({1, "Fruits"})
dt.Rows.Add({2, "Vegetables"})
dt.Rows.Add({3, "Trees"})
Dim dgCol As DataGridViewComboBoxColumn = Me.DataGridView1.columns("Category")
dgCol.ValueMember = "ID"
dgCol.DisplayMember = "Category"
dgCol.DataSource = dt

list of SelectedRows in QTableWidget

I have problem getting selected rows from QTableWidget. I have table like this:
[id] [ key ]
0 test
1 pass
I want to get every row's key values. I tried QTableWidget->selectedIndexes(); but it says it's protected and I can't access that. When I tried QTableWidget->SelectionModel->selectedIndexes, I don't know how to loop through list and get the key values. Do anyone know better way how can I do it?
Regards.
I'm assuming that you set the selection behavior of your table widget to select rows.
You can always access the so-called "selection model" of any item view/widget. QTableWidget inherits from QAbstractItemView, which gives you access to this special model. This model can tell you the selected rows as a list of QModelIndex, which can then tell you the row. Once you've got them, you can access the table content, in your case the text in the column with index 1 (key column).
static const KEY_COLUMN = 1;
QList<QString> selectedKeys;
QItemSelectionModel *selectionModel = ui->tableWidget->selectionModel();
foreach(QModelIndex index, selectionModel->selectedRows())
selectedIDs << ui->tableWidget->item(index->row(), KEY_COLUMN)->text();
Because you are using QTableWidget, you probably want to be calling selectedItems(). Your results will be based on what you have set the selection behavior to, via setSelectionBehavior()
When you have a list of items, you can specifically get the second column item (if it wasn't selected already):
QTableWigetItem *keyItem = table->item(anItem->row(), 1);
QString val = keyItem->text();