Looking for a sample example in which QtreeWidget at left side QSplitter and QStackedWidget on right side



  • I have a application as follows

    1. QtreeWIdget on Left Side
      2 Vertical Splitter on right side
    2. Qstacked Widget on right Side

    The output of the sample should

    https://ibb.co/hVRdnw

    and

    https://ibb.co/nh7Uub

    if someone can guide me this with a sample small application is to show only one page of stacked widget on one item clicked



  • #include <QWidget>
    #include <QTreeWidget>
    #include <QStackedWidget>
    #include <QSplitter>
    #include <QVBoxLayout>
    #include <QLabel>
    #include <QTableWidget>
    #include <QTextEdit>
    class ExampleWid : public QWidget{
        Q_OBJECT
        Q_DISABLE_COPY(ExampleWid)
    public:
        ExampleWid(QWidget* parent = Q_NULLPTR)
            : QWidget(parent)
        {
            QSplitter* mainSplit =new QSplitter(Qt::Horizontal,this);
            QVBoxLayout* mainLay=new QVBoxLayout(this);
            mainLay->addWidget(mainSplit);
            m_tree=new QTreeWidget(this);
            m_tree->setSelectionMode(QAbstractItemView::SingleSelection);
            mainSplit->addWidget(m_tree);
            m_stacked=new QStackedWidget(this);
            mainSplit->addWidget(m_stacked);
            connect(m_tree->selectionModel(),&QItemSelectionModel::currentChanged,this,&ExampleWid::pageChange);
            addTestData();
            m_tree->selectionModel()->setCurrentIndex(m_tree->model()->index(0,0),QItemSelectionModel::ClearAndSelect);
            m_tree->expandAll();
        }
    private:
        Q_SLOT void pageChange(const QModelIndex& idx){
            if(idx.isValid())
                m_stacked->setCurrentIndex(idx.data(Qt::UserRole).toInt());
        }
        void addTestData(){
            QAbstractItemModel* const mdl = m_tree->model();
            mdl->removeRows(0,mdl->rowCount());
            mdl->removeColumns(0,mdl->columnCount());
            mdl->insertRows(0,2);
            QModelIndex tempIdx = mdl->index(0,0);
            mdl->insertRow(0,tempIdx);
            tempIdx = mdl->index(1,0);
            mdl->insertRow(0,tempIdx);
            ////////////////////////////////////////
            tempIdx = mdl->index(0,0);
            mdl->setData(tempIdx,"Parent1");
            mdl->setData(tempIdx,m_stacked->addWidget(new QLabel("Parent1",this)),Qt::UserRole);
            ////////////////////////////////////////
            ///////////////////////////////////////////
            tempIdx = mdl->index(0,0,tempIdx);
            mdl->setData(tempIdx,"Child1");
            mdl->setData(tempIdx,m_stacked->addWidget(new QTextEdit("Child1",this)),Qt::UserRole);
            ////////////////////////////////////////
            ////////////////////////////////////////
            tempIdx = mdl->index(1,0);
            mdl->setData(tempIdx,"Parent2");
            mdl->setData(tempIdx,m_stacked->addWidget(new QLabel("Parent2",this)),Qt::UserRole);
            ////////////////////////////////////////
            ////////////////////////////////////////
            tempIdx = mdl->index(0,0,tempIdx);
            mdl->setData(tempIdx,"Child2");
            QTableWidget* tempTable = new QTableWidget(2,2,this);
            tempTable->model()->setData(tempTable->model()->index(0,0),"Child2 Cell 0,0");
            tempTable->model()->setData(tempTable->model()->index(1,0),"Child2 Cell 1,0");
            tempTable->model()->setData(tempTable->model()->index(0,1),"Child2 Cell 0,1");
            tempTable->model()->setData(tempTable->model()->index(1,1),"Child2 Cell 1,1");
            mdl->setData(tempIdx,m_stacked->addWidget(tempTable),Qt::UserRole);
            ////////////////////////////////////////
        }
        QTreeWidget* m_tree;
        QStackedWidget* m_stacked;
    };
    


  • One more question

    If I have a QMap in which I have

    QMap<QString,QWidget*> map;

    widget1 = new WidgetA();
    map["Parent"] = widget1;
    widget2 = new WidgetB();
    map["Parent2"] = widget2;

    I will using

    tempIdx = mdl->index(0,0);
    mdl->setData(tempIdx,"Parent1");
    mdl->setData(tempIdx,m_stacked->addWidget(widetA,Qt::UserRole);
    ////////////////////////////////////////
    ///////////////////////////////////////////
    tempIdx = mdl->index(0,0,tempIdx);
    mdl->setData(tempIdx,"Child1");
    mdl->setData(tempIdx,m_stacked->addWidget(new idgetB)),Qt::UserRole);

    how to create a textEdit when the class of QTextEdit and map as value as QWIdget ?



  • I'm not sure I understood correctly. what does

    how to create a textEdit when the class of QTextEdit and map as value as QWIdget ?

    mean?

    How would you determine the parent-child relation in the tree from a flat QMap?



  • QMap mymap<QString,QTextEdit>
    mymap["name"] = new QTextEdit;

    this shoudl work



  • Yes but how do you know if "name" is a parent or a child and a child of what?



  • Iif I have a tree like

    Parent1
    Child1
    ChildAofChild1
    Child2
    ChildAofChild1
    Parent2
    Child2
    ChildAofChild2
    Child2
    ChildAofChild2

    can u suggest the changes in addTestData



  • I still don't understand what's the link between the model and the QMap, sorry



  • now now it si not QMAp
    it is change in Tree now the tree is chaged

    the tree has to be like in https://ibb.co/ebS6QG . Please help



  • I tried the following code

        = mdl->index(0,0);
        mdl->setData(tempIdx,"Parent1");
        mdl->setData(tempIdx,m_stacked->addWidget(QLabel("A"),Qt::UserRole);
        ////////////////////////////////////////
        ///////////////////////////////////////////
        tempIdx = mdl->index(0,0,tempIdx);
        mdl->setData(tempIdx,"Child1");
        mdl->setData(tempIdx,m_stacked->addWidget(new QTextEdit("Child1",this)),Qt::UserRole);
    
        QModelIndex tempIdx = mdl->index(0,0,tempIdx);
        mdl->setData(tempIdx,"ChildOfChidl1");
    
        QTableWidget* tempTable1 = new QTableWidget(2,2,this);
        tempTable1->model()->setData(tempTable1->model()->index(0,0),"ChildOfChild1 Cell 0,0");
        tempTable1->model()->setData(tempTable1->model()->index(1,0),"ChildOfChild1 Cell" ,0");
        mdl->setData(tempIdx,m_stacked->addWidget(tempTable1),Qt::UserRole);


  • You are definitely on the right path. The setting of data is correct, you just forgot to insert a line in the tree before writing to it. Like I did in:

     QModelIndex tempIdx = mdl->index(0,0);
    mdl->insertRow(0,tempIdx);
    


  • One more question regarding the sequnce of displayed children

       QModelIndex tempIdx  = mdl->index(0,0);
        mdl->setData(tempIdx,"Parent1");
        mdl->setData(tempIdx,m_stacked->addWidget(QLabel("A"),Qt::UserRole);
        ////////////////////////////////////////
        ///////////////////////////////////////////
        tempIdx = mdl->index(0,0,tempIdx);
        mdl->setData(tempIdx,"Child1");
        mdl->setData(tempIdx,m_stacked->addWidget(new QTextEdit("Child1",this)),Qt::UserRole);
    
    
        mdl->insertRow(0,tempIdx);
        QModelIndex tempIdx = mdl->index(0,0,tempIdx);
        mdl->setData(tempIdx,"ChildOfChidl1");
        QTableWidget* tempTable1 = new QTableWidget(2,2,this);
        tempTable1->model()->setData(tempTable1->model()->index(0,0),"ChildOfChild1 Cell 0,0");
        tempTable1->model()->setData(tempTable1->model()->index(1,0),"ChildOfChild1 Cell" ,0");
        mdl->setData(tempIdx,m_stacked->addWidget(tempTable1),Qt::UserRole);
    
        mdl->insertRow(0,tempIdx);
        QModelIndex tempIdx = mdl->index(0,0,tempIdx);
        mdl->setData(tempIdx,"ChildAOfChidl1");
        QTableWidget* tempTable1 = new QTableWidget(2,2,this);
        tempTable1->model()->setData(tempTable1->model()->index(0,0),"ChildOfChild1 Cell 0,0");
        tempTable1->model()->setData(tempTable1->model()->index(1,0),"ChildOfChild1 Cell" ,0");
        mdl->setData(tempIdx,m_stacked->addWidget(tempTable1),Qt::UserRole);
    
       mdl->insertRow(0,tempIdx);
        QModelIndex tempIdx = mdl->index(0,0,tempIdx);
        mdl->setData(tempIdx,"ChildBOfChidl1");
        QTableWidget* tempTable1 = new QTableWidget(2,2,this);
        tempTable1->model()->setData(tempTable1->model()->index(0,0),"ChildBOfChild1 Cell 0,0");
        tempTable1->model()->setData(tempTable1->model()->index(1,0),"ChildBOfChild1 Cell" ,0");
        mdl->setData(tempIdx,m_stacked->addWidget(tempTable1),Qt::UserRole);
    

    In my case sequence is a issue ChildBOfChild1 is displayed before ChildAOfChild1 why ?


Log in to reply
 

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