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. Looking for a sample example in which QtreeWidget at left side QSplitter and QStackedWidget on right side
QtWS25 Last Chance

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

Scheduled Pinned Locked Moved Unsolved General and Desktop
12 Posts 2 Posters 2.4k Views
  • 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.
  • Q Offline
    Q Offline
    Qt Enthusiast
    wrote on last edited by
    #1

    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

    1 Reply Last reply
    0
    • VRoninV Offline
      VRoninV Offline
      VRonin
      wrote on last edited by
      #2
      #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;
      };
      

      "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
      ~Napoleon Bonaparte

      On a crusade to banish setIndexWidget() from the holy land of Qt

      1 Reply Last reply
      3
      • Q Offline
        Q Offline
        Qt Enthusiast
        wrote on last edited by
        #3

        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 ?

        1 Reply Last reply
        0
        • VRoninV Offline
          VRoninV Offline
          VRonin
          wrote on last edited by
          #4

          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?

          "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
          ~Napoleon Bonaparte

          On a crusade to banish setIndexWidget() from the holy land of Qt

          1 Reply Last reply
          0
          • Q Offline
            Q Offline
            Qt Enthusiast
            wrote on last edited by
            #5

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

            this shoudl work

            1 Reply Last reply
            0
            • VRoninV Offline
              VRoninV Offline
              VRonin
              wrote on last edited by
              #6

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

              "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
              ~Napoleon Bonaparte

              On a crusade to banish setIndexWidget() from the holy land of Qt

              1 Reply Last reply
              0
              • Q Offline
                Q Offline
                Qt Enthusiast
                wrote on last edited by
                #7

                Iif I have a tree like

                Parent1
                Child1
                ChildAofChild1
                Child2
                ChildAofChild1
                Parent2
                Child2
                ChildAofChild2
                Child2
                ChildAofChild2

                can u suggest the changes in addTestData

                1 Reply Last reply
                0
                • VRoninV Offline
                  VRoninV Offline
                  VRonin
                  wrote on last edited by
                  #8

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

                  "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
                  ~Napoleon Bonaparte

                  On a crusade to banish setIndexWidget() from the holy land of Qt

                  1 Reply Last reply
                  0
                  • Q Offline
                    Q Offline
                    Qt Enthusiast
                    wrote on last edited by Qt Enthusiast
                    #9

                    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

                    1 Reply Last reply
                    0
                    • Q Offline
                      Q Offline
                      Qt Enthusiast
                      wrote on last edited by
                      #10

                      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);
                      
                      1 Reply Last reply
                      0
                      • VRoninV Offline
                        VRoninV Offline
                        VRonin
                        wrote on last edited by
                        #11

                        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);
                        

                        "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
                        ~Napoleon Bonaparte

                        On a crusade to banish setIndexWidget() from the holy land of Qt

                        1 Reply Last reply
                        0
                        • Q Offline
                          Q Offline
                          Qt Enthusiast
                          wrote on last edited by VRonin
                          #12

                          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 ?

                          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