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.
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
.
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);