2016-05-25 2 views
0

У меня есть QTableView, как показано ниже:Удалите вертикальные линии сетки из QTableView

enter image description here

Я хочу, чтобы удалить все вертикальные линии из таблицы. Я попытался установить свойство gridline-color, эквивалентное background-color, но он удалил все линии сетки.

Я хочу, чтобы горизонтальные линии сетки оставались и удаляли вертикальные. Как я могу это достичь?

+0

Пожалуйста, поделитесь код –

+0

, какая часть кода ?? –

+0

таблица и свойства стиля. –

ответ

2

Вы не можете. Для этого нет опции для QTableView.

Однако, вы можете сделать что-то вроде установки gridline-color свойства background-color (как вы это делали) , а затем установив границу по всем пунктам вашей QTableView; как вы хотите только горизонтальные линии сетки, она будет выглядеть следующим образом:

QTableView::item{ 
    border-top : 1px solid black 
    border-bottom : 1px solid black 
} 
0

Используйте setStyleSheet() с QTableView и внутри, которые дает пограничного-правого цвет и пограничного-левого цвета к цвету что вы дали для gridline-color.

2

delegate.h

class QLineDelegate : public QStyledItemDelegate 
{ 
    public: 
    QLineDelegate(QTableView* tableView); 

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

    private: 
    QPen pen; 
    QTableView* view; 
}; 

delegate.cpp

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

void QLineDelegate::paint(QPainter* painter, const QStyleOptionViewItem& option,const QModelIndex& index)const 
{ 
    QStyledItemDelegate::paint(painter, option, index); 
    QPen oldPen = painter->pen(); 
    painter->setPen(pen); 

    //draw verticalLine 
    //painter->drawLine(option.rect.topRight(), option.rect.bottomRight()); 

    //draw horizontalLine 
    //painter->drawLine(option.rect.bottomLeft(), option.rect.bottomRight()); 
    //above code, line have breakpoint, the following code can solve it well 

    QPoint p1 = QPoint(itemOption.rect.bottomLeft().x()-1,itemOption.rect.bottomLeft().y()); 
    QPoint p2 = QPoint(itemOption.rect.bottomRight().x()+1,itemOption.rect.bottomRight().y()); 
    painter->drawLine(p1, p2); 
    painter->setPen(oldPen); 
} 

tableview.cpp

tableView->setShowGrid(false); 
tableView->setItemDelegate(new QLineDelegate(tableView)); 

 Смежные вопросы

  • Нет связанных вопросов^_^