3

中に線を引きたいQTableWidgetItem。線を引くために、QStyledItemDelegate::paintメソッドを再実装しました。

しかし、スクロールしたり、QTableWidget. 一部のアイテムの描画効果が失われます。

これが私のペイントの実装です:

void DrawLineDelegate::paint(QPainter *poPainter, const QStyleOptionViewItem &oOption, const QModelIndex &oIndex) const
{
    // Valid index ?
    if(!oIndex.isValid())
        // Not valid.
        return;

    const QRect oRect( oOption.rect );

    // Painter has certain settings
    poPainter->save();

    // Draw line
    QColor oLineColor (oIndex.data(Qt::UserRole).toString());

    poPainter->setRenderHint(QPainter::Antialiasing);
    poPainter->setPen(QPen(oLineColor, 2, Qt::SolidLine, Qt::RoundCap));
    poPainter->drawLine(oRect.left(),                       // Start X-coordinate
                        oRect.top() - oRect.height() / 2 ,  // Center Height (Y-coordinate)
                        oRect.left() + oRect.width(),       // Line width
                        oRect.top() - oRect.height() / 2);  // Center Height (Y-coordinate)

    poPainter->restore();

    QStyledItemDelegate::paint( poPainter, oOption, oIndex );
}

TableWidget init 関数で、デリゲート項目を次のように設定します

ui->tableWidget->setItemDelegateForColumn(2, new DrawLineDelegate(this) );

注:各アイテムの色名は として保存していQt::UserDataます。

テーブルの初期化では、線がうまく描画されています。バグは、テーブルで遊んでいるときです。

ここにいくつかのスクリーンショットがあります

スクロール前

スクロール後

行選択後

助言がありますか ?

4

1 に答える 1