qt QListWidget custom Item insertion function notes

Regarding the problem of removing the dotted frame of item in QListWidget,

There is a way to achieve it through the delegate, there is a way to find more, find the best and most suitable,

There is also about the height and size of each item in the actual listwidget, seeing there are several ways to achieve it, which one is best

 

QListWidget uses a custom Item. When inserting and deleting, there will be uncertainty about the position of the insert, because the creation process and the binding process require attention.

// Use widget as list item

// This means that the parent window is not specified and there is no particularly big relationship, which can be displayed

// If you need the corresponding parent window to handle some events of this window, you can specify, the operation can be more convenient

/*

// qt source code, the change function is skipped from the setItemWidget call interface

// There is a line of key code w idget- > setParent ( viewport ());

// So it is useless to set the parent window, so generally it is not necessary to set,

// If you need to associate with the change window, you can connect some associated interfaces.

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);

/*

// Do not specify the parent window here, the official website says it will cause item order uncertainty

// 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);

The order of the two lines of code cannot be reversed. According to normal logic, there is no problem, but the two steps of the internal code processing process of qt cannot be reversed. The source code of qt is as follows:

 

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

{

Q_D(QListWidget);

/*

When the addItem function is after this function, because addItem is not executed at this time, the item is not bound to the QListWidget, so the index value obtained here is wrong, so it will not be displayed, so you need to execute addItem first.

Then execute the function, the index can get the correct value, and it can be displayed normally

*/

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

    QAbstractItemView::setIndexWidget(index, widget);

}

 

Note: When using a custom window as an Item, if you need to respond to the corresponding click event, you can rewrite the mousePressedEvent event. Remember to add a function like QWidget :: mousePressedEvent () after rewriting, because if the click event is not Add this line of code, qlistwidget is associated with the corresponding listwidgetitem, listwidget is suitable for self-painted itemwidget, so the click event of self-drawn itemwidget needs to be worn to the corresponding listwidgetitem, and then there is listwidgetitem passed to qlistwidget to display normal For the association, the corresponding kernel function is similar to QWidget :: mousePressedEvent, which is processed backwards until the association processing function is found, so it needs to be added.

 

 

Published 85 original articles · praised 18 · 120,000 views

Guess you like

Origin blog.csdn.net/a1317338022/article/details/105146250