Change StackedWidget Pages With TreeWidget



  • Hello,
    I Have a StackedWidget and TreeWidget in my Window
    I Want to Connect this Two Object,
    And I Wrote This Codes But None Of This Codes Worked!
    My Codes:

    #include "management_menu.h"
    #include "ui_management_menu.h"
    
    management_menu::management_menu(QWidget *parent) :
        QWidget(parent),
        ui(new Ui::management_menu)
    {
        ui->setupUi(this);
        connect(ui->lineEdit,SIGNAL(textChanged(QString)),this,SLOT(onTextChanged()));
       connect(ui->treeWidget,SIGNAL(itemActivated(QTreeWidgetItem*,int)),ui->stackedWidget,SLOT(setCurrentIndex(int)));
        connect(ui->treeWidget,SIGNAL(currentItemChanged(QTreeWidgetItem*,QTreeWidgetItem*)),ui->stackedWidget,SLOT(setCurrentIndex(int)));
        connect(ui->treeWidget,SIGNAL(itemChanged(QTreeWidgetItem*,int)),ui->stackedWidget,SLOT(setCurrentIndex(int)));
        connect(ui->treeWidget,SIGNAL(itemClicked(QTreeWidgetItem*,int)),ui->stackedWidget,SLOT(setCurrentIndex(int)));
    
    
    
    
    }
    
    management_menu::~management_menu()
    {
        delete ui;
    }
    
    
    

    What i must do?



  • @M4RZB4Ni

    the signal and slot parameter does not match. Try to write your own slot then call stackwidget methods.

    example :

    connect(ui->treeWidget,SIGNAL(itemActivated(QTreeWidgetItem*,int)), this, SLOT(myslot(QTreeWidgetItem*,int)));
    
    in my slot:
    void management_menu::mySlot(QTreeWidgetItem*,int index) 
    {
         ui->stackedWidget->setCurrentIndex(index);
    }
    
    

    This should work.



  • @c.savur
    this works but
    No more than once
    in Second click this those not work!



  • @M4RZB4Ni

    Yes, you are right it will work only one timer because, itemActivated(QTreeWidgetItem*,int), the second parameter is column id. So this will be same for the entire column.

    One way to do is that set some information to QTreeWidgetItem and in myslot use this information to call stackedWidget methods.

        QTreeWidgetItem *s = new QTreeWidgetItem();
    // set data
        s->setData(0, Qt::UserRole, QVariant("1"));
     
    // obtain data   
        QVariant data = s->data(0, Qt::UserRole);
    
    




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