Not able to add model to the main window..



  • hi, I've the following code

    mymodel.h

    @#ifndef MYMODEL_H
    #define MYMODEL_H

    #include <QAbstractTableModel>

    #include <QString>

    const int COLS= 3;
    const int ROWS= 3;

    class MyModel : public QAbstractTableModel
    {
    Q_OBJECT
    public:
    MyModel(QObject *parent);
    int rowCount(const QModelIndex &parent = QModelIndex()) const ;
    int columnCount(const QModelIndex &parent = QModelIndex()) const;
    QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const;
    QVariant headerData(int section, Qt::Orientation orientation, int role) const;

     bool setData(const QModelIndex & index, const QVariant & value, int role = Qt::EditRole);
          Qt::ItemFlags flags(const QModelIndex & index) const ;
    

    private:
    QString m_gridData[ROWS][COLS]; //holds text entered into QTableView

    signals:

          void editCompleted(const QString &);
    

    };

    #endif // MYMODEL_H
    @

    mymodel.cpp

    @#include "mymodel.h"

    MyModel::MyModel(QObject *parent)
    :QAbstractTableModel(parent)
    {
    }

    int MyModel::rowCount(const QModelIndex & /parent/) const
    {
    return 3;
    }

    int MyModel::columnCount(const QModelIndex & /parent/) const
    {
    return 3;
    }

    QVariant MyModel::data(const QModelIndex &index, int role) const
    {
    if (role == Qt::DisplayRole)
    {
    return QString("Row%1, Column%2")
    .arg(index.row() + 1)
    .arg(index.column() +1);
    }
    return QVariant();
    }

    QVariant MyModel::headerData(int section, Qt::Orientation orientation, int role) const
    {
    if (role == Qt::DisplayRole)
    {
    //////////////////////////////////////////Column Name
    if (orientation == Qt::Horizontal) {
    switch (section)
    {
    case 0:
    return QString("Column-1");
    case 1:
    return QString("Column-2");
    case 2:
    return QString("Column-3");
    }
    }

          /////////////////////////////////////////////Row Name
    
          if (orientation == Qt::Vertical) {
              switch (section)
              {
              case 0:
                  return QString("Row-1");
              case 1:
                  return QString("Row-2");
              case 2:
                  return QString("Row-3");
              }
          }
    
      }
    
      return QVariant();
    

    }

    bool MyModel::setData(const QModelIndex & index, const QVariant & value, int role)
    {
    if (role == Qt::EditRole)
    {
    //save value from editor to member m_gridData
    m_gridData[index.row()][index.column()] = value.toString();
    //for presentation purposes only: build and emit a joined string
    QString result;
    for(int row= 0; row < ROWS; row++)
    {
    for(int col= 0; col < COLS; col++)
    {
    result += m_gridData[row][col] + " ";
    }
    }
    emit editCompleted( result );
    }
    return true;
    }

    Qt::ItemFlags MyModel::flags(const QModelIndex & /index/) const
    {
    return Qt::ItemIsSelectable | Qt::ItemIsEditable | Qt::ItemIsEnabled ;
    }
    @

    mainwindow.h

    @#ifndef MAINWINDOW_H
    #define MAINWINDOW_H

    #include <QtGui/QTableView>
    #include "mymodel.h"

    #include <QMainWindow>

    class MyModel;

    namespace Ui {
    class MainWindow;
    }

    class MainWindow : public QMainWindow
    {
    Q_OBJECT

    public:
    explicit MainWindow(QWidget *parent = 0);
    ~MainWindow();

    private:
    Ui::MainWindow *ui;

    MyModel* model;
    

    };

    #endif // MAINWINDOW_H
    @

    mainwindow.cpp

    @#include "MainWindow.h"
    #include "ui_MainWindow.h"
    #include <QTableView>

    MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
    {
    ui->setupUi(this);
    model=new MyModel(this);
    QTableView tableView;
    tableView.setModel( &model );

    }

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

    and a main.cpp

    @#include <QtGui/QApplication>
    #include "MainWindow.h"

    int main(int argc, char *argv[])
    {
    QApplication a(argc, argv);
    MainWindow w;
    w.show();

    return a.exec&#40;&#41;;
    

    }
    @

    My task is to created model is attached in the main window.But My code gives me the following error..
    1.no matching function for call to 'QTableView::setModel(MyModel**)'

    tell me am I on the right track or not and how to remove that problem(any one plz help)



  • hello

    try to replace :
    tableView.setModel( &model );
    by
    tableView.setModel( model );

    frédéric



  • Your table view is created on the stack. As soon as the variable goes out of scpe - here: your constructor has finished - it is destroyed. Use a pointer and heap allocation:

    @
    QTableView *tableView = new QTableView(this);
    tableView->setModel(model);
    @

    Also, make sure to make the view the central widget or add it to a layout.


Log in to reply
 

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