Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • 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.
Forum Updated to NodeBB v4.3 + New Features

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 8.7k 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 p3c0
    #1

    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.
    and i found code like this...

    void TreeEdit::addNode()  
    {  
        QTreeWidgetItem *curItem=m_tree->currentItem();  
        QTreeWidgetItem *newItem=new QTreeWidgetItem(curItem);  
        curItem->addChild(newItem);  
        QLineEdit *lineEdit=new QLineEdit(this);  
        connect(lineEdit,SIGNAL(editingFinished()),this,SLOT(finishEdit()));  
        m_tree->setItemWidget(newItem,0,lineEdit);  
        m_tree->expandItem(curItem);  
        m_tree->setCurrentItem(newItem);  
        lineEdit->setText("default");  
        lineEdit->setSelection(0,7);  
    }  
    
    void TreeEdit::finishEdit()  
    {  
        QTreeWidgetItem *curItem=m_tree->currentItem();  
        QLineEdit * edit = qobject_cast< QLineEdit * >(m_tree->itemWidget(curItem,0));  
        if(!edit)  
            return;  
        QString text=edit->text();  
        m_tree->removeItemWidget(curItem,0);  
        curItem->setText(0,text);  
    }
    

    however, when i do some custom things in finishEdit(), which i do emit some signals, which make the app crash in app.exec(); which is really wired......

    so i want to ask is there any other way to do this ?
    and i would like to have a try.

    1 Reply Last reply
    0
    • O Offline
      O Offline
      opengpu2
      wrote on last edited by
      #2
      This post is deleted!
      1 Reply Last reply
      0
      • A Offline
        A Offline
        alex_malyu
        wrote on last edited by
        #3

        Your code should not even compile.

        1. QTreeWidgetItem *curItem=m_tree->currentItem();
          QLineEdit edit=qobject_cast<QLineEdit>(m_tree->itemWidget(curItem,0));
          if(!edit)
          return;
          You are trying to cast pointer to class object.

        2. There is no any need to create lineEdit and set it with setItemWidget
          newItem->setText(0, tr("Cities"));

        Read documentation on QTreeWidgetItem, also it might be helpful to create and populate QTreeWidget in the designer and then check code it generates.

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

          u don't get what i want to do ......

          A 1 Reply Last reply
          0
          • M Offline
            M Offline
            mcosta
            wrote on last edited by
            #5

            Hi,

            I didn't tried your code but you can get what you want creating a custom delegate for your Tree.
            In that case you can re-implement createEditor() to return a QLineEdit and setModelData() to handle the text inserted

            Once your problem is solved don't forget to:

            • Mark the thread as SOLVED using the Topic Tool menu
            • Vote up the answer(s) that helped you to solve the issue

            You can embed images using (http://imgur.com/) or (http://postimage.org/)

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

              what should i do when i want to do many things include emit signals when editFinishing?

              1 Reply Last reply
              0
              • M Offline
                M Offline
                mcosta
                wrote on last edited by
                #7

                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

                Once your problem is solved don't forget to:

                • Mark the thread as SOLVED using the Topic Tool menu
                • Vote up the answer(s) that helped you to solve the issue

                You can embed images using (http://imgur.com/) or (http://postimage.org/)

                O 3 Replies Last reply
                0
                • O opengpu2

                  u don't get what i want to do ......

                  A Offline
                  A Offline
                  alex_malyu
                  wrote on last edited by alex_malyu
                  #8

                  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 1 Reply Last reply
                  0
                  • 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 Extensions
                                        • Unsolved