开发者

HowTo draw border for QTableWidget row?

I'm trying to make a border for rows in QTableWidget with different ways, but all solutions don't respond my requirements. All that I want, is to draw a rectangle around a whole row. I had try QStyledItemDelegate class, but that is not my way, because delegates are used only for item[ row, column ], not for the whole rows or columns.

开发者_运维技巧Here is wrong solution:

/// @brief Рисуем границу вокруг строки. 
class DrawBorderDelegate : public QStyledItemDelegate
{
public:
     DrawBorderDelegate( QObject* parent = 0 ) : QStyledItemDelegate( parent ) {}
     void paint( QPainter* painter, const QStyleOptionViewItem& option, const QModelIndex& index ) const;

}; // DrawBorderDelegate

void DrawBorderDelegate::paint( QPainter* painter, const QStyleOptionViewItem& option, const QModelIndex& index ) const
{
     QStyleOptionViewItem opt = option;

     painter->drawRect( opt.rect );

     QStyledItemDelegate::paint( painter, opt, index );  
}

And somewhere in code:

tableWidget->setItemDelegateForRow( row, new DrawBorderDelegate( this ) );

Thanks for help!


Your solution was not far wrong. You just need to be a bit more selective about which edges of the rectangle you draw:

void DrawBorderDelegate::paint( QPainter* painter, const QStyleOptionViewItem& option, const QModelIndex& index ) const
{
     const QRect rect( option.rect );

     painter->drawLine( rect.topLeft(), rect.topRight() );
     painter->drawLine( rect.bottomLeft(), rect.bottomRight() );

     // Draw left edge of left-most cell
     if ( index.column() == 0 )
         painter->drawLine( rect.topLeft(), rect.bottomLeft() );

     // Draw right edge of right-most cell
     if ( index.column() == index.model()->columnCount() - 1 )
         painter->drawLine( rect.topRight(), rect.bottomRight() );

     QStyledItemDelegate::paint( painter, option, index );
}

Hope this helps!


  #include <QTableWidget>     
  QTableWidget* table = new QTableWidget();

  table->resize(400, 250);

  table->setSizePolicy(QSizePolicy::Expanding,QSizePolicy::Expanding);
  table->setColumnCount(3);
  table->setRowCount(2);

  //Set Header Label Texts Here
  table->verticalHeader ()->hide();
  table->horizontalHeader()->hide();

  table->setItem(0,0,new QTableWidgetItem("CELL 1"));
  table->setItem(0,1,new QTableWidgetItem("CELL 2"));
  table->setItem(0,2,new QTableWidgetItem("CELL 3"));

  table->setItem(1,0,new QTableWidgetItem("CELL 4"));
  table->setItem(1,1,new QTableWidgetItem("CELL 5"));
  table->setItem(1,2,new QTableWidgetItem("CELL 6"));

  table->setEditTriggers(QAbstractItemView::NoEditTriggers);
  table->setFocusPolicy(Qt::NoFocus);
  table->setSelectionMode(QAbstractItemView::NoSelection);

  table-> setObjectName (QString :: fromUtf8 ("table_"));
  table->show();

Stylesheet:

QTableWidget
{
    background-color : none;
    gridline-color: white;    // this border for rows and columns
    color:#ffffff;
}

QTableWidget#table_{
     border:1px solid #ffffff;   // this border for total table
}

sample output

HowTo draw border for QTableWidget row?

Hope this simple way to helps!!!

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