2

我正在开发一个需要在屏幕左侧有一个包含多个项目(文本)的菜单的应用程序。我想要显示的唯一项目是实际文本和突出显示栏。我还想修改突出显示栏,以便:我可以为它制作动画,并将其从一个选择滑到下一个 b。我可以使用带有圆角的自定义像素图而不是默认的突出显示颜色

我尝试使用 QListWidget 和样式表并取得了一些成功,但我不相信使用这种方法可以使我的高亮栏的角落变圆。我也不确定我是否可以为栏从一个项目移动到下一个项目设置动画:

preset_list_view->setStyleSheet("QListView {color: rgb(230, 230, 230); background-color: rgba(0,0,0,0); border-style: none} QListView::item:selected {background-image: url(:/ui_resources/elements-preset_select/highlight_bar_270x30-black_bg.bmp)}");

我在网上查遍了,并没有找到太多。有人提到修改 QListWidget 的委托,但描述含糊不清。我也不确定这是否能解决我的动画问题。

有任何想法吗?

4

2 回答 2

4

您可以在 QListWidget 顶部放置一个半透明的惰性小部件,并在选择更改时对其进行动画处理。但是您还需要一个委托来禁用正常选择指示器。

一个工作示例:

#include <QListWidget>
#include <QFrame>
#include <QPropertyAnimation>
#include <QStyledItemDelegate>

class RemoveSelectionDelegate : public QStyledItemDelegate {
public:
    RemoveSelectionDelegate(QObject *parent = 0)
        : QStyledItemDelegate(parent) {
    }

    void paint(QPainter *painter, const QStyleOptionViewItem &option,
               const QModelIndex &index) const {
        // Call the original paint method with the selection state cleared
        // to prevent painting the original selection background
        QStyleOptionViewItemV4 optionV4 =
            *qstyleoption_cast<const QStyleOptionViewItemV4 *>(&option);
        optionV4.state &= ~QStyle::State_Selected;
        QStyledItemDelegate::paint(painter, optionV4, index);
    }
};

class ListWidget : public QListWidget {
    Q_OBJECT
public:
    ListWidget(QWidget *parent = 0)
        : QListWidget(parent)
        , selectionFrame(this)
        , animation(&selectionFrame, "geometry") {
        // Create a semi-transparent frame that doesn't interact with anything
        selectionFrame.setAttribute(Qt::WA_TransparentForMouseEvents);
        selectionFrame.setFocusPolicy(Qt::NoFocus);

        // You can put your transparent image in that stylesheet
        selectionFrame.setStyleSheet("background: solid rgba(0, 0, 125, 25%);");
        selectionFrame.hide();
        animation.setDuration(250);
        animation.setEasingCurve(QEasingCurve::InOutBack);

        connect(this,
                SIGNAL(currentItemChanged(QListWidgetItem*, QListWidgetItem*)),
                SLOT(updateSelection(QListWidgetItem*)) );        
        setItemDelegate(new RemoveSelectionDelegate(this));
    }

private slots:
    void resizeEvent(QResizeEvent *e) {
        QListWidget::resizeEvent(e);
        updateSelection(currentItem());
    }

    void updateSelection(QListWidgetItem* current) {
        animation.stop();
        if (!current) {
            selectionFrame.hide();
            return;
        }
        if (!selectionFrame.isVisible()) {
            selectionFrame.setGeometry(visualItemRect(current));
            selectionFrame.show();
            return;
        }
        animation.setStartValue(selectionFrame.geometry());
        animation.setEndValue(visualItemRect(current));
        animation.start();
    }
private:
    QFrame selectionFrame;
    QPropertyAnimation animation;
};
于 2011-09-01T22:30:27.613 回答
1

因此,如果它只是文本,那么为什么不使用带有 QLabels 的 QDockwidget。

例如,看一下左侧的'Qt Designer's 'Widget Box',您可以将其拖动并放置在顶部。那是你要找的吗?

您可以根据需要移动dockwidget。

于 2011-09-01T19:03:20.550 回答