qt QListWidget 自定义Item插入功能注意事项

关于QListWidget  的去掉item虚线框的问题,

有一种办法就是通过delegate来实现,还有一种办法在多找找,找到最好,最适合的,

还有就是关于实在listwidget每个项目的高度,大小,看有几种办法实现,用哪种最好

 

QListWidget使用自定义的Item,当在进行插入删除操作的时候,会存在插入的位置不确定性,原因是因为创建过程,绑定过程,需要注意。

//将widget作为列表的item

//这里指不指定父窗口目前没有特别大的关系,可以显示出来

//如果需要对应的父窗口处理该窗口的一些事件,可以指定,操作可以更方便一些

/*

// qt源码,改函数是从setItemWidget调用接口内部跳过来的

//里面有行关键代码widget->setParent(viewport());

//所以设置了父窗口也没有用,所以一般也是不用设置的,

//如果需要和改窗口产生关联,可以连接一些关联接口就可以了

void QAbstractItemView::setIndexWidget(const QModelIndex &index, QWidget *widget)

{

    Q_D(QAbstractItemView);

    if (!d->isIndexValid(index))

        return;

    if (indexWidget(index) == widget)

        return;

    if (QWidget *oldWidget = indexWidget(index)) {

        d->persistent.remove(oldWidget);

        d->removeEditor(oldWidget);

        oldWidget->removeEventFilter(this);

        oldWidget->deleteLater();

    }

    if (widget) {

        widget->setParent(viewport());//*******

        d->persistent.insert(widget);

        d->addEditor(index, widget, true);

        widget->installEventFilter(this);

        widget->show();

        dataChanged(index, index); // update the geometry

        if (!d->delayedPendingLayout)

            widget->setGeometry(visualRect(index));

    }

}

*/

QWidget *widget = new QWidget(ui.listget);

/*

//这里不要指定父窗口,官网说会导致item循序的不确定性

// Constructs an empty list widget item of the specified type with the given parent.If parent is not specified, the item will need to be inserted into a list widget with QListWidget::insertItem().This constructor inserts the item into the model of the parent that is passed to the constructor. If the model is sorted then the behavior of the insert is undetermined since the model will call the '<' operator method on the item which, at this point, is not yet constructed. To avoid the undetermined behavior, we recommend not to specify the parent and use QListWidget::insertItem() instead.

*/

QListWidgetItem *ITEM = new QListWidgetItem();

QSize size = ITEM->sizeHint();

ITEM->setSizeHint(QSize(size.width(), 56));

ui.listWidget->addItem(ITEM);

widget->setSizeIncrement(size.width(), 56);

ui.listWidget->setItemWidget(ITEM, widget);

 

注意:ui.listWidget->addItem(ITEM); ui.listWidget->setItemWidget(ITEM, widget);

2行代码的顺序不能反,按正常逻辑感觉没有什么问题,但是qt内部代码处理过程这2个步骤是不能反过来的,qt源码如下:

 

void QListWidget::setItemWidget(QListWidgetItem *item, QWidget *widget)

{

Q_D(QListWidget);

/*

当addItem函数在该函数之后,这个时候因为并没有执行addItem,item没有和QListWidget建立绑定关联,所以这里得到的索引值index是不对的,所以会显示不出来,所以需要先执行addItem,

再执行该函数,index就能取到正确的值,就能正常显示了

*/

    QModelIndex index = d->listModel()->index(item);

    QAbstractItemView::setIndexWidget(index, widget);

}

 

注意事项:在使用自定义窗口作为Item时,如果需要响应对应的点击事件,可以重写mousePressedEvent事件,记得重写之后最好一定要加上类似QWidget::mousePressedEvent()函数,因为点击事件如果不加这行代码,qlistwidget是和对应的listwidgetitem关联的,listwidget适合自绘的itemwidget关联的,所以自绘的itemwidget的点击事件需要穿给对应的listwidgetitem,然后再有listwidgetitem传给qlistwidget就能显示正常,给关联是在对应的内核函数类似QWidget::mousePressedEvent进行往后传处理的,直到找到关联处理函数为止,所以需要加上。

 

 

发布了85 篇原创文章 · 获赞 18 · 访问量 12万+

猜你喜欢

转载自blog.csdn.net/a1317338022/article/details/105146250
今日推荐