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)
{
}
{
return new DelegateWidget(parent, index);
}
{
static_cast<DelegateWidget*>(editor)->setText(index.data(Qt::EditRole).toString());
}
{
QString value
=static_cast<DelegateWidget
*>
(editor
)->GetText
();
model->setData(index, value, Qt::EditRole);
}
{
static_cast<DelegateWidget*>(editor)->setGeometry(option.rect);
}
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);
}
To copy to clipboard, switch view to plain text mode
DelegateWidget
,mText(index.data().toString())
{
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);
}
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);
}
To copy to clipboard, switch view to plain text mode
Main
int main(int argc, char *argv[])
{
QLabel* title
= new QLabel("Custom widgets on a QListWidget");
title->setAlignment(Qt::AlignHCenter);
list->setItemDelegate(new ItemDelegate(list));
list
->setIconSize
(QSize(200,
200));
list->setWrapping(false);
first->setText("1");
first
->setIcon
(QPixmap("/Users/Nick/Desktop/chocolate.jpeg"));
first->setFlags (first->flags () | Qt::ItemIsEditable);
list->addItem(first);
second->setText("2");
second
->setIcon
(QIcon("/Users/Nick/Desktop/chocolate.jpeg"));
second->setFlags (second->flags () | Qt::ItemIsEditable);
list->addItem(second);
layout->addWidget(title);
layout->addWidget(list);
window.setLayout(layout);
window.show();
return a.exec();
}
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();
}
To copy to clipboard, switch view to plain text mode
I got most of this code from another post somewhere in the forum (can't find it anymore though)
Thank you
Bookmarks