QT5---QTableWidget简单应用之文件浏览器
实现以下这个简单的文件浏览功能,经常使用的QTableWidget设置都用到了。
基本设置
ui->tableWidget->setColumnCount(5); //设置表格列数
ui->tableWidget->horizontalHeader()->setStretchLastSection(true); //设置表格是否充满,即行位不留空
ui->tableWidget->setSelectionBehavior(QAbstractItemView::SelectRows);//设置鼠标选择行为:一个单元格/一行/一列
ui->tableWidget->horizontalHeader()->setFixedHeight(35); //设置表头高度
/*设置表头单元格内容*/
QStringList header;
header<<tr("File Name")<<tr("Size")<<tr("Double Click to Decode") << "Double Click to Close"<< tr("Decode Process");
ui->tableWidget->setHorizontalHeaderLabels(header);
/*设置表头字体加粗*/
QFont font = ui->tableWidget->horizontalHeader()->font();
font.setBold(true);
ui->tableWidget->horizontalHeader()->setFont(font);
ui->tableWidget->setFrameShape(QFrame::NoFrame); //设置无边框
ui->tableWidget->verticalHeader()->hide(); //设置垂直的头部隐藏,不隐藏默认会显示列表数字
ui->tableWidget->horizontalHeader()->resizeSection(0,150); //设置第0列的宽度为150px
ui->tableWidget->setItemDelegate(new NoFocusDelegate()); //设置鼠标行时,不显示单元格虚框,NoFocusDelegate类实现如附录
ui->tableWidget->setMouseTracking(true); //设置鼠标捕捉,不设置。如鼠标进入等动作检測不到
ui->tableWidget->setStyleSheet("selection-background-color:lightblue;");//设置行选中时背景颜色
ui->tableWidget->horizontalHeader()->setStyleSheet("QHeaderView::section{background:greenyellow;}");//表头背景颜色
/*信号槽,单元格双击*/
connect(ui->tableWidget, SIGNAL(cellDoubleClicked(int,int)),
this, SLOT(sdlog2Decode(int,int)));
/*信号槽,鼠标进入单元格*/
connect(ui->tableWidget,SIGNAL(itemEntered(QTableWidgetItem*)),
this,SLOT(itemEnteredHover(QTableWidgetItem *)));
/*加入构件到单元格,如button。进度条等*/
QProgressBar *pgbar = new QProgressBar(ui->tableWidget);
pgbar->setTextVisible(false);
ui->tableWidget->setCellWidget(row,column,pgbar); //替换当前widget
/*设置单元格背景颜色*/
QTableWidgetItem *itemtemp = ui->tableWidget->item(row, col);
itemtemp->setBackgroundColor(QColor(255,255,255,255));
ui->tableWidget->removeCellWidget(row,col); //移除单元格中的构件
ui->tableWidget->removeRow(row); //移除表格某一行
int col = ui->tableWidget->columnCount(); //获取表格列数
int row = ui->tableWidget->rowCount(); //获取行数
/*插入第row行,以两列为例*/
ui->tableWidget->insertRow(row);
QTableWidgetItem *Item1 = new QTableWidgetItem("hello1");
Item1->setTextAlignment(Qt::AlignCenter | Qt::AlignVCenter);
Item1->setFlags(Item1->flags() ^ Qt::ItemIsEditable);
QTableWidgetItem *Item2 = new QTableWidgetItem("hello2"); //设置单元格内容
Item2->setTextAlignment(Qt::AlignCenter | Qt::AlignVCenter); //设置单元格内容对其方式
Item2->setFlags(Item2->flags() ^ Qt::ItemIsEditable); //设置单元格是否可编辑
ui->tableWidget->setItem(row, 0, Item1);
ui->tableWidget->setItem(row, 1, Item2);
/*设置各种文件icon*/
QFileInfo info;
QFile file(currentDir.absoluteFilePath(filesname));
info = QFileInfo(filesname);
qint64 size = info.size();
QFileIconProvider icon_provider;
QIcon icon = icon_provider.icon(info);
QTableWidgetItem *fileNameItem = new QTableWidgetItem(info.filePath());
fileNameItem->setIcon(icon);
fileNameItem->setTextAlignment(Qt::AlignCenter | Qt::AlignVCenter);
fileNameItem->setFlags(fileNameItem->flags() ^ Qt::ItemIsEditable);
附录程序
NoFocusDelegate.h
#ifndef NoFocusDelegate_H
#define NoFocusDelegate_H
#include <QPainter>
#include <QStyledItemDelegate>
class NoFocusDelegate :public QStyledItemDelegate
{
public:
NoFocusDelegate();
~NoFocusDelegate();
protected:
void paint(QPainter* painter, const QStyleOptionViewItem & option, const QModelIndex &index) const;
};
#endif // SETHIGHLIGHTSECTIONS_H
NoFocusDelegate.cpp
#include "NoFocusDelegate.h"
NoFocusDelegate::NoFocusDelegate()
{
}
NoFocusDelegate::~NoFocusDelegate()
{
}
void NoFocusDelegate::paint(QPainter* painter, const QStyleOptionViewItem & option, const QModelIndex &index) const
{
QStyleOptionViewItem itemOption(option);
if (itemOption.state & QStyle::State_HasFocus)
{
itemOption.state = itemOption.state ^ QStyle::State_HasFocus;
}
QStyledItemDelegate::paint(painter, itemOption, index);
}