0

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.

pazduha
  • 147
  • 3
  • 17
  • 1
    Qt 5.3 works well. BTW your code shows the first item at the bottom of list, not top. – JustWe May 15 '18 at 09:26
  • yes that is the problem. I will test with different version than current 5.9.4 – pazduha May 15 '18 at 09:29
  • 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 May 15 '18 at 09:33
  • Yes, the problem is: If I move the bar the text stays in the same place. – pazduha May 15 '18 at 09:35
  • Test it like this. Press PushButton till u get lots of items and a scrollbar. Name the first item something. Move the scrollbar. – pazduha May 15 '18 at 09:37
  • 1
    @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.1 – eyllanesc May 15 '18 at 09:45

1 Answers1

1

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);
}
JustWe
  • 4,250
  • 3
  • 39
  • 90
  • This a solution for this particular problem. The problem is I need to add more to the horizontal layout as shown in the edited code (GIT, edit above). I will look into QListView and QItemDelegate. – pazduha May 16 '18 at 07:23
  • @pazduha For that case, you need to _use QListView and subclass QItemDelegate instead_ – JustWe May 16 '18 at 07:31
  • @pazduha If you have multi columns use QTableView https://stackoverflow.com/questions/16660292/qt-using-custom-qitemdelegate-for-qtableview – JustWe May 16 '18 at 08:03
  • And if you just want them to display as a list why not simply use QLayout with the scroll bar? – JustWe May 16 '18 at 08:07
  • In my project I use dragDropMode - InternalMove. I want to keep using this, thats why QListView is needed. QTableView would probably not be ok for drag/drop. – pazduha May 16 '18 at 08:21