如何在QTableView中设置特定单元格的线条样式? [英] How can I set the line style of a specific cell in a QTableView?

查看:2494
本文介绍了如何在QTableView中设置特定单元格的线条样式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用QT GUI。我使用QTableView实现一个简单的十六进制编辑控件。我最初的想法是使用一个有十七列的表。表的每一行将具有16个十六进制字节,然后是第十七列中该数据的ASCII表示。理想情况下,我想编辑/设置第十七列的样式,使每个单元格的顶部和底部没有线条,使文本具有自由流畅的外观。

I am working with a QT GUI. I am implementing a simple hex edit control using a QTableView. My initial idea is to use a table with seventeen columns. Each row of the table will have 16 hex bytes and then an ASCII representation of that data in the seventeenth column. Ideally, I would like to edit/set the style of the seventeenth column to have no lines on the top and bottom of each cell to give the text a free flowing appearance. What is the best way to approach this using the QTableView?

推荐答案

我可以考虑几种方法来做你所需要的;两者都将包括绘制自定义网格,因为它看起来没有直接的方式挂钩到QTableView类的网格绘制例程:

I could think about a couple of ways of doing what you need; both would include drawing custom grid as it looks like there is no straight forward way of hooking into the grid painting routine of QTableView class:

1.关闭标准网格你的树视图网格通过调用setShowGrid(false)和绘制网格线为需要他们使用项委托的单元格。下面是一个例子:

1.Switch off the standard grid for your treeview grid by calling setShowGrid(false) and draw grid lines for cells which need them using item delegate. Below is an example:

// custom item delegate to draw grid lines around cells
class CustomDelegate : public QStyledItemDelegate
{
public:
    CustomDelegate(QTableView* tableView);
protected:
    void paint(QPainter* painter, const QStyleOptionViewItem& option, const QModelIndex& index) const;
private:
    QPen _gridPen;
};

CustomDelegate::CustomDelegate(QTableView* tableView)
{
    // create grid pen
    int gridHint = tableView->style()->styleHint(QStyle::SH_Table_GridLineColor, new QStyleOptionViewItemV4());
    QColor gridColor = static_cast<QRgb>(gridHint);
    _gridPen = QPen(gridColor, 0, tableView->gridStyle());
}

void CustomDelegate::paint(QPainter* painter, const QStyleOptionViewItem& option, const QModelIndex& index) const
{
    QStyledItemDelegate::paint(painter, option, index);

    QPen oldPen = painter->pen();
    painter->setPen(_gridPen);

    // paint vertical lines
    painter->drawLine(option.rect.topRight(), option.rect.bottomRight());
    // paint horizontal lines 
    if (index.column()!=1) //<-- check if column need horizontal grid lines
        painter->drawLine(option.rect.bottomLeft(), option.rect.bottomRight());

    painter->setPen(oldPen);
}

// set up for your tree view:
ui->tableView->setShowGrid(false);
ui->tableView->setItemDelegate(new CustomDelegate(ui->tableView));

2.创建一个QTableView后代并覆盖 paintEvent 方法。在那里你可以绘制自己的网格或让基类绘制它,然后使用tableview的背景颜色在网格的顶部绘制水平线。

2.Create a QTableView descendant and override the paintEvent method. There you could either draw your own grid or let base class to draw it and then paint horizontal lines on top of the grid with using tableview's background color.

希望这有帮助,

这篇关于如何在QTableView中设置特定单元格的线条样式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