How to set row height of QTableView? - c++

I have QTableView and QAbstractTableModel. I require rows to have height equal to 24. I know the only way to do this is by calling QTableView::setRowHeight. Since the model is dynamic it may be added new rows, but I don't want to call setRowHeight each time new row is added.
How can I configure QTableView such that it uses the same height for new added rows or can a model be sent the height of rows?

For Qt versions < 5
QHeaderView *verticalHeader = myTableView->verticalHeader();
verticalHeader->setResizeMode(QHeaderView::Fixed);
verticalHeader->setDefaultSectionSize(24);
For Qt versions >= 5 use
QHeaderView *verticalHeader = myTableView->verticalHeader();
verticalHeader->setSectionResizeMode(QHeaderView::Fixed);
verticalHeader->setDefaultSectionSize(24);
If that function doesn't apply to vertical headers, you likely will have to call setRowHeight() every time you add a new row.

Related

QTreeView/QHeaderView resize only first column to stretch and interactive both

I have a QTreeView which has 5 columns. The requirement is to stretch only the first column but all the 5 columns should be resizable by user(i.e. they can be interactive as well) also. So I wrote the following code:
int numCols = myModel->columnCount();
for(int i=0;i<numCols;i++)
{
myQTreeView->resizeColumnToContents(i);
if(i==0)
{
myQTreeView->header()->setResizeMode(i,QHeaderView::Stretch);
}
else
{
myQTreeView->header()->setResizeMode(i,QHeaderView::Interactive);
}
}
But this does not work as expected for 1st column. Although the 1st column stretches but it is not resizable/interactive like rest of the columns. Hence I want to add stretch+interactive for the 1st column. Rest of the 4 columns might as well be interactive only.
Is this possible?
According to the documentation for QHeaderView::ResizeMode, when the mode has been set to QHeaderView::Stretch...
The size cannot be changed by the user or programmatically.
So, no, I don't think it's possible to achieve what you want using the standard APIs.
You could try setting the resize mode to QHeaderView::Custom and overriding the various mouse event handlers in the QHeaderView by either installing an event filter on the existing QHeaderView or by creating your own class inheriting from QHeaderView and installing an instance of it in your view via QTreeView::setHeader.

I want to hide the row number column in QTableWidget

Normal QTablewidget is as follows:
a b
1 data data
2 data data
3 data data
I want to remove the first column that shows row numbers.
My table should look like this:
a b
data data
data data
data data
I can not find the way to do this. Let me know if there is a way to add items more easily.
That 's how I implemented my table:
QString a[5];
a[0]="ddd";
QTableWidgetItem *item1 = new QTableWidgetItem(a[0]);
ui->tableWidget->setItem(0,0,item1 );
There are two views in QTable the vertical and the horizontal header, they are defined in QTableView, as any widget you can hide them so it can be done as below :
ui->tableWidget->verticalHeader()->setVisible(false); // Get Vertical header and hide it
You can have vertical header in easy way as one line code,
ui->tableWidget->setHorizontalHeaderLabels(QString("HEADER 1;HEADER 2;HEADER 3;HEADER 4").split(";"));
For setting the data in Table you can use QString Array which contains data using for loop you can store the data in table, but you need to take care about indexing.

variable column number in qtreewidget

I want to have a qtreewidget with 1 column header but its children have more columns, I tried below code but I want to hide subsidies header too.
treeView = new QTreeView;
treeView->setModel(completer->model());
treeView->header()->hide();
treeView->expandAll();
thanks
You should reimplement QHeaderView for your needs, because QTreeView uses number of columns for root items.

QTableWidget - combobox delegate how do I allow different options per cell

Aloha
I have a QTableWidget with two columns that are currently using a ComboboxDelegate (my subclass of QItemDelegate) to present options to the user. I'd like the choice in the first column to effect the options available in the second, for the current row only.
E.g have a list of cars in the first column, and in the second a list of colours which are available for that car. Other rows to have different cars selected and thus different colour choices available.
From what I can see, I can only set an item delegate per row or column, so I can't see how to change the options in the second column's delegate without affecting all the other rows.
Is this possible? I'd really like to avoid going to a full view/model separation as I have quite a bit of code looking at this QTableWidget already (and I'm under time pressure)
Well for those interested; I went back to my pre-delegate approach, which was to use QTableWidget::setItemWidget() to provide a combobox widget for each cell.
I subclassed qcombobox to take a reference to the table, and connected the combobox CurrentIndexChanged with a slot to update the table data.
(setting a widget in a cell does not affect the tablewidget data unless you do this).
Using a full combobox like this is more expensive than an itemdelegate, but my tables are very small so I can get away with it. The rendering of the combobox is not as nice as the delegate (the combobox is visible all the time instead of only during editing in the delegate's case), but with time I'm sure I can improve on this.

QTableWidget change selection mode

When clicking on QTableWidget cell, it selects only the cell. How to configure tablewidget, so that when click on a cell , the whole row will be selected which contains the cell?
It can be done using signal,slots. I'm curious is there standard way doing it?
Simply use setSelectionBehavior
QTableView * tmp = new QTableView();
tmp->setSelectionBehavior(QAbstractItemView::SelectRows);
http://doc.qt.io/qt-5/qabstractitemview.html#SelectionBehavior-enum