How to get this QTableWidget to display items? - qt

I have a QTableWidget and I can't get anything to show up in it.
The following appears in the constructor of the main window:
ui->tableWidget->setItem(0,0,new QTableWidgetItem("Item1"));
ui->tableWidget->setItem(0,1,new QTableWidgetItem("Item2"));
ui->tableWidget->setItem(0,2,new QTableWidgetItem("Item3"));
When I run the application, the table widget shows up, but the items do not.
I tried adding ui->tableWidget->insertRow(0); before the above code, but it didn't work.

Aha! I figured out what was going on... I needed to tell the control the number of rows it should have:
ui->tableWidget->setRowCount(2);

Example code:
//this will give the present number of rows available.
int insertRow = ui->tableWidget->rowCount();
//insert the row at the bottom of the table widget - using.
ui->tableWidget->insertRow(insertRow);
//After a new row is inserted we can add the table widget items as required.
ui->tableWidget->setItem(insertRow,0,new QTableWidgetItem("Item1"));
ui->tableWidget->setItem(insertRow,1,new QTableWidgetItem("Item2"));
ui->tableWidget->setItem(insertRow,2,new QTableWidgetItem("Item3"));

Related

Is it possible to add values to table widget programmatically in Google Appmakers

I am able to show the data source data in google app maker table widget, but I don not know how to show static values (array of object) to table widgets programmatically.
I am new to app maker,I don not know is it possible to add values to table widget programmatically or not.
If any one knows please help me...
Thank you in advance :)
To call your own create children method you would want to set up a panel similar to the picture provided and set a datasource for that panel for which items you want to appear in that panel.
Then go to events for that panel and in the onDataLoad event enter the following code as it pertains to your own data. In my example I used a datasource called employees, and I created a label with the employee name for each item in the datasource.
widget.datasource.items.forEach(function(item) {
var node = document.createElement('div');
node.className = 'app-Label';
node.style.margin = '8px';
node.textContent = item.EmployeeName;
widget.getElement().appendChild(node);
});
That is about the simplest example I have. In the case of a table you would want to create your own base container, a header, and then a container that holds all your table rows. Then in your table body you would set up a similar script that attaches row panels and in the row panels you would create your labels. This would also be possible to declare your own array of objects and loop over each object this way.

reload table in WatchKit

We have a hierarchical watch app.
The root controller is a table of menu items. That list of items is controlled by a server. The data is retrieved and stored in core data. The menu is populated the first time going into the app.
But I want this table to stay current. My thought was to add code to willActivate to check if there was changes, and reload the table. In my reload logic I call the same function I called the first time, which sets the menuTable.setNumberOfRows and creates each row. Looking at what I'm putting in the logs, it is going through this logic with a different count of rows and new labels. But the app on the watch shows the table with the old data.
How can I get this table to reload with the new information?
I've had this problem too and as rmp says, it still seems to be a bug in watchOS 1.0.1. The problem appears when you try to reload your tableView after run willActivate() and nothing will happen.
In my case, I reload the tableView after receive a reply from a delegate and then I reload all the content just if it's necessary. To achieve this, I remove all rows from a NSIndexSet and load again.
if isNecessary {
self.table.removeRowsAtIndexes(NSIndexSet(indexesInRange: NSMakeRange(0, maxItems)))
isNecessary = false
}
I've tried a lot of tricks but none has worked for me:
Force to reload rows by table.setNumberOfRows(0, withRowType: "data")
Setting parameters to empty text before assign new values
One thing you could do is to hide tableView before removing rows, and avoid the remove animation.
It is a bug in WatchKit. Seems like Apple doesn't handle the repetitive interface object correctly.
The general principle here is: Only insert or remove necessary rows after a table is created. Do not reload the whole table like what we usually do in iOS. It just doesn't work (or trigger the bug).
So specifically, you have to:
Do this in willActivated method. This is correct.
If this is the first load, before the table is even created, do what you are now doing – load all table rows.
For all following times, don't reload the table, fetch the new data and check the desired number of rows.
Compare with the current number of rows in the table, insert to or remove from the bottom of the existing table.
Now simply re-assign the new data to all existing rows. Again, do not reload.
It should work if you follow the above steps.
I have found what works best given the current state of watchkit is to remove all rows then re-populate the table.
Try something like this:
- (void)loadTableData{
//clear the table
[mainTableView setNumberOfRows:0 withRowType:#"myTableRow"];
//set the row count again
[mainTableView setNumberOfRows:numberOfRows withRowType:#"myTableRow"];
//populate table
}
Its pretty simple. As Apple hasn't provided any method to reload the data.
You can still achieve that by simply populating the rows for the tableview.
Below is the sample code:
for index in 0..<tracksTableView.numberOfRows {
if let controller = tracksTableView.rowController(at: index) as? EditPlaylistRowController {
controller.playingTrackId = self.playingTrackID
controller.sharedTrack = trackItems[index]
}
}
Use it whenever you want to refresh the data.
NOTE:
You can still make some conditional statements inside the row controller class.
Happy to help :)

QT: QTableView read cell content

I'm writing a small program with QT creator (QT 5.2.1) under Windows 7 (32 bit) and I'm having problems reading the informations stored in a TableView. My application has 3 elements, a TableView to store text data, a TextBrowser to show info and a buttom.
I modified the TableView properties: when the user selects with the mouse a cell, the full row is selected and multiple row selection is not allowed.
The user select a row and when the buttom is pressed, I would like to read the content of a specific TableView cell and show it in a TextBrowser. In particular, I would like to know the row index of the selected row and read the content of the cell with that row index and a specific column index (example 2).
The pseudo-code is this:
void my_program::on_pushButton_clicked()
{
ui->textBrowser->append("button pressed");
QItemSelectionModel *select = ui->tableView->selectionModel();
int index_row = select->selectedRows();
int index_column = 2;
char cell_data[30] = ??[index_row][index_column]
ui->textBrowser->append(cell_data);
}
The main problem is that select->selectedRows() returns a QModelIndex that is a collection of indexs and I do not know how to convert it to int (since multiple selection are not allowed, it should have only one element).
I would be glad if someone can suggest me a way to proceed.
Thanks
Francesco
edit:
Hi Bogdan, thanks a lot!! I succeed to read the cell content by using
ui->textBrowser->append(ui->tableView->model()->data(ui->tableView->model()->index(2,5)).toString());
this give me the content of the cell in position 2,5.
not sure if this is the best way or not but it works !!.
Can you be a bit more precise about how to iterate the QModeIndexList ? thanks :)
selectedRows() returns QModelIndexList, thus you need to iterate over it and call QModelIndex::data() to get stored data.

