PDA

View Full Version : tableview , model and owncolumn



maston
27th August 2010, 22:31
Hi.

I have something like this :


QSqlQueryModel *model = new QSqlQueryModel(this);
model->setQuery("SELECT name,surname FROM students WHERE class= "+id);
model->setHeaderData(0, Qt::Horizontal, QObject::tr("Name"));
model->setHeaderData(1, Qt::Horizontal, QObject::tr("Surname"));
model->insertColumn(2);
model->setHeaderData(2, Qt::Horizontal, QObject::tr("Amount"),Qt::EditRole);


And i want to set third column to editable value. I have to type there text.

How force model to set column(2) with textbox field?

Thanks in advance :)

waynew
27th August 2010, 23:35
The user can't edit the model directly, you have to edit through the view.
Like this, for example:



QModelIndex index = ui->tvMemories->QAbstractItemView::currentIndex();
ui->tvMemories->edit(index);

maston
28th August 2010, 09:53
thank You for Your reply

but i have to do something like this:

http://www.maston.pl/qt.jpg

insert line edit in last cell and next get value to insert to database after click save

tbscope
28th August 2010, 10:14
Create a delegate

maston
28th August 2010, 10:35
thanks :)

i have this.

in header :



namespace Ui {
class DodajWplate;
class LineEditDelegate;
}



class DodajWplate : public QDialog {
...
};


class LineEditDelegate : public QItemDelegate
{
Q_OBJECT

public:
LineEditDelegate(QObject* parent = 0);

QWidget* createEditor(QWidget *parent, const QStyleOptionViewItem& option, const QModelIndex& index) const;
void setEditorData(QWidget* editor, const QModelIndex& index) const;
};


in cpp :



QWidget* LineEditDelegate::createEditor(QWidget* parent, const QStyleOptionViewItem&, const QModelIndex&) const
{
QLineEdit* lewtv = new QLineEdit(parent);

return lewtv;
}

void LineEditDelegate::setEditorData(QWidget* editor, const QModelIndex& index) const
{
QString value = index.model()->data(index, Qt::EditRole).toString();

QLineEdit* lewtv = static_cast<QLineEdit*>(editor);

lewtv->setText(value);
}


and



ui->tableView->setItemDelegateForColumn(2, new LineEditDelegate);


When i want to build i have error :

undefined reference to `LineEditDelegate::LineEditDelegate(QObject*)'

tbscope
28th August 2010, 10:51
Do you have

LineEditDelegate::LineEditDelegate(QObject *parent) :
QItemDelegate(parent)
{
}
in your cpp file?

maston
28th August 2010, 11:00
thanks.
program runs. but tableview in column(2) doesn't have lineedit widget :( i have to change something? Maybe something is missing