[SOLVED] Qt: access model from function
-
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();
}
@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?
-
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());
@ -
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?
-
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].