PDA

View Full Version : EXAMPLE:Trying to understand model/view with QTableView!!!



fatecasino
22nd February 2011, 20:22
well, I have read some tens of times most tutorials,posts and examples. I think I have understood most of the functions and classes, but I fail to put all these together.

I am looking for a very simple example:

a tableview with just 1 row and 2 columns, just two boxes.
One box should be a widget-qcombobox and the other just normal box to insert a string.

Now, I want to connect the database of the tableview with a QLineEdit (let's call it resultLineEdit) every time a new value is inserted by the user.

Can you give me a piece of code to start with?

Added after 1 58 minutes:

i started like this:


class M : public QWidget
{
Q_OBJECT

public:
M(QWidget *parent = 0);


private:
QStandardItemModel *model;
QTableView *table;

QLineEdit * resultEdit;
QPushButton *quit;

};



M::M(QWidget *parent)
: QWidget(parent)
{
int r = 2,c = 2;

model = new QStandardItemModel(r,c);
table = new QTableView();
table->setModel(model);

for (int row = 0; row < r; ++row) {
for (int column = 0; column < c; ++column) {
QStandardItem *item = new QStandardItem(QString("iii"));
model->setItem(row,column,item);
}
}

resultEdit = new QLineEdit;
resultEdit->setText("results here....");
quit = new QPushButton;
quit->setText("quit!");
connect(quit, SIGNAL(clicked()), qApp, SLOT(quit()));
QVBoxLayout *mainLayout = new QVBoxLayout();
mainLayout->addWidget(resultEdit);
mainLayout->addWidget(quit);
mainLayout->addWidget(table);
setLayout(mainLayout);
}

the question is:
how can I connect the the changes of any item of the table to the slot setText(QString) of the resultEdit?

fatecasino
22nd February 2011, 20:37
Is it a combination of the following?

QItemSelectionModel
QModelIndex
Qt:: DisplayRole

any small code example would be really helpful!

wysota
22nd February 2011, 22:11
QTableView is not really fit for what you want. I would rather use QDataWidgetMapper. It uses widgets to display data from a model. QTableView doesn't.

fatecasino
23rd February 2011, 10:27
just to understand,
do you mean that if I generally want to display/use the data from the model/view architecture I have to use QDataWidgetMapper? What I really want to do is the user to set some parameters to plot a scientific diagram through qwt. The user would inserts these values (boxes and qcomboboxes) and if necessary s/he would add a new row and insert more (it's a complex curve fitting problem)