I'm showing multiple images in a QTableView. Now I want to trace the image on which the user is pressing the left mouse button once, I want to trace it because I will show that exactly that image in a bigger window. How can I do that? I mean how I can get the row and column index of the image on which the user pressed the left mouse button once?
I cannot see any direct clicked() SIGNAL in QTableView, so what is the tool that gives me the row or column of the QTableView?
EDIT:
I thought I should also mention that I used QStandardItemModel to set the model in the QTableView. First I set the rows and colums of the model and then input each QImage type item(I convert the QImage to QIcon) in a QStandardItem and then put that QStandardItem in the QStandardItemModel, once the QStandardItemModel is setup or filled up I put in the QTableView.
Thanks.
In order to obtain the pressed item's row and column values you need to connect your QTableView's pressed() signal to a slot. Something like this:
connect(tableView, SIGNAL(pressed(const QModelIndex &)), this, SLOT(onItemPressed(const QModelIndex &));
Here is the slot that will handle the mouse action:
void MyClass::onItemPressed(const QModelIndex &index)
{
int row = index.row();
int column = index.column();
[..]
}
Related
Initially, I used qsqlrelationtablemodel in my database project. And the values were entered into the database using the combobox inside the tableView. And it looked something like this:
enter image description here
And the code looked very simple:
model->setTable(sql_query);
model->setRelation(1, QSqlRelation("my_schema.aircrafts","id_aircraft","registration_number"));
model->setRelation(3, QSqlRelation("my_schema.airports","id_airport","name_airport"));
model->setRelation(4, QSqlRelation("my_schema.airports","id_airport","name_airport"));
ui->tableView->setModel(model);
ui->tableView->setItemDelegateForColumn(1, new QSqlRelationalDelegate(this));
ui->tableView->setItemDelegateForColumn(3, new QSqlRelationalDelegate(this));
ui->tableView->setItemDelegateForColumn(4, new QSqlRelationalDelegate(this));
But now I have redone the data entry on the forms and there are no problems with transferring information to qlineedit. There are difficulties with transferring data from foreign key's to an external combobox.
Now it looks like this (the value of the combobox does not change when you click on another row of the tableView):
enter image description here
Now I'm using this code, but I don't understand how to pass the value of the selected index in the tableView to the combobox.
QString query = QString("SELECT * FROM my_schema.route WHERE id_route = '%1'").arg(id);
QSqlQuery qw;
qw.exec(query);
while(qw.next())
{
ui->lineEdit->setText(qw.value(1).toString());
ui->lineEdit_2->setText(qw.value(2).toString());
ui->lineEdit_3->setText(qw.value(3).toString());
ui->comboBox_2->setModel(model);
ui->comboBox_2->setModelColumn(4);
}
I would like to see something like this result:
enter image description here
Please tell me in the most accessible form how to achieve this
You have model and tableview for fetching all columns for clicked row by using QAbstractItemView::clicked(const QModelIndex &index) signal for receive event. And implement the slot that loops all columns in selected row like this example snippet:
// create query with join for combobox model only containing desired values not foreign keys
// after that create QStringList and fill it with query result
// set stringlistmodel to combobox
ui->comboBox_2->setModel(stringlistmodel); // this is different from tableview's model
connect(ui->tableView, &QTableView::clicked, [this](const QModelIndex &index)
{
auto row{index.row()};
ui->lineEdit->setText(model.data(model.index(row, 0).toString());
//...
ui->comboBox_2->setCurrentIndex(model.data(model.index(row, 4).toInt());
//...
});
I have already coded this for QTableWidget:
void ReadOnlyWindow::addReportIconToRow(const int rowIndex){
QIcon icon;
QSize sz(16, 16);
icon.addPixmap(style()->standardIcon(QStyle::SP_FileDialogEnd).pixmap(sz), QIcon::Normal);
QTableWidgetItem *iconItem = new QTableWidgetItem();
iconItem->setText("report");
iconItem->setIcon(icon);
iconItem->setFlags(iconItem->flags() & (~Qt::ItemIsEditable));
ui->homeWorksTable->setItem(rowIndex, REPORT_COLUMN_INDEX, icon);
}
REPORT_COLUMN_INDEX is const int from class and it has value 4.
I am trying to found out how to rewrite code if table is `` QTableView`.
I was trying to use setItemData() and setData() but I think I used it in bad way because it didn't work.
P.S.: Now I want to do it for QTableView because its easy to load SQLite table there. This part works. I also added one more column. Now I need add to all rows of this column icon with text (how is in my code for QTableWidget). Function up there should be for one cell and will be implemented in loop.
to add an icon to a tableWidget you set the item like specified here :
and when defining the QTableWidgetItem, use the constructor taking an Icon to be displayed.
here is a short example:
this->ui->myTable->setItem(row, col, new QTableWidgetItem(QIcon(":/resources_to_icon_.png"),"SomeText"));
in your code:
ui->homeWorksTable->setItem(rowIndex, REPORT_COLUMN_INDEX, QTableWidgetItem(icon,"some text");
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
I have QTableWidget with specific row and column. When I click on empty cell a text opens and let user to write on cell. Is there any way to avoid this situation, I mean, to avoid adding text on empty cell?
UPDATE:
I added this part to slot of cellclick(), but empty cells are yet editable,
QTableWidgetItem *item = filesTable->item(row, column);
item->setFlags(item->flags() & ~Qt::ItemIsEditable);
Is there any way to force empty cells to be read-only?
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.
}