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");
Related
I have a problem where i did not find a solution so far, actually the question is when and for what QModelIndex we have to call beginInsertColumns and similar functions.
My example is the standard Qt example from here:
http://doc.qt.io/qt-5/qtwidgets-itemviews-editabletreemodel-example.html
It is also delivered with every qt installation, e.g. C:\Qt\Qt5.12.0\Examples\Qt-5.12.0\widgets\itemviews\editabletreemodel
When just executed, it works fine, but now lets insert a QSortFilterProxyModel, just to make it sortable. We change the MainWindow c'tor to
...
QFile file(":/default.txt");
file.open(QIODevice::ReadOnly);
TreeModel *model = new TreeModel(headers, file.readAll());
file.close();
QSortFilterProxyModel* sort = new QSortFilterProxyModel( this );
sort->setSourceModel( model );
view->setModel( sort );
...
Now execute the example, expand one or two of the root nodes and select the menu entry "insert column" you will see, that only the root nodes have a new column, all their children do not have the new column. It works without the proxy. Now my question, do i have to emit beginInsertColumns for every QModelIndex that has children?
How can i fix this?
Note, this is not only a problem with qt 5.12, but also with many earlier releases.
I've always had problems using QSortFilterProxyModel, and as far as I understood they are often related to the index mapping between with the source model.
I managed to solve it using the QSortFilterProxyModel::invalidate() method that will reset the mapping, without spoiling the current state of the view.
Modify the MainWindow::insertColumn as follows:
bool MainWindow::insertColumn()
{
QAbstractItemModel *model = view->model();
int column = view->selectionModel()->currentIndex().column();
bool changed = false;
// Insert a column in the parent item.
QSortFilterProxyModel* sfpm = qobject_cast<QSortFilterProxyModel*>(model);
if (sfpm)
{
changed = sfpm->insertColumn(column + 1);
if (changed)
sfpm->setHeaderData(column + 1, Qt::Horizontal, QVariant("[No header]"), Qt::EditRole);
sfpm->invalidate();
}
updateActions();
return changed;
}
It is not an optimal solution, and I don't know if there is a better way, but works for me.
As you may know, you can set a widget for a cell in a QTableWidget using setCellWidget:
table->setCellWidget(0, 0, new QProgressBar);
In some circumstances, I just want to get rid of this widget and everything becomes like when there was no cell widget. I tested setting widget to nullptr but It wasn't helpful.
In other words, What is the default widget of a cell in QTableWidget?
You have to use void QTableWidget::removeCellWidget(int row, int column)
In your case:
table->removeCellWidget(0, 0);
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 help with customizing a QTableView, I have defined a QTableView as this example show, which I found on the internet:
model = new QStandardItemModel(2,3,this); //2 Rows and 3 Columns
model->setHorizontalHeaderItem(0, new QStandardItem(QString("ID")));
model->setHorizontalHeaderItem(1, new QStandardItem(QString("Name")));
model->setHorizontalHeaderItem(2, new QStandardItem(QString("Description")));
ui->tableView->horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch);
ui->tableView->setModel(model);
How can I define a size for each column separately i.e using percentages:
I would get first column 10% of the width second 50%, third 40%.
When I run the program and double click on a row in the QTableView, I can change the value of the cell clicked on , although I have defined a QTableView onDoubleclick method , I mean its like when you click on rename a file it highlights the text so you can modify, how can I disable that?
How to make the columns resizable, meaning can be resized by dragging their columns edge.
First: Use setColumnWidth() method after setModel(). For example:
//...
ui->tableView->setModel(model);
double ii = ui->tableView->columnWidth(0);
ui->tableView->setColumnWidth(1,0.4*ii);
ui->tableView->setColumnWidth(2,0.5*ii);
Third: To do this remove
ui->tableView->horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch);
from your code.