Thanks for the replies!
I was able to use the delegate in a small application having a QListWidget but the same code is not working in my application
I am using same code as given in the QTDIR/examples/itemviews/spinboxdelegate example. I have made changes to the main.cpp of the example. Here are the contents:
#include <QApplication>
#include <QtGui>
#include <QString>
#include <QListWidget>
#include <QListWidgetItem>
#include "delegate.h"
int main(int argc, char *argv[])
{
SpinBoxDelegate delegate;
list->setItemDelegate(&delegate);
Item1->setText("1");
Item1->setFlags (Item1->flags () | Qt::ItemIsEditable);
Item2->setText("2");
Item3->setText("3");
list
->setWindowTitle
(QObject::tr("List Box Delegate"));
list->show();
return app.exec();
}
#include <QApplication>
#include <QtGui>
#include <QString>
#include <QListWidget>
#include <QListWidgetItem>
#include "delegate.h"
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
QListWidget *list = new QListWidget;
SpinBoxDelegate delegate;
list->setItemDelegate(&delegate);
QListWidgetItem *Item1 = new QListWidgetItem(list);
Item1->setText("1");
Item1->setFlags (Item1->flags () | Qt::ItemIsEditable);
QListWidgetItem *Item2 = new QListWidgetItem(list);
Item2->setText("2");
QListWidgetItem *Item3 = new QListWidgetItem(list);
Item3->setText("3");
list->setWindowTitle(QObject::tr("List Box Delegate"));
list->show();
return app.exec();
}
To copy to clipboard, switch view to plain text mode
Now, this code is working properly i.e. when i double click on the 1st list widget item, a combo box appears which allows me to choose or enter only integer values.
When i used the same code in my application, it didn't work. In my application i created a list widget having 3 items. The list widget items are visible normally but when i set the delegate using "setItemDelegate", the list widget comes out as empty i.e. no contents are getting displayed in the list widget.
Here's a part of my code:
The header consists of several classes, i have added the delegate class to the same header.
test.h:
// Class for custom spin box delegate
{
Q_OBJECT
public:
SpinBoxDelegate
(QObject *parent
= 0);
void updateEditorGeometry
(QWidget *editor,
};
// Class for "Test" Screen Dialog
{
Q_OBJECT
public:
Test();
private:
};
// Class for custom spin box delegate
class SpinBoxDelegate : public QItemDelegate
{
Q_OBJECT
public:
SpinBoxDelegate(QObject *parent = 0);
QWidget *createEditor(QWidget *parent, const QStyleOptionViewItem &option,
const QModelIndex &index) const;
void setEditorData(QWidget *editor, const QModelIndex &index) const;
void setModelData(QWidget *editor, QAbstractItemModel *model,
const QModelIndex &index) const;
void updateEditorGeometry(QWidget *editor,
const QStyleOptionViewItem &option, const QModelIndex &index) const;
};
// Class for "Test" Screen Dialog
class Test: public QDialog
{
Q_OBJECT
public:
Test();
private:
QListWidget *m_pList;
};
To copy to clipboard, switch view to plain text mode
test.cpp
Test::Test ()
{
m_pList->setAlternatingRowColors (TRUE);
// Set delegate to combo box while editing the scenario events list widget
SpinBoxDelegate delegate;
m_pList->setItemDelegate (&delegate);
Item1->setText (tr ("1"));
Item1->setFlags (Item1->flags () | Qt::ItemIsEditable);
Item2->setText (tr ("2"));
Item3->setText (tr ("3"));
m_pList->setCurrentRow (0);
verLayout1->addWidget (someLabel);
verLayout1->addWidget (m_pList);
}
Test::Test ()
{
m_pList = new QListWidget;
m_pList->setAlternatingRowColors (TRUE);
// Set delegate to combo box while editing the scenario events list widget
SpinBoxDelegate delegate;
m_pList->setItemDelegate (&delegate);
QListWidgetItem *Item1 = new QListWidgetItem (m_pList);
Item1->setText (tr ("1"));
Item1->setFlags (Item1->flags () | Qt::ItemIsEditable);
QListWidgetItem *Item2 = new QListWidgetItem (m_pList);
Item2->setText (tr ("2"));
QListWidgetItem *Item3 = new QListWidgetItem (m_pList);
Item3->setText (tr ("3"));
m_pList->setCurrentRow (0);
QVBoxLayout *verLayout1 = new QVBoxLayout;
verLayout1->addWidget (someLabel);
verLayout1->addWidget (m_pList);
}
To copy to clipboard, switch view to plain text mode
As i mentioned, my code works normally and all items inside the list widget are visible. But when i use setItemDelegate(), everything disappears from the list widget. Only the List widget is visible.
Any ideas?
Bookmarks