PDA

View Full Version : MODEL/VIEW programming and TABLES



newbie
22nd August 2006, 14:26
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 ??:confused:
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



.
.
.
#include "ui/ui_MainWindow.h"
#include "MainWindow.h"
#include <Q3Table>
#include <QRegExp>
#include <QSpinBox>
#include <vector>
.
.
.
#include "ui/ui_Tab.h"
#include "Tab.h"
#include "Tab.cpp"

using namespace std;
vector <osoba> people;
.
.
.
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
{
setupUi(this);
.
.
.
count=people.size();
connectSlots();
}

void MainWindow::show_table()
{
Tab *my_table = new Tab;

my_table->table->setNumRows(people.size());
my_table->table->setColumnWidth ( 0, 115 );
my_table->table->setColumnWidth ( 1, 115 );
my_table->table->setColumnWidth ( 2, 70 );

for( int row= 0; row < count ; row++)
{
my_table->table->setText(row,0,people[row].sur_name);
my_table->table->setText(row,1,people[row].first_name);
my_table->table->setText(row,2,people[row].number);
my_table->table->sortColumn(0,TRUE,TRUE);
}
my_table->table->setReadOnly(TRUE);
my_table->show();
}
.
.
.
void MainWindow::quit()
{
qApp->closeAllWindows();
}


and this is my screen of this pgm.. http://ki.ujep.cz/~minsky/screenqt.jpg

jacek
22nd August 2006, 14:41
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-programming-2/t-speed-of-setdata-lots-of-items-in-treeview-2888.html

newbie
22nd August 2006, 15:54
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-programming-2/t-speed-of-setdata-lots-of-items-in-treeview-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..?

jacek
22nd August 2006, 16:36
has someony good example for viewing table with m/w..?
See $QTDIR/examples/itemviews.

jacek
22nd August 2006, 22:17
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" (http://doc.trolltech.com/4.1/qabstracttablemodel.html#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 (http://doc.trolltech.com/4.1/qt.html#ItemDataRole-enum) 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.

newbie
27th August 2006, 21:26
See $QTDIR/examples/itemviews.

ok i have it its maybe too much simple..



model = new QStandardItemModel(count_peopl, 3, this);
model->setHeaderData(0,Qt::Horizontal, tr("Surname"));
model->setHeaderData(1,Qt::Horizontal, tr("first name"));
model->setHeaderData(2,Qt::Horizontal, tr("pers. number"));


QTableView *table = new QTableView;
table->setModel(model);
QItemSelectionModel *selectionModel = new QItemSelectionModel(model);
table->setSelectionModel(selectionModel);
table->setWindowTitle(QObject::tr("Seznam zaku"));
table->setColumnWidth(0,75);
table->setColumnWidth(1,75);
table->setColumnWidth(2,70);

table->show();


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