QTableWidget, centering cellWidgets - c++

Is there a way to place QCheckBox as a cell widget of QTableWidget in the center of a cell, not at the left side, without additional QWidget and adding the checkbox to it's layout?

Use setCellWidget to add QCheckBox to table:
QWidget *checkBoxWidget = new QWidget(); //create QWidget
QCheckBox *checkBox = new QCheckBox(); //create QCheckBox
QHBoxLayout *layoutCheckBox = new QHBoxLayout(checkBoxWidget); //create QHBoxLayout
layoutCheckBox->addWidget(checkBox); //add QCheckBox to layout
layoutCheckBox->setAlignment(Qt::AlignCenter); //set Alignment layout
layoutCheckBox->setContentsMargins(0,0,0,0);
ui->tableWidget->setCellWidget(0,0, checkBoxWidget);
Also use these line to resize to contents:
ui->tableWidget->resizeRowsToContents();
ui->tableWidget->resizeColumnsToContents();
setCellWidget: Sets the given widget to be displayed in the cell
in the given row and column, passing the ownership of the widget to
the table.
Reference: https://evileg.com/en/post/79/

I have a simple solution without any additional layout if you want to use ui->tableWidget->resizeColumnsToContents():
Use checkBox->setStyleSheet( "text-align: center; margin-left:50%; margin-right:50%;" );
AFTER this, call ui->tableWidget->resizeColumnsToContents().
If the column is resized after this (eg. the table gets resized by the layout), you may have to call ui->tableWidget->resizeColumnsToContents() again.
It the widget is placed before the resulting column width is known, it retains its (wrong) position. Using resizeColumnsToContents() repositions it.

Related

Add QIcon to QTableView cell

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");

Get rid of cell widget in QTableWidgetItem

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);

Qt: avoid add text to empty cell in QTableWidget

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?

Gettting row and colums from one left mouse click on QTableView?

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();
[..]
}

QT QTableWidget not appearing in QTabWidget

I have an application which uses multiple tabs. I used QTabWidget. On some tabs I needed to show tables, so I used QTableWidget.
The code snippet is:
QWidget *qwgt = qPreviewTabs->widget(Index);
QTableWidget *qDrvTab = new QTableWidget();
....
....
....
QVBoxLayout *vbLyt = new QVBoxLayout();
vbLyt->addWidget(qDrvTab);
qwgt->setLayout(vbLyt);
When I add push buttons and tree widgets they all appear on the specified tab without any problem. Only the QTableWidget refuses to show.
A table with no rows and columns is a void.
So do
qDrvTab->setRowCount(no_of_rows);
qDrvTab->setColumnCount(no_of_cols);
before adding it to layout.
Now you can see your Tablewidget in layout.