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. [SOLVED] Qt: access model from function

[SOLVED] Qt: access model from function

Scheduled Pinned Locked Moved General and Desktop
12 Posts 4 Posters 5.0k Views 1 Watching
  • 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.
  • M Offline
    M Offline
    markus.goliarcur
    wrote on last edited by
    #3

    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?

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #4

      Hi and welcome to devnet,

      What Xander84 suggested is something like

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

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      0
      • X Offline
        X Offline
        Xander84
        wrote on last edited by
        #5

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

        1 Reply Last reply
        0
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #6

          Sorry ! I've mixed both models.

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

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          1 Reply Last reply
          0
          • X Offline
            X Offline
            Xander84
            wrote on last edited by
            #7

            [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?

            1 Reply Last reply
            0
            • M Offline
              M Offline
              markus.goliarcur
              wrote on last edited by
              #8

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

              1 Reply Last reply
              0
              • P Offline
                P Offline
                panosk
                wrote on last edited by
                #9

                Hi,

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

                1 Reply Last reply
                0
                • SGaistS Offline
                  SGaistS Offline
                  SGaist
                  Lifetime Qt Champion
                  wrote on last edited by
                  #10

                  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.

                  Interested in AI ? www.idiap.ch
                  Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                  1 Reply Last reply
                  0
                  • M Offline
                    M Offline
                    markus.goliarcur
                    wrote on last edited by
                    #11

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

                    1 Reply Last reply
                    0
                    • P Offline
                      P Offline
                      panosk
                      wrote on last edited by
                      #12

                      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].

                      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