Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Resize Column Width QTableWidget

I have this widget created with QTableWidget: enter image description here

and I would like that the column of my table resize in order to occupy the entire width of the widget, while for the rows is ok as it is. I know there is a similar question like mine but I was not able to solve it in that way.. This is my code

void MainWindow::createTable(int rows, int columns) {
    mainList = new QTableWidget;
    QStringList headerLabels;
    headerLabels << "Title" << "Director" << "Year" << "Counter" << "Rating";
    mainList->setRowCount(rows);
    mainList->setColumnCount(columns);
    mainList->setHorizontalHeaderLabels(headerLabels);
    mainList->setEditTriggers(QAbstractItemView::DoubleClicked | QAbstractItemView::SelectedClicked);
    mainList->setSelectionBehavior(QAbstractItemView::SelectRows);
    mainList->resizeColumnsToContents();
    mainList->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);

    setCentralWidget(mainList);
}
like image 987
ayasha Avatar asked Apr 22 '14 09:04

ayasha


People also ask

How do I change the size of a column in QTableWidget?

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.

How do you resize column width in a table?

Change column width To change the width to a specific measurement, click a cell in the column that you want to resize. On the Layout tab, in the Cell Size group, click in the Table Column Width box, and then specify the options you want.

How do you resize column?

Resize columns Select a column or a range of columns. On the Home tab, select Format > Column Width (or Column Height). Type the column width and select OK.

How do I resize a column in Jira?

Hold ALT (Option) and drag resizer. In this mode, you will redistribute the space between two adjacent columns - increasing the width of one column and decreasing the width of another.


1 Answers

Considering that you are using Qt5, give a try to

QTableWidget* mainList = new QTableWidget;
QHeaderView* header = mainList ->horizontalHeader();
header->setSectionResizeMode(QHeaderView::Stretch);

OR

There is a header flag to ensure that the QTableView's last column fills up its parent if resized.

header->setStretchLastSection(true);
like image 104
DNamto Avatar answered Oct 04 '22 03:10

DNamto