Qt QTableWidget Column resizing - c++

I have a MainWindow with a QToolbar, QWidget and a QTabWidget. The layout is "Grid". However, my window is resizeable and since I have a layout it works well. But there is one problem, in my QTabWidget I have a QTableWidget with two columns (layout is also "Grid"). If I resize my whole window the QTableWidget resizes but not the columns.
For example Whenever I resize my window, my QTabWidget resizes and the QTableWidget in it too. Only the columns in my QTableWidget won't.
So... how can I resize them if my QTableWidget resizes?

Change the ResizeMode of the QHeaderView. For example, use:
horizontalHeader()->setResizeMode( 0, QHeaderView::Stretch );
to make the first column resize so the QTableWidget is always full.
Override the resizeEvent and set the widths of each column yourself when the QTableWidget has been resized.

To stretch last column:
ui->tableWidget->horizontalHeader()->setStretchLastSection(true);
To stretch column #n:
ui->tableWidget->horizontalHeader()->setSectionResizeMode(n, QHeaderView::Stretch);

The best solution for this, in Qt5 you have to use setSectionResizeMode instead of setResizeMode
tabv = QTableView()
tabv.horizontalHeader().setSectionResizeMode(QHeaderView::Stretch)
Also you can specify the Stretch mode when resizing
tabv.horizontalHeader().resizeSections(QHeaderView::Stretch)

ui->mytable->horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch);

If you want to resize just the last column:
ui->tableWidget->horizontalHeader()->setStretchLastSection(1);

You can change the "resize mode" of your columns or rows with the QHeaderView and the method QHeaderView::setResizeMode().
http://qt-project.org/doc/qt-4.8/qheaderview.html#setResizeMode
http://qt-project.org/doc/qt-4.8/qtableview.html#verticalHeader
http://qt-project.org/doc/qt-4.8/qtableview.html#horizontalHeader

In Qt5 you have to use setSectionResizeMode instead of setResizeMode
QTableWidget* myTable = new QTableWidet;
QHeaderView* header = myTable->horizontalHeader();
header->setSectionResizeMode(QHeaderView::Stretch);

Related

Columns auto-resize to size of QTableView

I am new to Qt and I have just managed to make a QTableView work with my model. It has fixed 3 columns. When I open a window, it look ok but when i resize the window, the QTableView itself gets resized but columns' width remains the same. Is there any build-in way to make it work? I want columns to resize to fit the edges of QTableView every the the window gets resized.
This code equally stretches each column so that they fit the table's width.
table->horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch);
Docs:
QHeaderView::setSectionResizeMode
See resize modes here.
There is a header flag to ensure that the QTableView's last column fills up its parent if resized. You can set it like so:
table_view->horizontalHeader()->setStretchLastSection(true);
However, that does not resize the other columns proportionately. If you want to do that as well, you could handle it inside the resizeEvent of your parent thusly:
void QParent::resizeEvent(QResizeEvent *event) {
table_view->setColumnWidth(0, this->width()/3);
table_view->setColumnWidth(1, this->width()/3);
table_view->setColumnWidth(2, this->width()/3);
QMainWindow::resizeEvent(event);
}
QParent class is subclass of QMainWindow.
Widgets QTableView, QTreeView and their derived classes (such as QTableWidget) have this two usefull methods:
QHeaderView* horizontalHeader() const;
QHeaderView* verticalHeader() const;
If you open documentation for a class QHeaderView, you will find methods that set up appearance and behavior of row or column header for item views. You can resolve your problem by one of these methods:
void QHeaderView::stretchLastSection( bool stretch )
As Davy Jones mentioned.
Example:
QTableView *table = new QTableView();
table->horizontalHeader()->setStretchLastSection(true);
void QHeaderView::setResizeMode( ResizeMode mode )
As mode you can set QHeaderView::Stretch or QHeaderView::ResizeToContents.
Unfortunately this method have a drawback - after it's apply you will not be able to change size of columns (or rows) manually (in GUI) or programmatically.
Example:
QTableView *table = new QTableView();
table->horizontalHeader()->setResizeMode(QHeaderView::Stretch);
In PyQt5 you can achieve this in your table_widget by doing:
header = table_widget.horizontalHeader()
header.setSectionResizeMode(QtWidgets.QHeaderView.ResizeToContents)

Reserving Space for QScrollArea

I am using a QScrollArea with a custom QWidget. The problem I am facing is that whenever the scrollbar appears, it leads to shifting of elements in the widget. So, I want to reserve some space, so when the scrollbar appears or disappears, the widget is not affected. How can I achieve this?
scrollArea->setWidgetResizable(false);
I encountered this problem and just solved it(might not completely).
I set a fixed width for scroll widget and QScrollArea, and set QScrollArea Horizontal SizePolicy fixed, and hide HorizontalScrollBar.
Sample code
QWidget *pWidget = new QWidget(this);
pWidget->setFixedWidth(500);
pWidget->setLayout(...)
QScrollArea *pScrollArea = new QScrollArea();
// Same with widget
pScrollArea->setFixedWidth(500);
pScrollArea->setWidget(pWidget);
pScrollArea->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Expanding);
// Needed.
pScrollArea->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
I also find a Bugreport: QTBUG-2347
QScrollArea: a new scroll policy that reserves space for the scroll bar, and it is closed. But I don't know whether the bug is fixed and what's the solution.

resize problem in scroll area

Hello everyone, here is my code:
myplot *p = new myplot(gao.structpayloadgraph,
gao1.structpayloadgraph,
gao.structcol-2, "payload");
ui->scrollArea->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
ui->scrollArea->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
ui->scrollArea->setWidgetResizable(false);
p->resize(ui->scrollArea->size().width(), ui->scrollArea->size().height());
ui->scrollArea->setWidget(p);
I want p to take up the full available space of the scrollbar area and fit itself. However, the appearance looks 'squeezed' even though I called the resize function. What should I do to achieve the desired effect?
You have to treat the scroll area content widget as a normal QWidget. If you want automatic resize and you must use layouts in Qt. Try the following :
QVBoxLayout layout = new QVBoxLayout( ui->scrollAreaContent);
layout->setMargin(0);
layout->setContentsMargins(0,0,0,0);
layout->setSpacing(0);
ui->scrollAreaContent->setLayout( layout);
layout->addWidget(p);
NOTE: ui->scrollAreaContent is a guess, but I think you are using ui files and default content widget is named like that ...
Go to the top right of the QT creator designer screen (Object, Class), right click on the QScrollArea row and select the "Lay Out" menu item, choose a layout (eg vertical or horizontal layout), make sure that your QWidget has a minimum or above size policy. Your scroll widget should now resize with the layout.

QTableWidget alignement with the borders of its parent QWidget

Let's consider we have QWidget that contains QTableWidget (only). So we want to resize the table by resizing the widget, and we dont want to have an indet between the QWidget border and cells of the table. What kind of property is making posible for QTableWidget to be aligned with the borders of it parent widget?
Thanks.
First, you want to make sure the QTableWidget is placed inside a layout. For instance,
QTableWidget* tw = new QTableWidget(parent_widget);
QVBoxLayout* layout = new QVBoxLayout();
layout->addWidget(tw);
parent_widget->setLayout(layout);
assuming parent_widget is already pointing to the widget containing the QTableWidget. This will ensure that the table resizes when the parent widget does. To have the table fill the entire space of the widget, just set the margin to zero on the layout. Try one of these:
layout->setMargin(0);
or
layout->setContentsMargins(0,0,0,0);

Change table columns width on resizing window or splitter

Consider there is a QTablWidget and a QTextEdit. Both of them are in a horisontal QSplitte. Let the QTable widget has 2 columns.
The problem is to resize the table columns' width as you do resize operation by moving the splitter with mouse. Are there any options to may colums to be resized synchornosly with the table?
Thanks.
QHeaderView *header = ui->tableWidget->horizontalHeader();
header->setResizeMode(QHeaderView::Stretch);
This code sets all columns of ui->tableWidget to equal width and let it change automatically.
And take a look on QHeaderView description in docs, you can do almost anything you can imagine with table columns with this API.
Sad, but you can't set any stretch factor or smth., if you need relational column widths not to be equal, but you still can reimplement sizeHint() or resize sections when header's geometriesChanged fires.