Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6636997
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T23:15:26+00:00 2026-05-25T23:15:26+00:00

I have a Qt application that contains a QTreeWidget. I want to replace one

  • 0

I have a Qt application that contains a QTreeWidget.
I want to replace one of the items.

I have a pointer:

QTreeWidgetItem *elementToSubstitute;

and a function that returns a QTreeWidgetItem*.

I want to overwrite the previous one with this new element, in the same place.
If I delete the previous element and insert the new one after that, the new item is placed at the bottom of the tree.

How should I proceed to have the new branch replace exactly the previous one?

EDIT for example


void MainWindow::addRoot(QString name ,QString description)
{
    QTreeWidgetItem *itm = new QTreeWidgetItem(ui->tree);
    itm->setText(0,name);
    itm->setText(1,description);

    QTreeWidgetItem *child1 = addChild(itm,"one","hello");
    QTreeWidgetItem *child2 = addChild(itm,"two","hello");
    QTreeWidgetItem *child3 = addChild(itm,"tree","hello");
    QTreeWidgetItem *child4 = addChild(itm,"four","hello");


    QTreeWidgetItem *parent = child2->parent(); 
    int itemIndex = parent-indexOfChid(child2); 
    parent->removeChild(child2); 
    parent->insertChild(itemIndex, child4 );

}

QTreeWidgetItem MainWindow::addChild(QTreeWidgetItem *parent,QString name ,QString description)
{
    QTreeWidgetItem *itm = new QTreeWidgetItem();
    itm->setFlags(Qt::ItemIsSelectable | Qt::ItemIsUserCheckable | Qt::ItemIsEnabled | Qt::ItemIsDragEnabled );
    itm->setText(0,name);
    itm->setText(1,description);

    addChild2(itm,"sub-one","subchild2");
    addChild2(itm,"sub-two","subchild2");
    addChild2(itm,"sub-tree","subchild2");

    parent->addChild(itm);
    return itm;

}

void MainWindow::addChild2(QTreeWidgetItem *parent,QString name ,QString description)
{
    QTreeWidgetItem *itm = new QTreeWidgetItem();

    itm->setFlags(Qt::ItemIsSelectable | Qt::ItemIsUserCheckable | Qt::ItemIsEnabled | Qt::ItemIsDragEnabled | Qt::ItemIsEditable);

    itm->setText(0,name);
    itm->setText(1,description);
    itm->setText(2,"test third coloumn");
    parent->addChild(itm);
}

…..

Now i have a pointer to one of the chiled. ( let’s sa of level 1) ans i want to replace it with another. for instance i want to replace child “2” with child “4” but i want this to be put in the place f child 2.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T23:15:27+00:00Added an answer on May 25, 2026 at 11:15 pm

    You could try doing something like this:

    QTreeWidgetItem *parent = elementToSubstitute->parent();
    int itemIndex = parent->indexOfChid(elementToSubstitute);
    parent->removeChild(elementToSubstitute);
    parent->insertChild(itemIndex, yourNewTreeItem);
    

    Note: if yourNewTreeItem is already in the tree, this will have no effect on its position, i.e. it will not duplicate a node.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have one application that contains sub-applications. I would like to segregate the GIN
Essentially what I want to do is have a root application.haml that contains the
I have an application that contains 4 mutable arrays,i already displays one array of
I currently have a single solution that contains both the one application developed so
I have an application that contains many controls on a panel, each with its
I have an application that contains a VC++ project (along with C# projects). Previously,
We have a windows application that contains an ActiveX WebBrowser control. As part of
I have a console application that contains quite a lot of threads. There are
I have a .NET application that contains a checkbox (System.Windows.Forms.Checkbox). This component (WindowsForms10.BUTTON.app.0.378734a1) is
I have this senario. We have an application server that contains a few web

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.