Note that there are some explanatory texts on larger screens.

plurals
  1. POUsing QItemDelegate with QAbstractTableModel
    primarykey
    data
    text
    <p>I have a QAbstractItemModel and a QItemDelegate and here is my problem. The Delegate does nothing. Its subroutines are being called but nothing happens.</p> <p>Here is what I would like to see in my table.</p> <p>Text : QComboBox : Text : Text : QProgressBar</p> <p>where : is a column seperator.</p> <p>Delegate. #ifndef DELEGATEACTION_H #define DELEGATEACTION_H</p> <pre><code>#include &lt;QVariant&gt; #include &lt;QItemDelegate&gt; #include &lt;QWidget&gt; #include &lt;QLabel&gt; #include &lt;QComboBox&gt; #include &lt;QProgressBar&gt; class DelegateAction : public QItemDelegate { Q_OBJECT public: explicit DelegateAction(QObject *parent = 0); QWidget* createEditor(QWidget *parent, const QStyleOptionViewItem &amp;option, const QModelIndex &amp;index) const; void setEditorData(QWidget *editor, const QModelIndex &amp;index) const; void setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &amp;index) const; void updateEditorGeometry(QWidget *editor, const QStyleOptionViewItem &amp;option, const QModelIndex &amp;index) const; }; #endif // DELEGATEACTION_H #include "DelegateAction.h" DelegateAction::DelegateAction(QObject *parent) : QItemDelegate(parent) { } QWidget * DelegateAction::createEditor(QWidget *parent, const QStyleOptionViewItem &amp;option, const QModelIndex &amp;index) const { QWidget* editor = 0; switch (index.column()) { case 0: default: { editor = new QLabel(); break; } case 1: { QComboBox* combo = new QComboBox(parent); combo-&gt;addItem("Test"); combo-&gt;addItem("Test 2"); editor = combo; break; } case 4: { editor = new QProgressBar(parent); break; } } editor-&gt;installEventFilter(const_cast&lt;DelegateAction*&gt;(this)); return editor; } void DelegateAction::setEditorData(QWidget *editor, const QModelIndex &amp;index) const { QVariant value = index.model()-&gt;data(index, Qt::DisplayRole); switch (index.column()) { case 0: default: { QLabel* label = static_cast&lt;QLabel*&gt;(editor); label-&gt;setText(value.toString()); break; } case 1: { QComboBox* combo = static_cast&lt;QComboBox*&gt;(editor); combo-&gt;setCurrentIndex(combo-&gt;findText(value.toString())); break; } case 4: { QProgressBar* progress = static_cast&lt;QProgressBar*&gt;(editor); progress-&gt;setValue(value.toInt()); break; } } } void DelegateAction::setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &amp;index) const { QVariant value; switch (index.column()) { case 0: default: { value = static_cast&lt;QLabel*&gt;(editor)-&gt;text(); break; } case 1: { value = static_cast&lt;QComboBox*&gt;(editor)-&gt;currentText(); break; } case 4: { value = static_cast&lt;QProgressBar*&gt;(editor)-&gt;value(); break; } } model-&gt;setData(index, value); } void DelegateAction::updateEditorGeometry(QWidget *editor, const QStyleOptionViewItem &amp;option, const QModelIndex &amp;index) const { editor-&gt;setGeometry(option.rect); } </code></pre> <p>Model.</p> <pre><code>#ifndef MODELACTIONS_H #define MODELACTIONS_H #include &lt;QAbstractTableModel&gt; #include &lt;Unit.h&gt; class ModelAction : public QAbstractTableModel { Q_OBJECT public: explicit ModelAction(QObject *parent = 0); int rowCount(const QModelIndex &amp;parent = QModelIndex()) const; int columnCount(const QModelIndex &amp;parent = QModelIndex()) const; QVariant data(const QModelIndex &amp;index, int role = Qt::DisplayRole) const; Qt::ItemFlags flags(const QModelIndex &amp;index) const; QVariant headerData(int section, Qt::Orientation orientation, int role) const; bool setData(const QModelIndex &amp;index, const QVariant &amp;value, int role); void sort(int column, Qt::SortOrder order); void setUnits(const QList&lt;Unit*&gt;* units); private: const QList&lt;Unit*&gt;* units; bool ascending[5]; }; #endif // MODELACTIONS_H #include "ModelAction.h" ModelAction::ModelAction(QObject *parent) : QAbstractTableModel(parent), units(0) { } int ModelAction::rowCount(const QModelIndex &amp;parent) const { if (units == 0) { return 0; } else { return units-&gt;length(); } } int ModelAction::columnCount(const QModelIndex &amp;parent) const { return 5; } QVariant ModelAction::data(const QModelIndex &amp;index, int role) const { if (index.isValid() == false) { return QVariant(); } if (role == Qt::TextAlignmentRole) { if (index.column() == 0 || index.column() == 2) { return int(Qt::AlignLeft | Qt::AlignVCenter); } else { return int(Qt::AlignRight | Qt::AlignVCenter); } } else if (role == Qt::DisplayRole) { if (index.column() == 0) { // Unit's id. return index.row() + 1; } else if (index.column() == 1) { return "bob"; // Unit's Action. //return mechs.at(index.row())-&gt;getWeight(); } else if (index.column() == 2) { // Unit's Action start. //return mechs.at(index.row())-&gt;getTechnology(); } else if (index.column() == 3) { // Unit's Action end. //return Currency::numberToCurrency(mechs.at(index.row())-&gt;getPurchaseValue()); } else if (index.column() == 4) { // Unit's Action progress. //return Currency::numberToCurrency(mechs.at(index.row())-&gt;getSellValue()); } } return QVariant(); } QVariant ModelAction::headerData(int section, Qt::Orientation orientation, int role) const { if (role != Qt::DisplayRole) { return QVariant(); } if (orientation == Qt::Horizontal) { if (section == 0) { return "Id"; } else if (section == 1) { return "Action"; } else if (section == 2) { return "Begin Time"; } else if (section == 3) { return "End Time"; } else if (section == 4) { return "Progress"; } } return QVariant(); } void ModelAction::sort(int column, Qt::SortOrder order) { // MechCompare compare; // compare.column = column; // ascending[column] = !ascending[column]; // compare.ascending = ascending[column]; // qSort(mechs.begin(), mechs.end(), compare); // reset(); } void ModelAction::setUnits(const QList&lt;Unit *&gt; *units) { this-&gt;units = units; reset(); } Qt::ItemFlags ModelAction::flags(const QModelIndex &amp;index) const { switch (index.column()) { case 0: default: { return Qt::NoItemFlags; break; } case 1: { return Qt::ItemIsEditable | Qt::ItemIsEnabled; } } } bool ModelAction::setData(const QModelIndex &amp;index, const QVariant &amp;value, int role) { switch (index.column()) { case 1: { } } } </code></pre> <p>The only issue I'm aware of is the ModelAction::setData() function is incomplete. I have to go back and edit the data classes that this model displays before I can complete that subroutine. Still the comboboxes and progressbars should still display, just not do anything.</p> <p>At this point I only see the id numbers and my test text "bob" for each row in the table. The QComboBox and QProgressBar are <strong>not</strong> rendered at all.</p> <p>Any help will be appreciated.</p> <p>Jec</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload