[SOLVED] change the Qstackedwidget page when the Qtreeview item is clicked



  • when the Qtreeview item is clicked on, i am trying to change the Qstackedwidget page. i need to get the int value for the row that the qtreeview item was clicked on. i could not find anything in the documents that would help. i know why i get the error message. i just don't know how to solve it.

    i am getting the error: no matching function for call to 'QStackedWidget::setCurrentIndex(const QModelIndex&)'
    candidates are: void QStackedWidget::setCurrentIndex(int)

    below is the modified code from the qt examples. at line 52, i get the error

    @#include "mainwindow.h"
    #include "ui_mainwindow.h"
    #include <QTreeView>
    #include <QStandardItemModel>
    #include <QItemSelectionModel>

    MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
    {
    ui->setupUi(this);
    //ui->treeView = new QTreeView(this);
    // setCentralWidget(treeView);
    standardModel = new QStandardItemModel ;
    QStandardItem *rootNode = standardModel->invisibleRootItem();

    //defining a couple of items
    QStandardItem *americaItem = new QStandardItem("America");
    QStandardItem *mexicoItem =  new QStandardItem("Canada");
    QStandardItem *usaItem =     new QStandardItem("USA");
    QStandardItem *bostonItem =  new QStandardItem("Boston");
    QStandardItem *europeItem =  new QStandardItem("Europe");
    QStandardItem *italyItem =   new QStandardItem("Italy");
    QStandardItem *romeItem =    new QStandardItem("Rome");
    QStandardItem *veronaItem =  new QStandardItem("Verona");
    
    //building up the hierarchy
    rootNode->    appendRow(americaItem);
    rootNode->    appendRow(europeItem);
    americaItem-> appendRow(mexicoItem);
    americaItem-> appendRow(usaItem);
    usaItem->     appendRow(bostonItem);
    europeItem->  appendRow(italyItem);
    italyItem->   appendRow(romeItem);
    italyItem->   appendRow(veronaItem);
    
    //register the model
    ui->treeView->setModel(standardModel);
    ui->treeView->expandAll();
    
    //selection changes shall trigger a slot
    QItemSelectionModel *selectionModel= ui->treeView->selectionModel();
    connect(selectionModel, SIGNAL(selectionChanged (const QItemSelection &, const QItemSelection &)),
            this, SLOT(selectionChangedSlot(const QItemSelection &, const QItemSelection &)));
    

    }

    void MainWindow::selectionChangedSlot(const QItemSelection & /newSelection/, const QItemSelection & /oldSelection/)
    {
    //get the text of the selected item
    const QModelIndex index = ui->treeView->selectionModel()->currentIndex();
    ui->stackedWidget->setCurrentIndex(index);
    }

    MainWindow::~MainWindow()
    {
    delete ui;
    }@



  • The error seems really clear to me. You are trying to call QStackedWidget::setCurrentIndex() with a [[doc:QModelIndex]] instead of with an int. A QModelIndex is not the same as an int, and cannot be converted to it, so obviously your compiler complains.

    The thing about trees is, that there is no clear way to say that any item really is on a given row. It is a nested list, where each node in the tree may be the parent of a new list, that starts its row counting again at 0. So, only you know which page you want to show for which selected item your tree.

    A simple solution for your problem is to add some more information to the nodes in the tree themselves. You can add the corresponding page id as a separate data role to your QStandardItems. Then, when you want to respond to having one of these selected, you read back that piece of data, and use that to set the right page number. See the data() and setData() methods for both QStandardItem and QAbstractItemModel.



  • If you know for sure, that the tree view actually only contains a list and not a hierarchy of items, you can get the current row from the [[Doc:QModelIndex]]. I leave it to your exercise to click on the link and discover the method name :-)

    The better approach is to store the stacked widget's page id using data() as suggested by Andre. This way you decouple the id from the order of the items (which may change if you sort for example!).



  • i am going to try with Andre approach. I am getting a compile error. could you tell me if this code will work once the header is modified...

    error in mainwindow.h: error: 'QStandardItem::data' is not a type
    mainwindow.h
    @private slots:
    const QVariant dataChanged(QStandardItem::data(int));@

    mainwindow.cpp
    @ connect(this, SIGNAL(itemChanged(const QStandardItem::data( int role))), this, SLOT(dataChanged(const QStandardItem::data( int role))));
    }

    void MainWindow::dataChanged(const QStandardItem::data(int role))
    {

    ui->stackedWidget->setCurrentIndex(role);
    

    }
    @



  • That's plain C++ errors, nothing Qt specific. The parameters of methods take class names, not method names with class specifiers as arguments. And the method declaration in the header file must match the definition in the .cpp file.

    I can only guess what you want:

    @
    private slots:
    void dataChanged(const QStandardItem *item);

    // wherever that itemChanged signal may be declared....
    //
    connect(this, SIGNAL(itemChanged(QStandardItem *)), this, SLOT(dataChanged(QStandardItem *)));

    void MainWindow::dataChanged(const QStandardItem *item)
    {
    int stackId = item->data(IdRole);
    ui->stackedWidget->setCurrentIndex(stackId);
    }
    @

    But before continuing with this project, I strongly recommend to read and understand a good introduction into C++ textbook or online resource. This all is very basic stuff and it seems you do not completely understand what the pieces are and how they play together. It will ease your further development experience dramatically.



  • Volker your code is giving the same error and mine. yes, i am already ready a c++ book.

    error in mainwindow.h: error: ‘QStandardItem::data’ is not a type
    mainwindow.h

    I have decided to use the listwidget instead of the treewidget. the treewidget is a bit too complicated for me right now.



  • Volkers's code does not give the same error. Really, it doesn't.

    You, on the other hand, also really need to read more about the syntax of the connect statement.



  • [quote author="kalster" date="1325990995"]Volker your code is giving the same error and mine. yes, i am already ready a c++ book.

    error in mainwindow.h: error: ‘QStandardItem::data’ is not a type
    mainwindow.h
    [/quote]

    You see the characters "QStandardItem::data" literally somewhere in the few lines I've posted? No? So what makes you think that this can be the cause of an error message then?

    [quote author="kalster" date="1325990995"]
    I have decided to use the listwidget instead of the treewidget. the treewidget is a bit too complicated for me right now.[/quote]

    Do a search for "Tree" and replace it by "List" in your source code and you'll run into the very same troubles.

    BTW: Why are you using a QTree widget together with a QStandardItem?



  • [quote author="Andre" date="1326016730"]Volkers's code does not give the same error. Really, it doesn't.

    You, on the other hand, also really need to read more about the syntax of the connect statement. [/quote]

    your correct Andre. Volkers code did not give the error that i thought. I apologize. I guess i got a bit confused in the code that i was modifying and thought it was volkers code. :)

    i never had any problems with the connect statement until the treeview code which i basically gave up on.



  • [quote author="Volker" date="1326033677"]
    You see the characters "QStandardItem::data" literally somewhere in the few lines I've posted? No? So what makes you think that this can be the cause of an error message then?

    Do a search for "Tree" and replace it by "List" in your source code and you'll run into the very same troubles.

    BTW: Why are you using a QTree widget together with a QStandardItem?[/quote]

    your right volker. your code did not give the error message and I apologize. read what i said about that in my next post above.

    I am not replacing the code. i am starting over with a listwidget code that i got the signal and slot to work. everything is up and running now and without any problems. lately, i never really had too many problems declaring code up until the treeview code that was a bit too complicated for me. nevertheless, I am still studying c++ so bare with me until i get it all figured out.


Log in to reply
 

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