how to add children to the QTree Widget



  • I am trying to add children to the QTree widget . but i am facing some problems.

    My code:

    // Create new item (top level item)
    QTreeWidgetItem *topLevelItem = new QTreeWidgetItem(ui->treeWidget);

    // Set text for item
    topLevelItem->setText(0,"Item");
    
    // Add it on our tree as the top item.
    ui->treeWidget->addTopLevelItem(topLevelItem);
    
    
    // Create new item and add as child item
    

    QTreeWidgetItem *item=new QTreeWidgetItem(topLevelItem);
    // Set text for item
    //item->setText(0,"SubItem");

    QList<QString>m_Data;
    
    for(int i=1;i<3;i++)
    {
        QString Data = QString("SubItem"+QString::number(i));
        m_Data.append(Data);
    }
    
    item->addChildren(m_Data);

  • Qt Champions 2017

    Hi
    You are not really telling what kind of problems :)
    but this tutorial explain usage pretty well in my opinion

    https://www.bogotobogo.com/Qt/Qt5_QTreeWidget.php

    The main idea is either to add as a top level item or
    add to other parent to build a tree like structure.

    void Dialog::addTreeRoot(QString name, QString description)
    {
        // QTreeWidgetItem(QTreeWidget * parent, int type = Type)
        QTreeWidgetItem *treeItem = new QTreeWidgetItem(ui->treeWidget);
    
        // QTreeWidgetItem::setText(int column, const QString & text)
        treeItem->setText(0, name);
        treeItem->setText(1, description);
        addTreeChild(treeItem, name + "A", "Child_first");
        addTreeChild(treeItem, name + "B", "Child_second");
    }
    
    void Dialog::addTreeChild(QTreeWidgetItem *parent,
                      QString name, QString description)
    {
        // QTreeWidgetItem(QTreeWidget * parent, int type = Type)
        QTreeWidgetItem *treeItem = new QTreeWidgetItem();
    
        // QTreeWidgetItem::setText(int column, const QString & text)
        treeItem->setText(0, name);
        treeItem->setText(1, description);
    
        // QTreeWidgetItem::addChild(QTreeWidgetItem * child)
        parent->addChild(treeItem);
    }
    


  • @mrjj
    Actually i want to add n number of children and child to that n number of children thats my question. But using the code.

    Example:
    I want to store the data in a Qlist and insert it in Qtree widget as children using the "addchildren"member function


  • Qt Champions 2017

    @ManiRon
    Ok, but there nothing magic to it.
    If you add it like
    QTreeWidgetItem *treeItem = new QTreeWidgetItem(ui->treeWidget);

    its a top level item
    if you use that item as parent, like

    QTreeWidgetItem *treeItem2 = new QTreeWidgetItem();
    treeItem ->addChild(treeItem2 );

    it becomes a child of that parent. and so on.

    so you simply use the last item as parent for next if u want it to go

    aaa
      bbbb
        cccc 
    


  • @mrjj ok but if i want to add n number of children under top level item how should i do that.

    And i have another doubt .
    I am trying to see the data which i have inserted into my QList . I can see the size but i cannot see the data.

    Code:

    QList<QString> m_Data;
    for(int i=1;i<3;i++)
    {
    Data = QString("SubItem"+QString::number(i));
    m_Data.append(Data.toLatin1().constData());
    QString H = m_Data.at(i); //trying to see data
    qDebug()<<"Data"<< m_Data.at(i);
    }



  • @mrjj

    Sir i have a doubt


  • Qt Champions 2017

    @ManiRon
    Hi
    With ?



  • @mrjj

    pro file



  • @mrjj

    Sir i am trying to add

    uinx{
    LIBS+= -L$$PWD/.../lib/linus/-lData
    LIBS+= -L$$PWD/.../lib/Xenomai/-lData1 -lrtdm -lrt -lxenomai
    }
    win32
    {
    LIBS += path
    }

    I am adding the linux and xenomai library and trying to compile . but it throws error. As it detects the linux library and it couldnt find the xenomai library it throws error.
    My question is i want to define all my library and based on the platform or OS i am running it should detect its particular library.
    For example : linux, xenomai, windiows. These are the platforms i am running the application. Why i ask this is because every time i have to add the corresponding library and compile it and the run the application . So is there any solution for this?



  • @mrjj

    One of the person from this forum told me to use "unix" and "win32". But i asked for linux,xenomai what should i define, he told to use "unix" itself. But i have separate library for linux and xenomai. but i want to add both is there any way?


  • Qt Champions 2017

    @ManiRon

    Hi it seems ok what you did
    except you missspelled unix (uinx)
    and it sometimes a bit funny with scopes so make sure t to
    unix {
    }
    and not
    unix
    {
    }
    http://doc.qt.io/qt-5/qmake-advanced-usage.html
    http://doc.qt.io/qt-5/third-party-libraries.html



  • @mrjj

    Ok Sir,

    But my doubt is i am able to add the library.
    But i am adding another library that supports xenomai. while running the application in linux OS it goes to the unix{
    LIBS+= -L$$PWD/.../lib/linus/-lData
    LIBS+= -L$$PWD/.../lib/Xenomai/-lData1 -lrtdm -lrt -lxenomai
    }
    part and searches for the library which is my problem, I want the application to detect the library based on the platform i run. As one person from Qt forum said unix is common for xenomai, linux. So is there any possibility i can make my application to run that particular library based on OS platform?


  • Qt Champions 2017

    @ManiRon
    But is unix not linux for you ?
    You have a unix build and a linux build?

    I would assume you can do it right with

    
    win32 {
    ..
    }
    
    unix {
      ..
    }
    


  • @mrjj

    its linux sir,

    but for xenomai also it is unix i think so
    unix{
    LIBS+= -L$$PWD/.../lib/linus/-lData
    LIBS+= -L$$PWD/.../lib/Xenomai/-lData1 -lrtdm -lrt -lxenomai
    }

    this is my problem sir,

    I am want to run library if its linux i want linux library and if its xenomai i want xenomai library to run



  • @mrjj

    ya i did this but for windows it works fine, and in unix if i define only one library that the platform i am running it works fine.

    but every time i want to add that particular library compile and run the application


  • Qt Champions 2017

    Hi
    Now i understand.
    For qmake both plain linux and xenomai will be the same
    as far as i know. (unix)

    you can call .sh script and things like that from the pro file
    so maybe you can detect your self if linux or xenomai ?
    http://doc.qt.io/archives/qt-4.8/qmake-function-reference.html#system-command

    Check what

    UNAME = $$system(uname -s)
    contains( UNAME, [lL]inux ):message( This looks like Linux ($$UNAME) to me )
    

    will output

    also did u try what been suggested in
    https://forum.qt.io/topic/94393/how-to-add-macro-for-the-libs-in-the-pro-file/9



  • @mrjj said in how to add children to the QTree Widget:

    UNAME = $$system(uname -s)
    contains( UNAME, [lL]inux ):message( This looks like Linux ($$UNAME) to me )

    So thsi is for linux?
    UNAME = $$system(uname -s)
    contains( UNAME, [lL]inux ):message( This looks like Linux ($$UNAME) to me )

    If i want to check for xenomai what should i do sir?



  • @mrjj

    Is there any way sir?


  • Qt Champions 2017

    @ManiRon
    Hi
    Yes you can do it.
    But you need to find out how. I dont have xenomai
    installed so i cant say how we can spot the difference between plain linux and
    xenomai
    so try
    UNAME = $$system(uname -s)
    on both systems and see what it types.
    If different, you can use that as a flag



  • @mrjj

    contains( UNAME, [lL]inux ):message( This looks like Linux ($$UNAME) to me )

    This message where it will be printed sir?



  • @ManiRon

    And i have another doubt ?


  • Qt Champions 2017

    @ManiRon
    yes message print out the variable name $$UNAME ( what it contains)
    what is other doubt?



  • @mrjj ok where i can see this message sir?


  • Qt Champions 2017

    @ManiRon
    in the Creator output pane.
    below where code is.
    alt text



  • @mrjj my anogther doubt is

    I am trying to view the data in a list . i have specified already



  • @mrjj

    And i have another doubt .
    I am trying to see the data which i have inserted into my QList . I can see the size but i cannot see the data.

    Code:

    QList<QString> m_Data;
    for(int i=1;i<3;i++)
    {
    Data = QString("SubItem"+QString::number(i));
    m_Data.append(Data.toLatin1().constData());
    QString H = m_Data.at(i); //trying to see data
    qDebug()<<"Data"<< m_Data.at(i);
    }

    while running the application crashesdonno why


  • Qt Champions 2017

    @ManiRon
    Hi
    what you mean u can see the size but not the data?

    qDebug()<<"Data"<< m_Data.at(i); <<< u print index from 1,2,3
    but u call append so first call, there is data in zero
    but u print 1
    then next loop
    you print from 2, but only data in 0,1
    and so on.
    it starts in zero. so adjust the for loop.

    for(int i=0;i<2;i++)
    {
    Data = QString("SubItem"+QString::number(i+1));
    m_Data.append(Data.toLatin1().constData());
    qDebug()<<"Data"<< m_Data.at(i);  //trying to see data
    }
    


  • @mrjj said in how to add children to the QTree Widget:

    but

    if i give

    int i = m_data.size();
    and print the i value , I can see the data size that which i have inserted in the list.

    But if i try to see the data

    QString H = m_data.at(i);
    and print the H value using qDebug i data is not coming and the application crashes


  • Qt Champions 2017

    @ManiRon
    Yes, you access to high index.
    as u start with 1.
    and list start with zero.
    So that can crash.



  • This post is deleted!


  • @mrjj but my size is two. I have inserted two data and i am trying to see the second data then it should work right?



  • @ManiRon OK SIR IT WORKED



  • @mrjj

    I have another doubt sir


  • Qt Champions 2017

    @ManiRon
    super :)
    ask away :)



  • @mrjj 0_1536571284463_1.jpg

    How to use this ? for this only i am trying



  • @mrjj What is my doubt is i want to add N number of children to a toplevelitem and one child to each child ? How can i add N Number of childern first


  • Qt Champions 2017

    @ManiRon
    Its a list of QTreeWidgetItems * ( pointers)
    So you must add them to a QList then give QList to tree.
    Its just a function to add multiple items at once.

    
    // top one
        QTreeWidgetItem *treeItemROOT = new QTreeWidgetItem(ui->treeWidget);
        treeItem->setText(0, name);
        treeItem->setText(1, description);
    
    // first child
        QTreeWidgetItem *treeItem = new QTreeWidgetItem();
        treeItem->setText(0, name);
        treeItem->setText(1, description);
    
        treeItemROOT ->addChild(treeItem); // add child to the root
    


  • @mrjj said in how to add children to the QTree Widget:

    treeItem

    thats what can u give any example code for that "Its a list of QTreeWidgetItems * ( pointers)
    So you must add them to a QList then give QList to tree."


  • Qt Champions 2017

    @ManiRon
    QList<QTreeWidgetItem> mylist;
    mylist.append( new QTreeWidgetItem); // might want to set some text also
    ..
    ...
    call addChildren with mylist.

    However, it wont help you build the tree, just other way of adding more than one item at a time.



  • @mrjj

    My idea was to insert a list of data as a children to a toplevelitem


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.