Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6468869
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T05:55:49+00:00 2026-05-25T05:55:49+00:00

So what I have here is my delegate code for a QAbstractTableModel . The

  • 0

So what I have here is my delegate code for a QAbstractTableModel. The 2nd column (index = 1), is always a comboBox. That comboBox has a list of actions that a given Unit in my simulation can perform. Upon clicking certain actions like Build or Train, I want a Dialog to pop up giving the user a set of choices. After making there choice, the delegate is supposed to update the model’s data and move on.

Here is the snag I’ve run into. If I try to call a dialog from setModelData(), setEditorData(), those functions are const so I can’t assign the results of the dialog to anything.

What I have tried to do is to connect the signal indexChanged(QString) from the QComboBox created in createEditor(). This at least allowed me to assign the result and call the dialog only when the appropriate choice was made. However I keep getting a seggy fault and the trace in QTCreator will not land on C code (33 calls of assembly). I’m unsure as to why I got that seggy.

When I tried that approach, the table no longer had the combobox once the dialog appeared. It reverted to its previous state and as soon as the program left the slot I made for QComboBox::indexChanged(QString), the seggy happened.

DelegateAction.h

#ifndef DELEGATEACTION_H
#define DELEGATEACTION_H

#include <QVariant>
#include <QStyledItemDelegate>
#include <QString>

#include <QApplication>
#include <QWidget>
#include <QLabel>
#include <QComboBox>
#include <QProgressBar>
#include <QMouseEvent>

#include <JECMessageTable.h>

#include <UnitBase.h>
#include <ModelListUnit.h>

class DelegateAction : public QStyledItemDelegate
{
    Q_OBJECT
public:
    explicit DelegateAction(QObject *parent = 0);
    ~DelegateAction();

    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;
    QSize sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const;

public slots:
    void setUnits(QList<Unit*>* units);

protected:
    void paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const;
    //bool editorEvent(QEvent *event, QAbstractItemModel *model, const QStyleOptionViewItem &option, const QModelIndex &index);
private slots:
    void comboChanged(QString string);
    void comboDestroyed();

private:
    bool comboUpdated;
    const UnitBase* selectedUnit;
    ModelListUnit *model;           // The model for the JECMessageTable.

    QList<Unit*>* units;     // The current list of units.

};

#endif // DELEGATEACTION_H

DelegateAction.cpp

#include "DelegateAction.h"

DelegateAction::DelegateAction(QObject *parent) :
    QStyledItemDelegate(parent),
    selectedUnit(0),
    model(0)
{
}

DelegateAction::~DelegateAction()
{
    delete model;
}

QWidget * DelegateAction::createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const
{
    QWidget* editor = 0;

    switch (index.column())
    {
    case 0:
    default:
    {
        editor = new QLabel();
        break;
    }
    case 1:
    {
        QComboBox* combo = new QComboBox(parent);
        combo->addItem("Idle");
        combo->addItem("Gather");
        combo->addItem("Train");
        combo->addItem("Build");
        combo->addItem("Upgrade");
        editor = combo;
//        connect(combo, SIGNAL(currentIndexChanged(QString)), this, SLOT(comboChanged(QString)));
//        connect(combo, SIGNAL(destroyed()), this, SLOT(comboDestroyed()));
        break;
    }
    case 4:
    {
        editor = new QProgressBar(parent);
        break;
    }
    }

    editor->installEventFilter(const_cast<DelegateAction*>(this));
    return editor;
}

void DelegateAction::setEditorData(QWidget *editor, const QModelIndex &index) const
{
    QVariant value = index.model()->data(index, Qt::DisplayRole);

    switch (index.column())
    {
    case 0:
    default:
    {
        QLabel* label = static_cast<QLabel*>(editor);
        label->setText(value.toString());
        break;
    }
    case 1:
    {
        QComboBox* combo = static_cast<QComboBox*>(editor);
        combo->setCurrentIndex(combo->findText(value.toString()));

        break;
    }
    case 4:
    {
        QProgressBar* progress = static_cast<QProgressBar*>(editor);
        progress->setValue(value.toInt());
        break;
    }
    }

}

