C++ Qt QTableWidget 列调整大小

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15686501/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 19:39:28  来源:igfitidea点击:

Qt QTableWidget Column resizing

c++qtqtablewidget

提问by Normal People Scare Me

I have a MainWindowwith a QToolbar, QWidgetand 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 QTabWidgetI have a QTableWidgetwith two columns (layout is also "Grid"). If I resize my whole window the QTableWidgetresizes but not the columns.

我有一个主窗口在QToolBarQWidget的QTabWidget。布局是“网格”。但是,我的窗口可以调整大小,而且由于我有一个布局,所以效果很好。但是有一个问题,在我的QTabWidget 中,我有一个带有两列的QTableWidget(布局也是“网格”)。如果我调整整个窗口的大小,QTableWidget 会调整大小,但不会调整列的大小。

For example Whenever I resize my window, my QTabWidgetresizes and the QTableWidgetin it too. Only the columns in my QTableWidgetwon't.

例如,每当我调整窗口大小时,我的QTabWidget和其中的QTableWidget也会调整大小。只有我的QTableWidget 中的列不会。

So... how can I resize them if my QTableWidgetresizes?

那么...如果我的QTableWidget调整大小,我该如何调整它们的大小?

回答by PrisonMonkeys

  1. Change the ResizeModeof the QHeaderView. For example, use:
  1. 更改ResizeModeQHeaderView。例如,使用:


horizontalHeader()->setResizeMode( 0, QHeaderView::Stretch );

to make the first column resize so the QTableWidgetis always full.

使第一列调整大小,以便QTableWidget始终充满。



  1. Override the resizeEventand set the widths of each column yourself when the QTableWidgethas been resized.
  1. 调整大小后,resizeEvent自己覆盖并设置每列的宽度QTableWidget

回答by fat

  1. To stretch last column:

    ui->tableWidget->horizontalHeader()->setStretchLastSection(true);
    
  2. To stretch column #n:

    ui->tableWidget->horizontalHeader()->setSectionResizeMode(n, QHeaderView::Stretch);
    
  1. 拉伸最后一列:

    ui->tableWidget->horizontalHeader()->setStretchLastSection(true);
    
  2. 拉伸列#n

    ui->tableWidget->horizontalHeader()->setSectionResizeMode(n, QHeaderView::Stretch);
    

回答by user4708486

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

回答by Jhon Escobar

The best solution for this, in Qt5 you have to use setSectionResizeModeinstead of setResizeMode

对此的最佳解决方案,在 Qt5 中,您必须使用setSectionResizeMode而不是setResizeMode

tabv = QTableView()
tabv.horizontalHeader().setSectionResizeMode(QHeaderView::Stretch)

Also you can specify the Stretchmode when resizing

您也可以Stretch在调整大小时指定模式

tabv.horizontalHeader().resizeSections(QHeaderView::Stretch)

回答by GENiEBEN

If you want to resize just the last column:

如果您只想调整最后一列的大小:

ui->tableWidget->horizontalHeader()->setStretchLastSection(1);

回答by Niklas

In Qt5 you have to use setSectionResizeModeinstead of setResizeMode

在 Qt5 中,您必须使用setSectionResizeMode而不是setResizeMode

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