3

QTableView、QAbstractTableModel、および QItemDelegate をサブクラス化しました。マウスオーバーで単一のセルをホバーすることができます:

void SchedulerDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
{
    ...

    if(option.showDecorationSelected &&(option.state & QStyle::State_Selected))
{
    QColor color(255,255,130,100);
    QColor colorEnd(255,255,50,150);
    QLinearGradient gradient(option.rect.topLeft(),option.rect.bottomRight());
    gradient.setColorAt(0,color);
    gradient.setColorAt(1,colorEnd);
    QBrush brush(gradient);
    painter->fillRect(option.rect,brush);
}

    ...
}

...しかし、行全体をホバーする方法がわかりません。誰かがサンプルコードを手伝ってくれますか?

4

2 に答える 2

1

2つの方法があります..

1)デリゲートを使用して行の背景を描画できます...
デリゲートで強調表示する行を設定し、それに基づいて強調表示を行う必要があります。

2) 現在の行の信号をキャッチします。その行のアイテムを繰り返し処理し、各アイテムの背景を設定します。

スタイルシートを試すこともできます:

QTableView::item:hover {
    background-color: #D3F1FC;
}        

願っています, それはあなたたちに役立つでしょう.

于 2014-04-16T13:52:58.263 に答える
0

これが私の実装です。うまく機能します。まず、QTableView/QTabWidget をサブクラス化し、mouseMoveEvent/dragMoveEvent 関数で QStyledItemDelegate に信号を送信する必要があります。この信号は、ホバリング インデックスを送信します。

QStyledItemDelegate では、メンバー変数 hover_row_(上記のシグナルへのスロット バインドで変更) を使用して、ペイント関数にどの行をホバーするかを伝えます。

コード例は次のとおりです。

//1: Tableview :
void TableView::mouseMoveEvent(QMouseEvent *event)
{
    QModelIndex index = indexAt(event->pos());
    emit hoverIndexChanged(index);
    ...
}
//2.connect signal and slot
    connect(this,SIGNAL(hoverIndexChanged(const QModelIndex&)),delegate_,SLOT(onHoverIndexChanged(const QModelIndex&)));

//3.onHoverIndexChanged
void TableViewDelegate::onHoverIndexChanged(const QModelIndex& index)
{
    hoverrow_ = index.row();
}

//4.in Delegate paint():
void TableViewDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
{
...
    if(index.row() == hoverrow_)
    {
        //HERE IS HOVER COLOR
        painter->fillRect(option.rect, kHoverItemBackgroundcColor);
    }
    else
    {
        painter->fillRect(option.rect, kItemBackgroundColor);
    }
...
}
于 2017-09-16T07:09:13.950 に答える