Results 1 to 1 of 1

Thread: [SOLVED] can't get QTableView working

  1. #1
    Join Date
    Nov 2008
    Location
    Croatia
    Posts
    4
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default [SOLVED] can't get QTableView working

    I'm trying to get QTableView working, from this example: http://doc.qt.nokia.com/latest/model...-example-model

    so i made a new project in creator, added my custom class (a copy paste from example):
    mymodel.h
    Qt Code:
    1. #ifndef MYMODEL_H
    2. #define MYMODEL_H
    3.  
    4. #include <QAbstractTableModel>
    5.  
    6. class MyModel : public QAbstractTableModel
    7. {
    8. Q_OBJECT
    9. public:
    10. MyModel(QObject *parent);
    11. int rowCount(const QModelIndex &parent = QModelIndex()) const ;
    12. int columnCount(const QModelIndex &parent = QModelIndex()) const;
    13. QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const;
    14. };
    15. #endif // MYMODEL_H
    To copy to clipboard, switch view to plain text mode 

    mymodel.cpp
    Qt Code:
    1. #include "mymodel.h"
    2. #include "QDebug"
    3.  
    4. MyModel::MyModel(QObject *parent)
    5. {
    6. qDebug() << "constructor";
    7. }
    8.  
    9. int MyModel::rowCount(const QModelIndex & /*parent*/) const
    10. {
    11. return 2;
    12. }
    13.  
    14. int MyModel::columnCount(const QModelIndex & /*parent*/) const
    15. {
    16. return 3;
    17. }
    18.  
    19. QVariant MyModel::data(const QModelIndex &index, int role) const
    20. {
    21. if (role == Qt::DisplayRole)
    22. {
    23. return QString("Row%1, Column%2")
    24. .arg(index.row() + 1)
    25. .arg(index.column() +1);
    26. }
    27. return QVariant();
    28. }
    To copy to clipboard, switch view to plain text mode 

    and in dialog.cpp i have push button on click, where i add the model to the table:
    dialog.cpp
    Qt Code:
    1. #include "dialog.h"
    2. #include "ui_dialog.h"
    3. #include "mymodel.h"
    4.  
    5. Dialog::Dialog(QWidget *parent) :
    6. QDialog(parent),
    7. ui(new Ui::Dialog)
    8. {
    9.  
    10. ui->setupUi(this);
    11. }
    12.  
    13. Dialog::~Dialog()
    14. {
    15. delete ui;
    16. }
    17.  
    18. void Dialog::on_pushButton_clicked()
    19. {
    20. MyModel myModel(0);
    21. ui->tableView->setModel( &myModel );
    22. ui->tableView->show();
    23.  
    24.  
    25. }
    To copy to clipboard, switch view to plain text mode 

    and when i run the example and click the button, i get this, nothing in table:

    Screenshot-Dialog.png

    i can just guess it's something basic i have missed, but i have no idea where?


    Added after 1 40 minutes:


    Found the solution, i created myModel locally...
    Last edited by marvin; 26th February 2011 at 11:08.

Similar Threads

  1. How to identify working row in QTableView
    By ronak.vashi in forum Newbie
    Replies: 2
    Last Post: 9th December 2010, 09:57
  2. Replies: 2
    Last Post: 26th November 2009, 04:45
  3. QTableView not working as expected.
    By junxuan in forum Qt Programming
    Replies: 7
    Last Post: 30th July 2009, 08:17
  4. QTableView doubleclicked event not working
    By sgmurphy19 in forum Qt Programming
    Replies: 6
    Last Post: 15th March 2009, 10:00
  5. QTableView ::setColumnWidth not working?
    By killerwookie99 in forum Qt Programming
    Replies: 2
    Last Post: 5th November 2008, 17:14

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.