Create Row dynamically in QListWidget

Hi,
I want to create a custome widget. Please help by giving some idea.
My intention is to make a Listwidget which shows some Information and when clicking particulat row it have to show the details coresponding to that row by creating a new area(Row or text edit) immediately below the selected row and pull the other rows below the selected row to down after the created new area.
In attachment when I click row Nancy (consider it as row) her details is coming below the selected row. Please help on this
What you want to use is the QTreeView. It is capable of showing rows with text and icons.
You need to define the QStandardItemModel for the table, this will give it column definition.
Below are three relevant functions from a class I wrote for a similar app. Note that I did not need to use any images in my rows so you'll have to figure that part out yourself.
def drawPeopleListBox(self):
header = ["Picture","First Name","Last Name","Title","Address", "City", "Region"]
self.model = QStandardItemModel(0, len(header), self)
for i in range(len(header):
self.model.setHeaderData(i, Qt.Horizontal, self.selectionDict[i+1].upper())
self.itemList = QTreeView()
self.itemList.setRootIsDecorated(False)
self.itemList.setAlternatingRowColors(True)
self.itemList.setSortingEnabled(False)
self.itemList.setModel(self.model)
self.itemList.NoEditTriggers=True
#self.itemList.clicked[QModelIndex].connect(self.onRowClick)
self.itemList.clicked.connect(self.onRowClick)
self.itemList.setCursor(Qt.PointingHandCursor)
self.itemList.setColumnWidth(0,70)
self.itemList.setColumnWidth(1,140)
self.itemList.setColumnWidth(2,70)
self.itemList.setColumnWidth(3,180)
self.itemList.setColumnWidth(4,100)
self.itemList.setColumnWidth(5,100)
self.itemList.setColumnWidth(6,100)
self.populateList(self.userDataList)
def populateList(self, userDataList):
row=[]
for user in userDataList:
for attrib in user:
row.append(QStandardItem(attrib))
for item in row:
item.setEditable(False)
self.model.appendRow(row)
def onRowClick(self, index):
print index.row()
'''
Here you need to resize the clicked row height. Also resise the image.
Or insert another row that matches your design requirement.
self.model.insertRow(rowNumber, listOfQStandardItems)
'''
self.repaint()
You may want to try using a custom delegate to view the rows. I believe the delegates know if they are selected. Using this, you would draw the unselected rows normally, and draw more information for the selected rows. The problem with this is that I don't know if you can resize the widget on selection. If not, the QTreeView solution should still work.

ExtJs / Sencha : how to highlight a grid row after insert?

I want to create such a grid:
http://www.sencha.com/deploy/dev/examples/grid/edit-grid.html
Actually I already did, but I want to highlight the last inserted row of my grid (in extjs this is the function highlight(), which does a yellowfade on the element).
I didn't actually succeed in doing this... my problem is that I can't get the row I just inserted, and thus obviously I can't highlight it.
Any clues?
Thanks in advance
You only need to do this (here for row number one):
var row = grid.getView().getRow(0);
Ext.get(row).highlight();
It's that easy.
The code has
store.insert(0, p);
So don't you just highlight row zero immediately after that statement?
yes sorry for answering too late,
use Ext.grid.RowSelectionModel and than use selectLastRow function u can easily will be able to point it out :)
Ext.data.store has a add listener which is passed an index at which records were inserted
add : ( Store this, Ext.data.Record[] records, Number index )
This would go inside of your add/create button event. The code this.getSelectionModel().select(0); will highlight the first row since we inserted into position 0, we are selecting position 0. This code works with ExtJS 4.2.0.
var rec = Ext.create('App.model.GridModel', {
id: '123',
name: 'ABC'
});
this.store.insert(0, rec);
this.getSelectionModel().select(0);

Resources