mos*_*osg 1 qt qt4 qtablewidget
我正在尝试以QTableWidget不同的方式为行制作边框,但所有解决方案都没有响应我的要求。我想要的只是围绕整行绘制一个矩形。我有尝试QStyledItemDelegate类,但这不是我的方式,因为代表仅用于项目[行,列],而不是用于整个行或列。
这是错误的解决方案:
/// @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 );
}
Run Code Online (Sandbox Code Playgroud)
在代码中的某处:
tableWidget->setItemDelegateForRow( row, new DrawBorderDelegate( this ) );
Run Code Online (Sandbox Code Playgroud)
感谢帮助!
你的解决方案并没有错。您只需要对绘制的矩形的哪些边更有选择性:
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 );
}
Run Code Online (Sandbox Code Playgroud)
希望这可以帮助!