Results 1 to 6 of 6

Thread: MODEL/VIEW programming and TABLES

  1. #1
    Join Date
    Aug 2006
    Posts
    3
    Thanks
    1
    Qt products
    Qt3 Qt4
    Platforms
    Windows

    Default MODEL/VIEW programming and TABLES

    hi i need help with model view programming!!!
    i have little databe of peoples, and i use Q3Table to show my table in new widget
    but my "hard" teacher wants to do it with model/view
    i dont know how can i do it and i have only few days for it.. can someone help me with this please ??
    i really thanks in advance

    show_table is the important function here.. and peoples are container type of "vector"
    and sry for my bad english..

    MainWindow.cpp

    Qt Code:
    1. .
    2. .
    3. .
    4. #include "ui/ui_MainWindow.h"
    5. #include "MainWindow.h"
    6. #include <Q3Table>
    7. #include <QRegExp>
    8. #include <QSpinBox>
    9. #include <vector>
    10. .
    11. .
    12. .
    13. #include "ui/ui_Tab.h"
    14. #include "Tab.h"
    15. #include "Tab.cpp"
    16.  
    17. using namespace std;
    18. vector <osoba> people;
    19. .
    20. .
    21. .
    22. MainWindow::MainWindow(QWidget *parent)
    23. : QMainWindow(parent)
    24. {
    25. setupUi(this);
    26. .
    27. .
    28. .
    29. count=people.size();
    30. connectSlots();
    31. }
    32.  
    33. void MainWindow::show_table()
    34. {
    35. Tab *my_table = new Tab;
    36.  
    37. my_table->table->setNumRows(people.size());
    38. my_table->table->setColumnWidth ( 0, 115 );
    39. my_table->table->setColumnWidth ( 1, 115 );
    40. my_table->table->setColumnWidth ( 2, 70 );
    41.  
    42. for( int row= 0; row < count ; row++)
    43. {
    44. my_table->table->setText(row,0,people[row].sur_name);
    45. my_table->table->setText(row,1,people[row].first_name);
    46. my_table->table->setText(row,2,people[row].number);
    47. my_table->table->sortColumn(0,TRUE,TRUE);
    48. }
    49. my_table->table->setReadOnly(TRUE);
    50. my_table->show();
    51. }
    52. .
    53. .
    54. .
    55. void MainWindow::quit()
    56. {
    57. qApp->closeAllWindows();
    58. }
    To copy to clipboard, switch view to plain text mode 

    and this is my screen of this pgm..

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: MODEL/VIEW programming and TABLES

    Where does the contents of "people" comes from? If you read it from a SQL database, you can use QSqlQueryModel or QSqlTableModel. If not, you can use QStandardItemModel or create your own model using QAbstractTableModel as a base --- it's quite easy.

    http://www.qtcentre.org/forum/f-qt-p...view-2888.html

  3. #3
    Join Date
    Aug 2006
    Posts
    3
    Thanks
    1
    Qt products
    Qt3 Qt4
    Platforms
    Windows

    Default Re: MODEL/VIEW programming and TABLES

    Quote Originally Posted by jacek
    Where does the contents of "people" comes from? If you read it from a SQL database, you can use QSqlQueryModel or QSqlTableModel. If not, you can use QStandardItemModel or create your own model using QAbstractTableModel as a base --- it's quite easy.

    http://www.qtcentre.org/forum/f-qt-p...view-2888.html
    no i didnt use any special databases like SQL...
    people is container type vector (know from C++)
    but i never make it with model/view, so i dont know how to begin...
    has someony good example for viewing table with m/w..?

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: MODEL/VIEW programming and TABLES

    Quote Originally Posted by newbie
    has someony good example for viewing table with m/w..?
    See $QTDIR/examples/itemviews.

  5. #5
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: MODEL/VIEW programming and TABLES

    Quote Originally Posted by newbie
    i readed all examples for this, and assistant too, but i still dont knoe how to do it.
    You need two things: model and view.

    As the view you can use QTableView. All you need to show the model is QTableView::setModel().

    As the model you can use QStandardItemModel which works like Q3Table --- you have to add columns and rows and then you can fill it with data.

    Better approach is to create a new class derived from QAbstractTableModel. In the QAbstractTableModel docs there is a section called "Subclassing" --- read it carefully.
    data() method provides data for the view. The index parameter specifies which "cell" the view wants and the role specifies what data the view is interested in.

    Most important role is Qt::DisplayRole --- if the view invokes data() with role == Qt::DisplayRole, this means that it wants data that it will display. For example a name of a person. Other roles are mostly used to customize the appearance. For example if you return QVariant( Qt::AlingCenter ) for some index and when role == Qt::TextAlignmetRole, that item will be centered in its cell. You can find more information about it in Qt Assistant --- just look up ItemDataRole in the index.

    The hardest part to understand is QModelIndex. Each model index has three properties row, column and the parent. There is also one special index --- and invalid index (i.e. an index for which QModelIndex::isValid() returns false ). Now, if you have a table model, then all valid indices form a table:

    (0,0) (0,1) (0,2) .... (0, m)
    (1,0) (1,1) (1,2) .... (n, m)
    .
    .
    .
    (n,0) (n,1) (n,2) .... (n, m)

    (where (x,y) means an index with row == x and column == y )

    and parent of all those indices is the invalid index. In more advanced models each index can have children, so you can have a whole hierarchy of such tables.

    When the view wants to know how big such table is (for given parent index), it will invoke rowCount() and columnCount() methods --- you must implement them, so that they return the number of rows and columns, but only for "invalid" parent.

  6. The following user says thank you to jacek for this useful post:

    newbie (23rd August 2006)

  7. #6
    Join Date
    Aug 2006
    Posts
    3
    Thanks
    1
    Qt products
    Qt3 Qt4
    Platforms
    Windows

    Default Re: MODEL/VIEW programming and TABLES

    Quote Originally Posted by jacek
    See $QTDIR/examples/itemviews.
    ok i have it its maybe too much simple..

    Qt Code:
    1. model = new QStandardItemModel(count_peopl, 3, this);
    2. model->setHeaderData(0,Qt::Horizontal, tr("Surname"));
    3. model->setHeaderData(1,Qt::Horizontal, tr("first name"));
    4. model->setHeaderData(2,Qt::Horizontal, tr("pers. number"));
    5.  
    6.  
    7. QTableView *table = new QTableView;
    8. table->setModel(model);
    9. QItemSelectionModel *selectionModel = new QItemSelectionModel(model);
    10. table->setSelectionModel(selectionModel);
    11. table->setWindowTitle(QObject::tr("Seznam zaku"));
    12. table->setColumnWidth(0,75);
    13. table->setColumnWidth(1,75);
    14. table->setColumnWidth(2,70);
    15.  
    16. table->show();
    To copy to clipboard, switch view to plain text mode 

    but one more question..
    can i set data only into "model" or can i it set into "table" too??
    EDIT: i know it now.. program is finished, thanks jacek
    Last edited by newbie; 27th August 2006 at 21:50.

Similar Threads

  1. Model - View Programming doubt.
    By munna in forum Qt Programming
    Replies: 4
    Last Post: 28th April 2006, 13:01

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.