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進行往後傳處理的,直到找到關聯處理函數爲止,所以需要加上。

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章