[SOLVED] Qt: access model from function



  • Hallo Leute,

    this is my first message. I'm writing a small program with Qt creator under windows 7 and I have a problem with the model of a qtableview. The code is the following:

    @
    my_program::my_program(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::my_program)
    {
    ui->setupUi(this);

    QStandardItemModel *model = new QStandardItemModel(3,5,this); 
    model->setHorizontalHeaderItem(0, new QStandardItem(QString("Title")));
    model->setItem(i, 0, blablabla); //items added to the model
    /* other stuffs*/
    

    }
    @

    than I created a button. When the button is pressed I would like to add a new row to the model.
    The code is the following

    @
    void my_program::on_pushButton_2_clicked(){
    model->setItem(1, 0, blablabla);}
    @

    This istruction do not work because model is declared in the other function.
    Do there is a way to access model with the on_pushButton_2_clicked()? I've tried to declarate model as global but seems not to work.



  • Hi and lol at "Hallo Leute" :D

    I think you can just access the model from the QTableView itself, you set the model in your constructor somewhere (code is missing above?), so you can also get it back and there is no need to save a pointer to model yourself.

    in your button slot try:
    @
    ui->tableView->model()->setItem(1, 0, blablabla);
    @
    to access your model, I don't know the ID of your QTableView so I just called it "tableView" here, but I guess you get the idea here!? :)



  • Hallo Xander84,

    thanks for the fast reply. You right, some code is missing. So, the main.cpp file is almost empty

    @
    #include "my_program.h"
    #include <QApplication>

    int main(int argc, char *argv[])
    {
    QApplication a(argc, argv);

    my_program w;
    w.show();
    
    return a.exec&#40;&#41;;
    

    }
    @

    in the my_program.cpp file I have:

    @
    my_program::my_program(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::twilight)
    {
    ui->setupUi(this);

    QStandardItemModel *model = new QStandardItemModel(3,5,this); 
    model->setHorizontalHeaderItem(0, new QStandardItem(QString("title")));
    

    //few other setHorizontalHeaderItem ...

    //for loop that read a line from a file and than
    model->setItem(i, 0, new QStandardItem(line.split(",").value(0)));
    model->setItem(i, 1, new QStandardItem(line.split(",").value(1)));
    model->setItem(i, 2, new QStandardItem(line.split(",").value(2)));
    //few other setItem ....

    ui->tableView->setModel(model);
    }

    my_program::~my_program()
    {
    delete ui;
    }

    @

    than the function

    @
    void my_program::on_pushButton_2_clicked(){
    //again for loop to read data from a different file
    model->setItem(i, 0, new QStandardItem(line.split(",").value(0)));
    model->setItem(i, 1, new QStandardItem(line.split(",").value(1)));
    //few other setitem
    }
    @

    the setItem istruction in on_pushButton_2_clicked() gives as error "model was not declared in this scope."
    By replacing model->setItem with
    @ui->tableView->model->setItem @
    I get "invalid use of member function (did you forget the '()' ?)"
    and using
    @ui->tableView->model()->setItem @
    as you suggest the error changes to class QAbstractItemModel' has no member named 'setItem'

    Any Idea?


  • Lifetime Qt Champion

    Hi and welcome to devnet,

    What Xander84 suggested is something like

    @ui->tableView->model()->setItem(i, 0, new QStandardItem(line.split(",").value(0)));@



  • yes, well if the "setItem" method is not known you can either cast the model to the actual model type you are using (QStandardItemModel) or chose a different method of the base class QAbstractItemModel if possible.

    That is "simple" c++ and has not much to do with Qt so I guess you are "new" to c++ or programming in general?

    anyway you can cast the model like this and then use all methods from QStandardItemModel:
    @
    QStandardItemModel model = static_cast<QStandardItemModel>(ui->tableView->model());
    model->setItem(i, 0, new QStandardItem());
    @


  • Lifetime Qt Champion

    Sorry ! I've mixed both models.

    Just one thing: the correct way to cast a QObject derived class is qobject_cast



  • [quote author="SGaist" date="1396303878"]Just one thing: the correct way to cast a QObject derived class is qobject_cast[/quote]
    I know about qobject_cast but in this case I thought a static cast would be sufficient? qobject_cast is more like a dynamic cast and slower because it still does runtime checks or not? whereas static_cast does the checks at compile time. Not that it matters in this case but would be nice to know if it's best practice to always use qobject_cast for Qt classes?



  • Thanks. I will try tonight and I will let you know :)



  • Hi,

    Why don't you simply make *model a private member of your class so you can expand its scope to the whole class?


  • Lifetime Qt Champion

    Not for all Qt classes, just the QObject derived classes.

    The compiler only checks for obvious incompatibilities for static_cast so you can still do unsafe cast with it.



  • Thanks all for the help guys :)
    I'm not new programing, just new to Qt and I do not have totally clear how the model exactly works. model() has no method called setItem so of course I get the error. The suggested cast do not worked for me.
    I solved the problem by using the method insertRow and than setdata inside the function my_program::on_pushButton_2_clicked()

    @
    ui->tableView->model()->insertRow(i); //insert a new empty row
    ui->tableView->model()->setData(ui->tableView->model()->index(i,0), (line.split(",").value(0))); //fill the row data.
    @

    everything fine now :)



  • Of course I didn't read carefully the whole story, so please disregard my invalid suggestion :)

    Anyway, you can now edit your initial post and append [SOLVED].


Log in to reply
 

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