我想在QtCreator中创建一个表,我的代码:
ui->tableWidget->setSelectionBehavior(QAbstractItemView::SelectRows);
ui->tableWidget->setSelectionMode(QAbstractItemView::SingleSelection);
ui->tableWidget->setColumnCount(5);
ui->tableWidget->setHorizontalHeaderLabels(QStringList() << tr("First") << tr("Second") << tr("Third") << tr("Fourth") << tr("Fifth"));
ui->tableWidget->setColumnWidth(0, 80);
ui->tableWidget->setColumnWidth(1, 140);
ui->tableWidget->setColumnWidth(2, 80);
ui->tableWidget->setColumnWidth(3, 140);
ui->tableWidget->setStyleSheet("QHeaderView::section { background-color: rgb(217,217,217)}");
ui->tableWidget->horizontalHeader()->setStretchLastSection(true);
for (int c = 0; c < ui->tableWidget->horizontalHeader()->count(); c++) {
ui->tableWidget->horizontalHeaderItem(c)->setSizeHint(QSize(0, 50));
}
for (int j = 0; j < ui->tableWidget->columnCount(); j++) {
QTableWidgetItem *item = new QTableWidgetItem;
if ((j == 0) || (j == 3) || (j == 4)) {
item->setFlags(item->flags() & ~Qt::ItemIsEditable);
}
if (j!=0) {
item->setTextAlignment(Qt::AlignCenter);
}
ui->tableWidget->setItem(0, j, item);
}
表中只有一行,我使用Designer进行操作,因此上面的代码中没有行数。 结果:
使用上面相同的代码,我在Visual Studio中进行并接收此表:
VS的行高是表的高度,太大了。如何在QtCreator中收到小行?