sark9012
sark9012

Reputation: 5747

QTableWidget headers not displaying

I use the UI editor to create a QTableWidget.

MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    populateFilesTable();
    connect(ui->browseButton, SIGNAL(clicked()), this, SLOT(selectDirectory()));
    connect(ui->searchButton, SIGNAL(clicked()), this, SLOT(findFiles()));
}

This shows how the UI is setup and then I call the function populateFilesTable().

The function is as follows:

void MainWindow::populateFilesTable()
{
    ui->filesTable->setSelectionBehavior(QAbstractItemView::SelectRows);

    QStringList labels;
    labels << tr("Filename") << tr("Size");
    ui->filesTable->setHorizontalHeaderLabels(labels);
    ui->filesTable->horizontalHeader()->setResizeMode(0, QHeaderView::Stretch);
    ui->filesTable->verticalHeader()->hide();
    ui->filesTable->setShowGrid(true);
}

The headers aren't being displayed on the table, any ideas? Thanks.

Upvotes: 4

Views: 9318

Answers (2)

Meghraj Nair
Meghraj Nair

Reputation: 1

For PyQt5,

check if it is self.tableWidget.horizontalHeader().setVisible(True) and not self.tableWidget.horizontalHeader().setVisible(False)

Idk why but after converting ui file to py file, this attribute is set as false by default.

Upvotes: 0

Tay2510
Tay2510

Reputation: 5978

What's wrong?

The horizontal header needs the information of columns from QTableWidget. As a QTableWidget is instantiated, both column count and row count are null hence you got no headers show even if you called setHorizontalHeaderLabels.


Solution

Insert columns before you set the header:

ui->filesTable->insertColumn(0);
ui->filesTable->insertColumn(1);

QStringList labels;
labels << tr("Filename") << tr("Size");
ui->filesTable->setHorizontalHeaderLabels(labels);

Upvotes: 5

Related Questions