1

I have a QTreeWidget and two buttons "+" and "-". When I press "+" I want to add new item to QTreeWidget and I want that item to be in edit mode. I managed to do that with following code (it gets called every time "+" is pressed):

// QTreeWidgetItem* lastItem = getLastItem();
// if (lastItem) { widget->closePersistentEditor(lastItem); }

QTreeWidgetItem* item = new QTreeWidgetItem(widget, {"100000"});
item->setFlags(item->flags() | Qt::ItemIsEditable);
widget->addTopLevelItem(item);
widget->editItem(item);

Problem is when I try to add a new item, but don't exit edit mode before adding (press Enter or something). I get error edit: editing failed and new item is added below current item (which is still in edit mode).

What I would like is that current item exists edit mode and that newly added item becomes focused and enters edit mode.

I tried to do that with first getting the last item in a QTreeWidget and calling closePersistentEditor(lastItem) (commented code) and then creating and adding new item, but it didn't work. So, how to close currently opened edit on item?

EDIT: Ok, I have added additional code with minimal example. Only thing you have to do to build it is to add QTreeWidget and QPushButton to the form mainwindow.ui and connect that button to on_btnAdd_clicked():

#include "mainwindow.h"
#include "ui_mainwindow.h"

#include <QTreeWidget>
#include <QTreeWidgetItem>

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::on_btnAdd_clicked()
{
    QTreeWidgetItem* item = new QTreeWidgetItem(ui->treeWidget, {"100000"});
    item->setFlags(item->flags() | Qt::ItemIsEditable);
    ui->treeWidget->addTopLevelItem(item);
    ui->treeWidget->editItem(item);
}

EDIT2: This is happening on macOS (Mojave) with Qt 5.12.

scopchanov
  • 7,966
  • 10
  • 40
  • 68
dosvarog
  • 694
  • 1
  • 6
  • 20
  • 1
    `QTreeWidget` does not have a `getLastItem` method. – scopchanov Oct 05 '20 at 18:37
  • @user6528273, i suppose `getLastItem` is the OP-s own method. @dosvarog, can you share some more details about your code (maybe even a reproducible code snippet which demonstrates the issue)? Do you use a model behind the view? And if yes, do you add the new item via the model? – zgyarmati Oct 06 '20 at 09:18
  • @user6528273 Yes, that is my own method. – dosvarog Oct 07 '20 at 16:07
  • @zgyarmati What I have provided *is* minimal example. But I edited the question and added whole mainwindow.cpp. No, I use QTreeWidget, not QTreeView. No, I add new item as it is shown in the code. – dosvarog Oct 07 '20 at 16:14
  • What you have provided was _not_ a minimal, reproducible example, neither is the code you have added with the edit. For me it works as expected, i.e. each time I hit `Add`, a new item is added and an editor is open for it. If a previous item is being edited, its editor is resp. closed. – scopchanov Oct 07 '20 at 16:33
  • So you want me to provide the whole project? .h, .cpp, .ui, .pro and all other files? Those 4 lines in `on_btnAdd_clicked` are the most important and only 4 lines that do something. I guess our opinions on what is minimal example are not same. When I hit Add, new item is added, editor is opened, yes, that is true. But when I hit Add again (without closing editor), new item is added below first one, but first one is still being edited. – dosvarog Oct 07 '20 at 16:44
  • @user6528273 This is happening on macOS with Qt 5.12, it never hit me that on other OS-es behaviour can be normal. I just tried at home on Ubuntu, and it is working as intended – dosvarog Oct 07 '20 at 16:49
  • 1
    _this is happening on macOS with Qt 5.12_ see - without enough details, looking for a solution is like shooting in a dark room. Anyway, now it makes sense and I have retracted my close vote. So, it looks like either it was a bug, which has been already addressed, or a Mac specific issue. I am on Win 10 with Qt 5.15.1, so I can't help further. – scopchanov Oct 07 '20 at 16:59
  • 1
    It seems it is a bug in Qt, now I found some bug report with similar issue: https://bugreports.qt.io/browse/QTBUG-26838 . I didn't know that default behaviour is the one I am looking for, so it never occurred to me that it could be a bug. Thanks anyway! – dosvarog Oct 07 '20 at 18:26
  • This bug affects much older versions of Qt on windows 7. I suggest you to report it, stating the version and the OS. – scopchanov Oct 08 '20 at 05:18

1 Answers1

0

Ok, it looks like this is a bug in Qt for macOS. Workaround that I did is following:

QTreeWidgetItem* lastItem = getLastTreeWidgetItem(widget);
if (lastItem) {
    widget->setDisabled(true);
    widget->setDisabled(false);
}
conversation->setFlags(conversation->flags() | Qt::ItemIsEditable);

getLastTreeWidget() is my own method that returns last added item in a QTreeWidget. Now every time when I press button to add new item, previous gets deselected and newly added enters edit mode.

dosvarog
  • 694
  • 1
  • 6
  • 20