Qtreeview,在选择线条时调整其大小

发布于 2024-09-27 11:29:55 字数 460 浏览 5 评论 0原文

我正在使用 Qt 4.7.0,一个具有多列的 Qtreeview。

我想做的是“简单”:我想要一条线在被选择时增加其高度

代表们足以做到这一点吗?

我已经使用 QTableView 经历了一些事情:

m_pMyTableView->verticalHeader()->setResizeMode(QHeaderView::Interactive);
...
QSize AbstractItemDelegate::sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const;

它正在使用这个表视图,但我不知道如何在 QTreeview 上执行此操作,因为首先,它没有任何垂直标题......

有人可以照亮我的道路吗?

I'm using Qt 4.7.0, a Qtreeview with multiple columns.

What I want to do is "simple" : I want a line to increase its height, when it's selected.

Will delegates be enough to do this ?

I've been through some stuff with a QTableView :

m_pMyTableView->verticalHeader()->setResizeMode(QHeaderView::Interactive);
...
QSize AbstractItemDelegate::sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const;

It's working with this tableview, but I can't see how I'll do this on a QTreeview, since, for a start, it doesn't have any vertical headers...

Can someone enlight my path please ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

梦忆晨望 2024-10-04 11:29:55

除了在 QTreeView 中设置 uniformRowHeights 关闭之外,我还会尝试这样做。

有几种方法可以做到这一点,我喜欢使用 Qt 的信号/槽,因此我们将通过 QTreeView 上的自定义 QAbstractItemModel 来更改高度。此自定义模型将连接到来自 QTreeViewQItemSelectionModel 的信号 selectionChanged。示例代码/片段适用于单选模式,但您可以轻松更改它以处理多个选定的行。

第 1 步 - 使用选择槽创建自定义模型

创建从 QAbstractItemModel 派生的自定义模型类,并确保创建一个槽,例如:

Q_SLOTS:
    void onSelectionChanged( const QItemSelection&, const QItemSelection& );

在模型类中添加以下代码片段/方法。

void MyModelClass::onSelectionChanged( const QItemSelection& selected, 
                                       const QItemSelection& deselected )
{
    if( !selected.empty() )
    {
        // Save the index within the class.            
        m_selectedIndex = selected.first();

        Q_EMIT dataChanged( m_selectedIndex, m_selectedIndex );
    }
}

QVariant MyModelClass::data( const QModelIndex& index, int role ) const 
{
    // Use the selected index received from the selection model.
    if( m_selectedIndex.isValid() &&
        index == m_selectedIndex && 
        role == Qt::SizeHintRole )
    {
        // Return our custom size!
        return QSize( 50, 50 );
    }

    ...
 }

第 2 步 - 将选择更改连接到您的模型

QTreeView 的初始化中创建自定义模型并执行以下操作:

MyTreeView::MyTreeView( QWidget* parent ) : QWidget( parent )
{
    ...
    MyModelClass* model = new MyModelClass();
    setModel( model );

    setSelectionMode( QAbstractItemView::SingleSelection );
    setSelectionBehavior( QAbstractItemView::SelectRows );

    connect
    ( 
        selectionModel(), 
        SIGNAL( selectionChanged(const QItemSelection&, const QItemSelection&) ),
        model,
        SLOT( onSelectionChanged(const QItemSelection&, const QItemSelection&) )
    );
}

我确信有几种方法可以做到这一点,即将 QItemSelectionModel 直接交给您的 QAbstractItemModel ,但我还是更喜欢使用信号/槽并将选择保存在模型中。

希望这有帮助。

Along with setting uniformRowHeights off in in your QTreeView here is what I would try.

There are few ways to do this, I like to use Qt's signals/slots, as such we're going to change the height through a custom QAbstractItemModel on the QTreeView. This custom model will be connected to the signal selectionChanged from the QItemSelectionModel of your QTreeView. The example code/snippets work with single selection mode but you can easily alter it to handle multiple selected rows.

Step 1 - Create Custom Model with Selection slot

Create the custom model class that derives from QAbstractItemModel and make sure you create a slot such as:

Q_SLOTS:
    void onSelectionChanged( const QItemSelection&, const QItemSelection& );

Inside your model class add the following snippets/methods.

void MyModelClass::onSelectionChanged( const QItemSelection& selected, 
                                       const QItemSelection& deselected )
{
    if( !selected.empty() )
    {
        // Save the index within the class.            
        m_selectedIndex = selected.first();

        Q_EMIT dataChanged( m_selectedIndex, m_selectedIndex );
    }
}

QVariant MyModelClass::data( const QModelIndex& index, int role ) const 
{
    // Use the selected index received from the selection model.
    if( m_selectedIndex.isValid() &&
        index == m_selectedIndex && 
        role == Qt::SizeHintRole )
    {
        // Return our custom size!
        return QSize( 50, 50 );
    }

    ...
 }

Step 2 - Connect Selection Changes to Your Model

Inside the initialization of your QTreeView create your custom model and do the following:

MyTreeView::MyTreeView( QWidget* parent ) : QWidget( parent )
{
    ...
    MyModelClass* model = new MyModelClass();
    setModel( model );

    setSelectionMode( QAbstractItemView::SingleSelection );
    setSelectionBehavior( QAbstractItemView::SelectRows );

    connect
    ( 
        selectionModel(), 
        SIGNAL( selectionChanged(const QItemSelection&, const QItemSelection&) ),
        model,
        SLOT( onSelectionChanged(const QItemSelection&, const QItemSelection&) )
    );
}

I'm sure there are a few ways of doing this, i.e. handing the QItemSelectionModel directly to your QAbstractItemModel but again I prefer to use signals/slots and to save the selection in the model.

Hope this helps.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文