Hello I'm using a QTableWidget and essentially have code such that the first column will allow the table to grow/shrink dynamically. The last populated row's first cell will have a Plus button to add a new row that can be edited and the Minus button will remove its corresponding button's row. For example, If I click the Plus button 5 times, it will be at the 6th row and the previous 5 rows will all have a Minus buttons to remove their row. If the cell's row has a Minus button on it, the row is implied editable, otherwise not. However, I allow the table to have a default amount of rows, such that it can never visually shrink less than 5, for example, even if it's technically "empty". To edit the row, you need to hit the Plus button which will check if we need to insert a new row, for example if we're at 5 rows and the base row size is 5, then we need a new row and to populate each column's index of the new row.
The main reason for this table is to read and write the table's information to a QSettings and be able to import the past exported information to the same cells and essentially restore the table's state as it appeared last. However, when reading from the QSettings, the table will crash after all cells are populated correctly with the information and I can tell they are correctly populated because the debugger will freeze the GUI and I can visually see and qDebug() how far into the loop of the QSettings array I got to before seg-faulting.
Now when I do everything manually, such as clicking the Plus button, everything is fine and the program performs as expected.
//newRowCount is assumed to be the amount of rows with Minus Buttons currently, only creates a new row if the table needs to expand
void TableTest::enableNewRow(){
newRowCount++;
if(newRowCount > table->rowCount()){
table->insertRow(table->rowCount());
for(int i = 0; i < table->columnSize(); ++i){
QTableWidgetItem* item = new QTableWidgetItem;
item->setFlags(item->flags() ^ Qt::ItemisEditable);
table->setItem(newRowCount, i, item);
//Code to set the previous column's item (newRowCount--) to have an Qt::ItemisEditable flag set to true
...
}
}
//Some button setup to make Plus button and create a new Minus button connect and do their jobs and move the Plus button down into the newly inserted row
...
}
void MainWindow::importFile(){
int settingRows = settings->beginReadArray("Groups");
for(int i = 0; i < settingRows; ++i){
settings->setArrayIndex(i);
//Will only add a new row if needed, check inside above function and all row items will be allocated if made
table->enableRow();
for(int j = 1; j < table->columnCount(); ++j){
table->item(i, j)->setText("testing");
}
}
}
And the program crashes with the following stack trace:
1 QWidget::show()
2 QAbstractItemView::updateEditorGeometries()
3 QAbstractItemView::updateGeometries()
4 QTableView::updateGeometries()
5 QTableView::timerEvent(QTimerEvent *)
6 QObject::event(QEvent *)
7 QWidget::event(QEvent *)
8 QFrame::event(QEvent* )
9 QAbstractScrollArea::event(QEvent* )
10 QAbstractItemView::event(QEvent* )
11-21 ... Not useful information about any of the code
22 QCoreApplication::exec()
23 main
However, it's all byte code and I can't actually inspect which line is crashing the code in any of the traces using the debugger. What I don't understand is that the same process for manually clicking the Plus button is being completed (I connect the click event of the Plus button to the enableRow() function) and when I programmatically call enableRow() from the QSettings loop in importFile() it will crash after it loops through all of the items.
Things to note: setting the base size of the table to the size of the QSettings array I'm reading from (i.e. if I want 10 rows, just setting 10 rows to begin with before setting the item texts will work correctly), however, I want the base size of the table upon creation to be something like 5. It seems once the loop goes beyond the row count I specified from the constructor, then the program will crash, but not at say, index 6, but only after it has completely looped through the table. It does not crash on any line such as QTableWidgetItem::setText or QTableWidget::setCellWidget(). I'm kind of confused and wondering if the table is populating too quickly for the QTableWidget::timerEvent on stack trace line 5. Nothing I do in the loop is any different than what I do when manipulating the table normally.
It turns out that the error was that I was setting the QTableWIdgetItem one row earlier than I should have been. I'm not sure why the table would be able to iterate up until the end of the QSettings loop before crashing, but that was essentially the error. I'm not quite sure why the stack trace was so cryptic.
in my enableRow() function, I actually posted above from memory the correct logic, which differed in my code's logic by 1 index previously for the row value. Why the code did not crash when using it normally with the buttons whose slots called the same function as the import() seems to baffle me, perhaps a case of undefined behavior.
Related
I have a javafx application with a TableView. Only the first column (dataTypeColumn) is editable and it contains a ComboBox for editing. Now the TableView does what it is supposed to do, however there is a combination of weird bugs when i start editing a cell and then scroll it out of view without commiting the edit. I am pretty sure it is because of the way TableView reuses the cells. However I haven't found any way to intervene with it, for example to forbid reuse of cells that are currently editing. Can you help me with that.
Even though I am pretty sure it is because of cell reuse, I will write the whole problem below, in case the source of the problem is a different.
The column in question contains Values of the enum DataType
I have a cell Factory for that column looking like this:
dataTypeColumn.setCellFactory(param -> new ComboBoxTableCell<>(DataType.values()));
The values for the column get read like this:
dataTypeColumn.setCellValueFactory(param -> Bindings.valueAt(configuration, param.getValue()));
In case this is confusing, the items that I give my TableView are Integers (from 0 to n-1), and in the different column CellValueFactories the actual values will be loaded depending on the Integer assoziated with the current column.
So when editing it, it shows a ComboBox with all the Values that DataType can have and let's the user select one. I have a callback on the column that reacts to the Edit commited event and looks like this:
public void editCommited(TableColumn.CellEditEvent<Integer, DataType> event) {
//configuration is an ObservableList containing DataType elements
configuration.set(event.getRowValue(), event.getNewValue());
}
So about the problems that occur. At the beginning all cells in that column have the same value: "nothing selected". It is a special value of the enum reserved for this case, as I didn't find a setPlaceholder function on the ComboBoxTableCell class. When I know start editing one of the cells and then scroll it out of view, one of the next rows will suddendly be in the editing state as well, even though that row has not been touched before. If I scroll further an cell in the editing state will appear every time the previous one scrolls out of view. I can also go back and will find the same cells in the editing state. If I edit the cell that should not be in the editing state, it will not change, but instead the cell that I originally tried to edit will change it's value. This might be due to the underlying ObservableList, that automatically updates the value in the column.
If I start editing a cell that has a different value (from a previous edit), something even more weird happens. When scrolling it out of view again a new row will have a cell in the editing page, however with the default value "nothing selected". If I scroll backwards the cell that i tried to edit originally will no longer be in the editing state however it's value has changed to "nothing selected". As if the new cell that got the editing state somehow commited it's own value.
Please some help with this :)
This indeed seems to be a bug. Rather surprising, if you move the scroll bar with the mouse, the edit is properly canceled, but not, if you use the mouse wheel to scroll.
You can easily create a workaround for this buf however, since you simply have to cancel the edit, when the item is replaced. You could e.g. use this method to create the cellFactory on your own:
public static <S, T> Callback<TableColumn<S, T>, TableCell<S, T>> comboBoxCellFactory(ObservableList<T> items) {
if (items == null) {
throw new IllegalArgumentException();
}
return column -> new ComboBoxTableCell<S, T>(items) {
#Override
public void updateIndex(int i) {
cancelEdit();
super.updateIndex(i);
}
};
}
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.
Hello guys i new to QT and i am doing Qtableview to add information in 3 columns to infinite row like
|--1--|--2--|--3--|
|--1--|--2--|--3--|
|--1--|--2--|--3--|
this is how i want to insert/append row but this is how i am getting after insert/append row functions.
|--1--|--2--|--3--|
|-----|-----|-----||--1--|--2--|--3--|
|-----|-----|-----||-----|-----|-----||--1--|--2--|--3--|
i am getting empty gaps and increased column count
i am using QStandardItemmodel for model this is code that creates the model item
void tableview::add_tableview() //this is used to add data to tableview
{
//to get data from line edit in add window
QStandardItem *item_1 = new QStandardItem(QString(enter1_edit->text()));
QStandardItem *item_2 = new QStandardItem(QString(enter2_edit->text()));
QStandardItem *item_3 = new QStandardItem(QString(enter3_edit->text()));
list << item_1 << item_2 << item_3;
model->appendRow(list);
//to set the model
main_tableview->setModel(model);
}
The values in the rows are added using qlineedit enter1_edit,enter2_edit & enter3_edit(its in other function/method)
main_tableview is the object of qtableview
Thanks for helping me!
Ding Ding Ding, I have it.
I tried to reproduce your problem without success unil I tried something...suboptimal :).
Is your list by any chance a global entity? If yes (and I assume it is) you keep adding items to it.
First time you call add_tableview() all is good, list is empty, gets three QStandardItem pointers added and is used to append a row with those three items. So far so god.
You call add_tableview() again, now you create another three QStandardItems and APPEND them to the list (which still contains the three from the last call). Invoking appendRow() actually tries to insert six items, from which the first three allready exist in the model. And as you know you mustn't add the same item pointer to a model twice. Thankfully Qt doesn't crash but inserts three empty columns for the item pointers allready in it.
Solution: At the end of your add_tableview() method call list.clear() or use a local variable for the list. The overhead should be minimal.
Best regards
D
I'm using PyQt to create a GUI application. In a view inherited from QTableView, need to detect the row the user has selected when they double click a row. The table has sorting, but no editing.
How do I do it?
Note - tried the doubleClicked(int) signal. It is emitted by mouse buttons, not by data cells, so it was never fired. :(
Ian
I dont understand.
The doubleClicked signal of the QTableView has the signature
void doubleClicked ( const QModelIndex & index )
If you connect that signal you should obtain the correct QModelIndex.
No need to use SIGNALs anymore:
self.your_table.doubleClicked.connect(your_function)
"doubleClicked" being inherited from QAbstractItemView.
Once you have the modelIndex, (from Frank's comment above) you can use it to find which cell was double clicked.
def slotDoubleClicked(self, mi):
row = mi.row()
column = mi.column()
You then can use these row and col values to access the table with table.setItem(row, column, newdata) or other table method
Like #regomodo said, you can simply connect your function to the double click via:
self.your_table.doubleClicked.connect(your_function)
Then, if you want to know on which row the user double clicked, you can use the following code:
for idx in self.your_table.selectionModel().selectedIndexes():
row_number = idx.row()
column_number = idx.column()
It will return an integer corresponding to the row or the column number.
There will always only be a single value as the double click remove the previous selection.
If you link your function to a push button or another signal, you can receive a list containing multiple elements selected by the user.
For example, you can easily retrieve a list of all selected rows using this code:
rows = []
for idx in self.your_table.selectionModel().selectedIndexes():
rows.append(idx.row())
rows = list(set(rows))
This will return a list of all selected rows (The set function will also remove any duplicates).
Cheers!
I've got a gridview with a list of categories. In the database (MSSQL2008), the category table has a SortOrder(INT) NULL field.
Currently categories are retrieved using:
Galleries.DataSource = From G In DB.GalleryCategories Order By G.SortOrder, G.Name
Now, what I need to be able to do is add "Move Up" and "Move Down" buttons to each row to allow the user to sort the items in an arbitrary way.
My initial thoughts are along the lines of:
Identify ID of selected item.
Identify ID of item before/after selected item.
Swap of identified items in the DB SortOrders.
I would then have make the sortorder NOT NULL and make sure it's initialised to a unique number
I'd appreciate any alternative suggestions / comments on this approach
Many thanks
I have generally seen it done this way, and have done it myself
SortOrder is an int
Each item increases by 10 (so, 10,20,30,40) or suitable increment
To move an item up, subtract 15
To move an item down, add 15
To insert an item, take the target and add/subtract 1
Apply a NormalizeSort() routine which resets the values to even intervals
10,20,25,30,40 => 10,20,30,40,50
That makes it all pretty simple, since inserting something above something else is just:
list.Add( New Item(..., target.SortOrder +1) )
list.NormalizeSort()
// or
item.SortOrder += 11, etc
If you want to make it a decimal, then you can just make it all sequential and just add .1, etc to the sort order and re-normalize again.
// Andrew
I believe
Galleries.AllowSorting = true;
would be far enough ;)