HowTo draw border for QTableWidget row?

♀尐吖头ヾ 提交于 2019-12-11 05:18:14

问题


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!


回答1:


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!




回答2:


  #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

Hope this simple way to helps!!!



来源:https://stackoverflow.com/questions/7416503/howto-draw-border-for-qtablewidget-row

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!