QWidget and QTextEdit in QAbstractTableModel
-
As Example. My Widget has 5 Stars, its a Rating, and my Data for it is an int with 0-5. So my Model returns the int like a 3 and the ItemDelegate uses the QWidget to display the int value with 3 filled Stars? Is that what you mean?
I hope i understand you right.
-
As Example. My Widget has 5 Stars, its a Rating, and my Data for it is an int with 0-5. So my Model returns the int like a 3 and the ItemDelegate uses the QWidget to display the int value with 3 filled Stars? Is that what you mean?
I hope i understand you right.
@Fuel
See the star delegate example. -
That Example is to complicated for my needs. I dont need it Editable and i have a Class that inherits from a QWidget. Dont know why its so complicated to put the Widget in this Cell. I go and try some Parts from that Example.
@Fuel
antoher possibility is to use QAbstractItemView::setIndexWidget()
But persistent editors / index widgets shouldn't be used for big lists. -
i tried it, but nothing will be displayed
ui->tvDiaryList->setIndexWidget(diarytreemodel->index(1, 3, QModelIndex()), new QPushButton);
@Fuel said in QWidget and QTextEdit in QAbstractTableModel:
diarytreemodel->index(1, 3, QModelIndex())
make sure the returned index is valid at the time you call this line of code -> means the model already has it's data set
-
i decided to use a QStyledItemDelegate now. At the moment i dont know how i can paint the QWidget in the paint Event of the Delegate. This is what i have from a Tutorial, but its incomplete.
void StarDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const { if (index.data().canConvert<StarTable>()) { StarTable starTable = qvariant_cast<StarTable>(index.data()); if (option.state & QStyle::State_Selected) painter->fillRect(option.rect, option.palette.highlight()); starTable.paintEvent(); //dont know how to paint } else { QStyledItemDelegate::paint(painter, option, index); } }
-
i decided to use a QStyledItemDelegate now. At the moment i dont know how i can paint the QWidget in the paint Event of the Delegate. This is what i have from a Tutorial, but its incomplete.
void StarDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const { if (index.data().canConvert<StarTable>()) { StarTable starTable = qvariant_cast<StarTable>(index.data()); if (option.state & QStyle::State_Selected) painter->fillRect(option.rect, option.palette.highlight()); starTable.paintEvent(); //dont know how to paint } else { QStyledItemDelegate::paint(painter, option, index); } }
@Fuel said in QWidget and QTextEdit in QAbstractTableModel:
starTable.paintEvent(); //dont know how to paint
-> starTable.render(painter);
Although is your widget really refrenced by value? Meaning not a pointer?!
I don't think that this even compiles. -
i changed that. How can i set now, that the Delegate will shown in my Column? Do i need to setup in the Model the Data Method? Or just add the Delegate to the TableView? At the moment it doesnt work.
Sorry but thats all new to me and its much that i need to learn and understand. At the moment its to much.
-
i changed that. How can i set now, that the Delegate will shown in my Column? Do i need to setup in the Model the Data Method? Or just add the Delegate to the TableView? At the moment it doesnt work.
Sorry but thats all new to me and its much that i need to learn and understand. At the moment its to much.
@Fuel
you always get the index passed. Check the column (and/or row) of it. -
i know what you mean, but where?
update:
to show you something
DiaryTableModel *diarytablemodel = new DiaryTableModel(this); StarDelegate *stardelegate = new StarDelegate(this); ui->tvDiaryList->setModel(diarytablemodel); ui->tvDiaryList->setItemDelegateForColumn(2, stardelegate); ui->tvDiaryList->setSelectionBehavior(QAbstractItemView::SelectRows);
void StarDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const { if (index.data().canConvert<StarTable*>()) { StarTable *starTable = qvariant_cast<StarTable*>(index.data()); starTable->render(painter); } else { QStyledItemDelegate::paint(painter, option, index); } } QSize StarDelegate::sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const { if (index.data().canConvert<StarTable*>()) { StarTable *starTable = qvariant_cast<StarTable*>(index.data()); return starTable->sizeHint(); } else { return QStyledItemDelegate::sizeHint(option, index); } }
QVariant DiaryTableModel::data(const QModelIndex &index, int role) const { if (!index.isValid()) return QVariant(); if (role == Qt::DisplayRole) { if (index.column() == 0) return int(item.at(index.row())->Id); else if (index.column() == 1) return QString(item.at(index.row())->Date); else if (index.column() == 2) { } else if (index.column() == 3) { } } return QVariant(); } void DiaryTableModel::setupModelData() { QTextEdit *text = new QTextEdit(); text->setText("Test"); item.append(new DiaryTableItem(0, "monday", new StarTable(2), text)); }
-
i changed that. How can i set now, that the Delegate will shown in my Column? Do i need to setup in the Model the Data Method? Or just add the Delegate to the TableView? At the moment it doesnt work.
Sorry but thats all new to me and its much that i need to learn and understand. At the moment its to much.
@Fuel said in QWidget and QTextEdit in QAbstractTableModel:
At the moment it doesnt work.
what exactly doesn't work?
If the column count is wrong, then your model needs to return the correct column count and data for the index. -
Anyone can help me? Im so close. At the moment i get an Error. The Program stopps working because of a Read Access Violation. In the Debugger i can see that the starTable Pointer dont shows to a Memory Address
void StarDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const { StarTable *starTable = qvariant_cast<StarTable*>(index.data()); starTable->render(painter); } `The Pointer shows to 0x0. Anyone knows how to pick up here in this Delegate the Class that inherits from a QWidget?
-
Hi,
As @raven-worx wrote, are you sure the index is correct ? In any case, an assert for a null pointer wouldn't be a bad idea.
-
Anyone can help me? Im so close. At the moment i get an Error. The Program stopps working because of a Read Access Violation. In the Debugger i can see that the starTable Pointer dont shows to a Memory Address
void StarDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const { StarTable *starTable = qvariant_cast<StarTable*>(index.data()); starTable->render(painter); } `The Pointer shows to 0x0. Anyone knows how to pick up here in this Delegate the Class that inherits from a QWidget?