Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. create a new QTreeWidgetItem in QTreeWidget, edit its name in QTreeWidgetItem, if the name is legal, create the new QTreeWidgetItem, if not, remove it.
Qt 6.12 LTS is out! See what's new in the release blog

create a new QTreeWidgetItem in QTreeWidget, edit its name in QTreeWidgetItem, if the name is legal, create the new QTreeWidgetItem, if not, remove it.

Scheduled Pinned Locked Moved General and Desktop
19 Posts 4 Posters 9.6k Views 3 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • O Offline
    O Offline
    opengpu2
    wrote on last edited by
    #9

    thank you for all !

    1 Reply Last reply
    0
    • A alex_malyu

      If you need control over editor widget you may use custom model/view delegate.
      That allows you to intercept editor widget creation.
      That what people above tried to tell you.
      You can search for using delegates on your own,
      But mostly likely you do not need it.
      QTreeWidget provides sufficient notifications..
      In you case connecting to QTreeWidget::itemChanged signal may be sufficient.
      Bu who knows if you do not tell us .

      O Offline
      O Offline
      opengpu2
      wrote on last edited by
      #10

      @alex_malyu
      itemChanged?

      i want to create a new QTreeWidgetItem in QTreeWidget, edit its name in QTreeWidgetItem, if the name is legal, create the new QTreeWidgetItem, if not, remove it.
      so that's why i tried sth like finishEdit() as said above...

      1 Reply Last reply
      0
      • O Offline
        O Offline
        opengpu2
        wrote on last edited by opengpu2
        #11

        i create a delegate and set it to QTreeWidget, but how to edit the item?
        i tried paint(), it go into it,
        but for createEditor and setModelData...it does not go into them...
        i tried setEditTriggers()....and setFlags(Qt::ItemIsEditable)....both not work...

        but openPersistentEditor does work! but when should i call closePersistentEditor?

        which one should i use?

        1 Reply Last reply
        0
        • O Offline
          O Offline
          opengpu2
          wrote on last edited by opengpu2
          #12

          is this a good method?

              QWidget *SpinBoxDelegate::createEditor(QWidget *parent,  
                  const QStyleOptionViewItem &/* option */,  
                  const QModelIndex &/* index */) const  
              {  
                  QSpinBox *editor = new QSpinBox(parent);  
                  editor->setMinimum(0);  
                  editor->setMaximum(100);  
                
                  return editor;  
              }  
              void SpinBoxDelegate::setEditorData(QWidget *editor,  
                                                  const QModelIndex &index) const  
              {  
                  int value = index.model()->data(index, Qt::EditRole).toInt();  
                
                  QSpinBox *spinBox = static_cast<QSpinBox*>(editor);  
                  spinBox->setValue(value);  
              }  
              void SpinBoxDelegate::setModelData(QWidget *editor, QAbstractItemModel *model,  
                                                 const QModelIndex &index) const  
              {  
                  QSpinBox *spinBox = static_cast<QSpinBox*>(editor);  
                  spinBox->interpretText();  
                  int value = spinBox->value();  
                
                  model->setData(index, value, Qt::EditRole);  
              }  
              void SpinBoxDelegate::updateEditorGeometry(QWidget *editor,  
                  const QStyleOptionViewItem &option, const QModelIndex &/* index */) const  
              {  
                  editor->setGeometry(option.rect);  
              }  
          
              QObject::connect(treeWidget,SIGNAL(itemDoubleClicked ( QTreeWidgetItem*, int)),  
                      this,SLOT(openPersistentEditor(QTreeWidgetItem*,int)));  
                
              QObject::connect(treeWidget,SIGNAL(itemSelectionChanged ()),  
                      this,SLOT(slotSelectionChanged()));  
                
              void CMainWindow::openPersistentEditor(QTreeWidgetItem* item,int column)  
              {  
                  if( 1 == column  )  
                  {  
                      treeWidget->openPersistentEditor(item,column);  
                      lastOpen = item;  
                  }  
              }  
              void CMainWindow::slotSelectionChanged()  
              {  
                  if( NULL!= lastOpen )  
                  {  
                      treeWidget->closePersistentEditor(lastOpen,1);  
                      lastOpen = NULL;  
                  }  
              }  
          
          1 Reply Last reply
          0
          • O Offline
            O Offline
            opengpu2
            wrote on last edited by
            #13

            another wired problem....

            connect(lineEdit, SIGNAL(editingFinished()), this, SLOT(commitAndCloseEditor()));
            
            void Class::commitAndCloseEditor()
            {
            QLineEdit* p = qobject_cast<QLineEdit*>(sender());
            //emit commitData(p);  //is this duplicated? i found setModelData is called without this emit
            emit closeEditor(p);  //not work......
            
            (CMainWindow*)parent()->slotSelectionChanged();  //succeed to close the editor...
            }
            
            1 Reply Last reply
            0
            • M mcosta

              Hi,

              the setModelData is called when the editing is finished; so you can check the inserted text in that method.
              In example in our program we delete automatically the Item if the user insert/change an empty text

              void setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index ) const
              {
                  QLineEdit *le = qobject_cast<QLineEdit*>(editor);
                  if (le && le->text().isEmpty()) {
                      model->removeRow(index.row());
                      return;
                  }
                  QStyledItemDelegate::setModelData(editor, model, index);
              }
              

              you could do something similar

              O Offline
              O Offline
              opengpu2
              wrote on last edited by
              #14

              @mcosta
              model->removeRow(index.row());
              i use this code....but remove failed...and it reture false.....
              why?

              1 Reply Last reply
              0
              • M mcosta

                Hi,

                the setModelData is called when the editing is finished; so you can check the inserted text in that method.
                In example in our program we delete automatically the Item if the user insert/change an empty text

                void setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index ) const
                {
                    QLineEdit *le = qobject_cast<QLineEdit*>(editor);
                    if (le && le->text().isEmpty()) {
                        model->removeRow(index.row());
                        return;
                    }
                    QStyledItemDelegate::setModelData(editor, model, index);
                }
                

                you could do something similar

                O Offline
                O Offline
                opengpu2
                wrote on last edited by opengpu2
                #15

                and how can i delete a QTreeWidgetItem ? just call delete pQTreeWidgetItem ?

                1 Reply Last reply
                0
                • M mcosta

                  Hi,

                  the setModelData is called when the editing is finished; so you can check the inserted text in that method.
                  In example in our program we delete automatically the Item if the user insert/change an empty text

                  void setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index ) const
                  {
                      QLineEdit *le = qobject_cast<QLineEdit*>(editor);
                      if (le && le->text().isEmpty()) {
                          model->removeRow(index.row());
                          return;
                      }
                      QStyledItemDelegate::setModelData(editor, model, index);
                  }
                  

                  you could do something similar

                  O Offline
                  O Offline
                  opengpu2
                  wrote on last edited by opengpu2
                  #16

                  @mcosta
                  hi do u know how to keep the focus and editing state on the editor(that is QLineEidt here), until the text user input is allowed?
                  i want to pop out meesagebox, also when user click the other item...after that i want the focus and editing state is still on the QLineEdit...

                  i tried to set the QLineEdit::setFocus....but after i do this, it always call setModelData again............

                  1 Reply Last reply
                  0
                  • SGaistS Offline
                    SGaistS Offline
                    SGaist
                    Lifetime Qt Champion
                    wrote on last edited by
                    #17

                    Hi,

                    You would probably need to have a look at the focusOut event for that matter

                    Interested in AI ? www.idiap.ch
                    Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                    1 Reply Last reply
                    0
                    • O Offline
                      O Offline
                      opengpu2
                      wrote on last edited by
                      #18

                      now i use a bool to avoid setModelData implemeted multi-times.
                      and is there any example that show what i should do in this situation?

                      1 Reply Last reply
                      0
                      • SGaistS Offline
                        SGaistS Offline
                        SGaist
                        Lifetime Qt Champion
                        wrote on last edited by
                        #19

                        There's no example because when a user input is mandatory, you generally use something like a QDialog than lock the rest of the application

                        Interested in AI ? www.idiap.ch
                        Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                        1 Reply Last reply
                        0

                        • Login

                        • Login or register to search.
                        • First post
                          Last post
                        0
                        • Categories
                        • Recent
                        • Tags
                        • Popular
                        • Users
                        • Groups
                        • Search
                        • Get Qt
                        • Unsolved