caster89
24th April 2013, 14:01
Hi everyone,
I'm fairly new to Qt so i'm pretty sure i'm taking the long way around to solv a very simple problem. Anyhow, what I want to do is to obtain is a QListWidget in which under every image is a description of the image, which can be edited in a QTextBox, thus allowing multiple lines and wordwrap.
What I did was to create a new QItemDelegate subclassing from QStyledItemDelegate (ItemDelegate), and then I created a editor subclassing from QWidget (DelegateWidget).
The problem I am having right now is that when I pass the QModelIndex to the DelegateWidget, I can't get the Image from the DecorativeRole.
Can someone tell me what I'm doing wrong, or if there is an easier way, as I really think I'm just making a mess.
P.S.
I was actually thinking I might just create a ScrollArea from scratch and just add the images and text to the ScrollArea, but then I'd probably hav to reimplement the dragging etc.
QItemDelegate
ItemDelegate::ItemDelegate(QObject *parent)
: QStyledItemDelegate(parent)
{
}
QWidget *ItemDelegate::createEditor(QWidget *parent, const QStyleOptionViewItem &/* option */, const QModelIndex & index ) const
{
return new DelegateWidget(parent, index);
}
void ItemDelegate::setEditorData(QWidget *editor, const QModelIndex &index) const
{
static_cast<DelegateWidget*>(editor)->setText(index.data(Qt::EditRole).toString());
}
void ItemDelegate::setModelData(QWidget *editor, QAbstractItemModel *model,const QModelIndex &index) const
{
QString value=static_cast<DelegateWidget*>(editor)->GetText();
model->setData(index, value, Qt::EditRole);
}
void ItemDelegate::updateEditorGeometry(QWidget *editor, const QStyleOptionViewItem &option, const QModelIndex &index) const
{
static_cast<DelegateWidget*>(editor)->setGeometry(option.rect);
}
DelegateWidget
DelegateWidget::DelegateWidget(QWidget *parent, const QModelIndex &index) :
QWidget(parent)
,TextEditBox(new QTextEdit)
,mText(index.data().toString())
,ImgDisplay(new QLabel)
{
QVBoxLayout *vLayout =new QVBoxLayout(this);
QPixmap px=static_cast<QPixmap>(index.data(Qt::DecorationRole));
ImgDisplay->setPixmap(px);
mText=index.data(Qt::EditRole).toString();
TextEditBox->setText(mText);
vLayout->addWidget(ImgDisplay);
vLayout->addWidget(TextEditBox);
}
QString DelegateWidget::GetText() const {
return mText;
}
void DelegateWidget::setText(const QString & text){
mText=text;
TextEditBox->setText(text);
}
void DelegateWidget::updateText(void){
mText = TextEditBox->toPlainText();
//emit textChanged(mText);
}
Main
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QWidget window;
QLabel* title = new QLabel("Custom widgets on a QListWidget");
title->setAlignment(Qt::AlignHCenter);
QListWidget* list = new QListWidget;
list->setItemDelegate(new ItemDelegate(list));
list->setViewMode(QListView::IconMode);
list->setIconSize(QSize(200,200));
list->setWrapping(false);
QListWidgetItem *first = new QListWidgetItem();
first->setText("1");
first->setIcon(QPixmap("/Users/Nick/Desktop/chocolate.jpeg"));
first->setFlags (first->flags () | Qt::ItemIsEditable);
list->addItem(first);
QListWidgetItem *second = new QListWidgetItem();
second->setText("2");
second->setIcon(QIcon("/Users/Nick/Desktop/chocolate.jpeg"));
second->setFlags (second->flags () | Qt::ItemIsEditable);
list->addItem(second);
QVBoxLayout* layout = new QVBoxLayout(&window);
layout->addWidget(title);
layout->addWidget(list);
window.setLayout(layout);
window.show();
return a.exec();
}
I got most of this code from another post somewhere in the forum (can't find it anymore though)
Thank you
I'm fairly new to Qt so i'm pretty sure i'm taking the long way around to solv a very simple problem. Anyhow, what I want to do is to obtain is a QListWidget in which under every image is a description of the image, which can be edited in a QTextBox, thus allowing multiple lines and wordwrap.
What I did was to create a new QItemDelegate subclassing from QStyledItemDelegate (ItemDelegate), and then I created a editor subclassing from QWidget (DelegateWidget).
The problem I am having right now is that when I pass the QModelIndex to the DelegateWidget, I can't get the Image from the DecorativeRole.
Can someone tell me what I'm doing wrong, or if there is an easier way, as I really think I'm just making a mess.
P.S.
I was actually thinking I might just create a ScrollArea from scratch and just add the images and text to the ScrollArea, but then I'd probably hav to reimplement the dragging etc.
QItemDelegate
ItemDelegate::ItemDelegate(QObject *parent)
: QStyledItemDelegate(parent)
{
}
QWidget *ItemDelegate::createEditor(QWidget *parent, const QStyleOptionViewItem &/* option */, const QModelIndex & index ) const
{
return new DelegateWidget(parent, index);
}
void ItemDelegate::setEditorData(QWidget *editor, const QModelIndex &index) const
{
static_cast<DelegateWidget*>(editor)->setText(index.data(Qt::EditRole).toString());
}
void ItemDelegate::setModelData(QWidget *editor, QAbstractItemModel *model,const QModelIndex &index) const
{
QString value=static_cast<DelegateWidget*>(editor)->GetText();
model->setData(index, value, Qt::EditRole);
}
void ItemDelegate::updateEditorGeometry(QWidget *editor, const QStyleOptionViewItem &option, const QModelIndex &index) const
{
static_cast<DelegateWidget*>(editor)->setGeometry(option.rect);
}
DelegateWidget
DelegateWidget::DelegateWidget(QWidget *parent, const QModelIndex &index) :
QWidget(parent)
,TextEditBox(new QTextEdit)
,mText(index.data().toString())
,ImgDisplay(new QLabel)
{
QVBoxLayout *vLayout =new QVBoxLayout(this);
QPixmap px=static_cast<QPixmap>(index.data(Qt::DecorationRole));
ImgDisplay->setPixmap(px);
mText=index.data(Qt::EditRole).toString();
TextEditBox->setText(mText);
vLayout->addWidget(ImgDisplay);
vLayout->addWidget(TextEditBox);
}
QString DelegateWidget::GetText() const {
return mText;
}
void DelegateWidget::setText(const QString & text){
mText=text;
TextEditBox->setText(text);
}
void DelegateWidget::updateText(void){
mText = TextEditBox->toPlainText();
//emit textChanged(mText);
}
Main
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QWidget window;
QLabel* title = new QLabel("Custom widgets on a QListWidget");
title->setAlignment(Qt::AlignHCenter);
QListWidget* list = new QListWidget;
list->setItemDelegate(new ItemDelegate(list));
list->setViewMode(QListView::IconMode);
list->setIconSize(QSize(200,200));
list->setWrapping(false);
QListWidgetItem *first = new QListWidgetItem();
first->setText("1");
first->setIcon(QPixmap("/Users/Nick/Desktop/chocolate.jpeg"));
first->setFlags (first->flags () | Qt::ItemIsEditable);
list->addItem(first);
QListWidgetItem *second = new QListWidgetItem();
second->setText("2");
second->setIcon(QIcon("/Users/Nick/Desktop/chocolate.jpeg"));
second->setFlags (second->flags () | Qt::ItemIsEditable);
list->addItem(second);
QVBoxLayout* layout = new QVBoxLayout(&window);
layout->addWidget(title);
layout->addWidget(list);
window.setLayout(layout);
window.show();
return a.exec();
}
I got most of this code from another post somewhere in the forum (can't find it anymore though)
Thank you