The first row is selected by default, when I shift-select the last row I expect that all rows will be selected but no, it only selects the visible rows of the QTableView.
I have a QTableView displaying data from a database-model. It is configured to allow extended selection (ctrl, shift etc) and to select rows only (not cells). The content is changing based on other parameters, and whenever I update the model I select by default the first row of my QTableView.
ui->tableView->setModel(model);
ui->tableView->setColumnHidden(UidColumn, true);
ui->tableView->setSelectionMode(QAbstractItemView::ExtendedSelection);
ui->tableView->setSelectionBehavior(QAbstractItemView::SelectRows);
ui->tableView->setEditTriggers(QAbstractItemView::NoEditTriggers);
ui->tableView->verticalHeader()->hide();
ui->tableView->setAlternatingRowColors(true);
// This is done whenever the content changes
QModelIndex index = model->index(0,0);
ui->tableView->setCurrentIndex(index);
I hide the vertical headers because nothing to display there, and I hide the first column because it is not relevant for the user.
The first row is displayed as selected, I even logged the changes of focus and the tableView->selectionModel()->selectedRows() is always good (i.e. returning QModelIndex(0,0)). But when I do the shift-click on the last row, it is like the first row was never selected at all.
If I manually select the first row (or any row), the next shift-click will work perfectly. If I do a ctrl-click, the multiselection works perfectly. It is like my default selection (done by the code) is ignored with shift-click.
The default QModelIndex(0,0) selects a cell that is part of the hidden column. Even if displayed as a selected row, apparently it messes up the shift-selection.
If I do not hide the first column, it works fine.
If I use QModelIndex index = model->index(0,1); it works fine.
A simpler solution is to do ui->tableView->selectRow(0);
Related
I'm using a QtableView to display and edit data from a QsqlTableModel.
Everything's fine : data from a postgreSQL table is displayed, and user can edit it and save modifications.
I want to add a row when uses click on a button. I use the insertRecord method from my QslTableModel.
The row is correctly added in the QTableView.
My problem is :
I want to insert a value from a query in the first cell of this new row. (to automatically populate an unique identifier).
This is my code :
def ajoutlgn(self):
query_idNewLine = QtSql.QSqlQuery(self.db)
if query_idNewLine.exec_('SELECT sp_idsuivi+1 FROM bdsuivis.t_suivprev_tablo ORDER BY sp_idsuivi DESC LIMIT 1'):
while query_idNewLine.next():
identifiant = query_idNewLine.value(0)
#print identifiant
record = QtSql.QSqlRecord()
record.setValue(1,identifiant)
self.model.insertRecord(self.model.rowCount(), record)
The value is not inserted in the new row (but if I enter a value by hand, it works perfectly).
Nevertheless, the query is OK (as I can see with the « print identifiant » line, which returns the expected integer).
Do you see what I'm doing wrong ?
Are there other methods to insert programmatically a value in a QTableView cell?
Or do I have to use a QitemDelegate ?
Thanks for advance.
Finally I found the solution :
This line creates a record, but not a record for my QsqlTableModel
record = QtSql.QSqlRecord()
I replaced it with this one, and it works perfectly :
record = self.model.record()
I'm retrieving a set of results from the database and I want to populate the QComboBox with the resulting columns from the database (each row of the QComboBox should have the same columns as database result) and after that I would like to be able to retrieve from one row of the QComboBox a specific column and use it further in the app. I'm thinking if it would be possible to add the QTableView to QComboBox. I'm want to do this because I want to add more meaning to the results in a way that some result columns are just plain numbers and other are the description information.
I found out that it would be possible to concatenate the result and populate the QComboBox but this will leave me with only one value for each row to work with and I have to explode the string to obtain the exact part that it is needed to work with.
The popup that comes by default is a QListView, this can be changed with any object that inherits from QAbstractItemView, and in this case a QTableView will be used for it to use the setView() method, the result when clicking only should return a item of the selected row, then to set the column to be displayed after being selected will use the method setModelColumn() indicating the position of the column, but before that the model is set to the QComboBox using the method setModel().
# my model
model = new QSqlTableModel;
model->setTable("person");
model->select();
# setModel
comboBox->setModel(model);
# select column
comboBox->setModelColumn(1);
QTableView *view = new QTableView(this);
comboBox->setView(view);
Note: The model is set to QComboBox, not to QTableView. Also you could have problems with the width of QTableView, so we must resize, in my case use the following:
view->horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch);
view->setMinimumWidth(500);
The complete example can be found in the following link
How to show Qtreewidget item as in editable mode? means when a data of that particular column will show it should be look like it can be edit.
Means it should be taken data as input.
QTreeWidgetItem *item=NULL;
item->setBackgroundColor(0,color);
item->setIcon(0,coloricon);
item->setText(0,QString(sample->sampleName.c_str()));
item->setData(0,Qt::UserRole,QVariant::fromValue(samples[i]));
**//I want to show this particular column as in editable mode**
item->setText(1,QString(sample->getSetName().c_str()));
item->setText(2,QString::number(sample->getNormalizationConstant(),'f',2));
I need a piece of code to check that the user has selected at least one row in a QTableWidget
The QTableWidget can be referenced using ui->tableWidget.
I'm trying to check there are selecting, if not, display a message box, if so, moving on to code I have written.
Thanks.
You can obtain the selected rows from the selection model like:
QItemSelectionModel *selectionModel = ui->tableWidget->selectionModel();
QModelIndexList *selectedRows = selectionModel->selectedRows();
if (selectedRows.size() > 0) {
// There is at lease one selected row.
}
I have a dialog list which use Use View dialog for Choices.
I have a row in a table which its appearing depends on the value selected from that dialog list. ( I am using Hide when formula... ) The form has Automatically refresh fields checked.
The problem is that after I select a certain value from the dialog list, and I even had selected Refresh fields on keyword change property, I MUST hit F5 or just TAB ( to go to a next field ) in order to make that row table to appear. I also tried to add uidoc.Refresh at the Exiting/OnChange event(s) of the dialog list.
I have noticed the following:
the refresh works fine for combo box, list box, radio button or check box
the refresh works fine for dialog list where you are using a list of choices / formula.
the refresh doesn't work for dialog list where you are using view dialog for choices ( my case ).
Is there any solution for this issue? I want when I selected a value from the dialog list, immediately the row / line should appear/disappear.
Thank for your time!