void DelegateAction::setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const
{
    QVariant value;
    switch (index.column())
    {
    case 0:
    default:
    {
        value = static_cast<QLabel*>(editor)->text();
        break;
    }
    case 1:
    {
        QString string = static_cast<QComboBox*>(editor)->currentText();

        switch (Action::getType(string))
        {
        case Action::INVALID:
        {
            return;
        }
        case Action::IDLE:
        {
            return;
        }
        case Action::GATHER:
        {
            return;
        }
        case Action::BUILD:
        {
            return;
        }
        case Action::TRAIN:
        {
            // Summon the build choice dialog.
            if (units == 0)
            {
                return;
            }

            JECMessageTable messageBox(this->model, "Test");
            messageBox.exec();

            if (messageBox.result() == QDialog::Accepted)
            {
                //selectedUnit = this->model->getSelectedUnit();
            }
            else
            {
               //selectedUnit = 0;
            }

            return;
        }
        case Action::MORPH:
        {
            return;
        }
        case Action::UPGRADE:
        {

        }
        }

        break;
    }
    case 4:
    {
        value = static_cast<QProgressBar*>(editor)->value();
        break;
    }
    }

    model->setData(index, value);
}

void DelegateAction::updateEditorGeometry(QWidget *editor, const QStyleOptionViewItem &option, const QModelIndex &index) const
{
    editor->setGeometry(option.rect);
}

QSize DelegateAction::sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const
{
    if (index.column() == 4)
    {
        return QSize(option.rect.width(), option.rect.height());
    }
    else
    {
        return QStyledItemDelegate::sizeHint(option, index);
    }
}

void DelegateAction::paint(QPainter *painter, const QStyleOptionViewItem &item, const QModelIndex &index) const
{
    if (index.column() == 4 && index.isValid() == true)
    {
        QStyleOptionProgressBarV2 progress;

        progress.minimum = 0;
        progress.maximum = 100;
        progress.progress = index.data().toInt();
        progress.rect = QRect(item.rect.x(), item.rect.y(), item.rect.width(), item.rect.height());

        QApplication::style()->drawControl(QStyle::CE_ProgressBar, &progress, painter);
    }
    else
    {
        // Painting the cells normally.
        QStyledItemDelegate::paint(painter, item, index);
    }
}

void DelegateAction::setUnits(QList<Unit*> *units)
{
    this->units = units;

    if (units != 0)
    {
        if (units->length() > 0)
        {
            model = new ModelListUnit(&units->at(0)->getUnitBase()->getOptionUnit());
        }
    }
}

void DelegateAction::comboChanged(QString string)
{

//    comboUpdated = true;
//    switch (Action::getType(string))
//    {
//    case Action::INVALID:
//    {
//        return;
//    }
//    case Action::IDLE:
//    {
//        return;
//    }
//    case Action::GATHER:
//    {
//        return;
//    }
//    case Action::BUILD:
//    {
//        return;
//    }
//    case Action::TRAIN:
//    {
//        // Summon the build choice dialog.
//        if (units == 0)
//        {
//            return;
//        }

//        JECMessageTable messageBox(model, "Test");
//        messageBox.exec();

//        if (messageBox.result() == QDialog::Accepted)
//        {
//            selectedUnit = model->getSelectedUnit();
//        }
//        else
//        {
//            selectedUnit = 0;
//        }

//        return;
//    }
//    case Action::MORPH:
//    {
//        return;
//    }
//    case Action::UPGRADE:
//    {

//    }
//    }

}

void DelegateAction::comboDestroyed()
{
    disconnect(this, SLOT(comboChanged(QString)));
    disconnect(this, SLOT(comboDestroyed()));
}

//bool DelegateAction::editorEvent(QEvent *event, QAbstractItemModel *model, const QStyleOptionViewItem &option, const QModelIndex &index)
//{
//    QStyledItemDelegate::editorEvent(event, model, option, index);

//    if (comboUpdated == true && index.isValid() == true)
//    {
//        comboUpdated = false;

//        if (selectedUnit != 0)
//        {
//            units->at(index.row());
//        }
//    }
//}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T05:55:49+00:00Added an answer on May 25, 2026 at 5:55 am

    I figured this out. If anyone is interested in something similar, I sub classed QTableView.

    Inside QTableView i used QTableView::setIndexWidget() to use QComboBoxes and QProgressBars. Basically my subclass had a QList of both types of widgets and they corresponded to a particular row in the table.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

here i have this code that i'm trying where openingHoursView.ohLocation has two values value1
I have a code here that generates XML for Engineer list using linq. My
I have some code here: (...) NSURLConnection *theConnection = [[NSURLConnection alloc] initWithRequest:theRequest delegate:self]; if(
i have the following code to return a list of generics that i want
Here is my setup. In my application delegate, I have a property called currentFoo.
I have something here that is really catching me off guard. I have an
We have system here that uses Java JNI to call a function in a
Here's the problem: 1.) We have page here... www.blah.com/mypage.html 2.) That page requests a
Just wondering what little scripts/programs people here have written that helps one with his
Since upgrading to 2008 I and many people here have noticed that randomly VS

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.