0
votes

I have a QListWidget inside a QGraphicsScene. I add new items with QLineEdit inside. When QListWidget fills and scrollbars are active on the scroll, text does not scroll, current item representation does.

enter image description here

Complete code Git: code

EDIT:

I included QCheckBox inside a Horizontal layout to show why I need the setItemWidget function.

1
Qt 5.3 works well. BTW your code shows the first item at the bottom of list, not top.JustWe
yes that is the problem. I will test with different version than current 5.9.4pazduha
So what's the problem...as you mentioned text does not scroll isn't this? text & scroll both work well, what performance you except?JustWe
Yes, the problem is: If I move the bar the text stays in the same place.pazduha
@pazduha Your code has an error, to correct it changes in mainwindow.h change void MyGraphicsView::resizeEvent(QResizeEvent *event); to void resizeEvent(QResizeEvent *event);, even so I can confirm the bug in Qt 5.10.1eyllanesc

1 Answers

1
votes

Answer:

The text does not scroll because setItemWidget is:

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

Sets the widget to be displayed in the given item. This function should only be used to display static content in the place of a list widget item. If you want to display custom dynamic content or implement a custom editor widget, use QListView and subclass QItemDelegate instead.

it's nothing about QGraphicsScene.

Solution:

If you want to make the text editable. it's much simpler then you customize the QItemDelegate.

First, set the list widget with an edit trigger, tell the widget when to start editing.

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ...

    ui->listWidget->setEditTriggers(QAbstractItemView::DoubleClicked);
}

Then when you create & insert the QListWidgetItem, make sure each item is editable.

※Replace you whole on_pushButton_clicked function as below:

void MainWindow::on_pushButton_clicked()
{
    QListWidgetItem* item = new QListWidgetItem("name");
    item->setFlags(item->flags() | Qt::ItemIsEditable);
    ui->listWidget->insertItem(ui->listWidget->currentRow() + 1, item);
}