1. 程式人生 > >QTableWidget去除表格虛線框,設定表格樣式等操作

QTableWidget去除表格虛線框,設定表格樣式等操作

QTableWidget去除表格虛線框:

class NoFocusDelegate : public QStyledItemDelegate
{
	Q_OBJECT

public:
	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);
	}
};

樣式設定:

m_ui->TableWidget->setSelectionBehavior(QAbstractItemView::SelectRows);  //按行選中
m_ui->TableWidget->setSelectionMode(QAbstractItemView::SingleSelection); //選中單行
m_ui->TableWidget->setEditTriggers(QAbstractItemView::NoEditTriggers); //不可編輯
m_ui->TableWidget->setItemDelegate(new NoFocusDelegate()); //設定無虛線
m_ui->TableWidget->horizontalHeader()->setHighlightSections(false);  //防止表頭塌陷
m_ui->TableWidget->horizontalHeader()->setFixedHeight(28); //設定表頭高度
m_ui->TableWidget->horizontalHeader()->setFocusPolicy(Qt::NoFocus); //設定表頭不可選