在表单元格中使用QWidget时,QTableView不刷新

时间:2018-07-26 13:32:14

标签: c++ qt delegates qtableview model-view

我有QTableView,并且已经在这些表单元格中使用委托使用了QPushButton。当我单击该按钮时,我需要更新这些QPushButtons

首先,我将模型和委托类设置为表视图,如下所示

qTaskManagerDialog_C::qTaskManagerDialog_C(QMainWindow *parent):
QDialog(parent)
{    
    m_taskManagerform = new Ui::TaskManagerFormUI();
    getFormUi()->setupUi(this);

    m_taskTable = getFormUi()->tableView;

    m_taskTabelDelegate = new qTaskTableViewDelegate_C();

    m_taskTable->setItemDelegateForColumn(0,m_taskTabelDelegate);
    m_taskTable->setItemDelegateForColumn(5,m_taskTabelDelegate);

    m_taskTable->createPersistentEditorForExistingItems();
}

在我的createPersistentEditorForExistingItems();函数中,我为所需的单元格调用openPersistentEditor

然后我使用QPushButtons函数将QStyledItemDelegate::createEditor设置为那些单元格

这是我的委托类中的createEditor函数

QWidget* qTaskTableViewDelegate_C::createEditor(QWidget* parent, const QStyleOptionViewItem& option, const QModelIndex& index) const
{
    if(!index.isValid()) return QStyledItemDelegate::createEditor(parent, option, index);

    if (index.column() == 0)
    {
        QModelIndex newIndex = getTaskModel()->index(index.row(),index.column(), index.parent());
        if(!newIndex.isValid()) return QStyledItemDelegate::createEditor(parent, option, index);;

        QPushButton* actionButton = getTaskModel()->getItemActionButton(newIndex);
        if(actionButton)
        {
            qJobState itemStatus = getTaskModel()->getItemStatus(newIndex);

            actionButton->setParent(parent);

            QIcon icon;
            if(itemStatus == qJobState::Running || itemStatus == qJobState::Queue)
            {
                icon.addFile(QString::fromUtf8(":/common/icon/common/png/stop.png"), QSize(), QIcon::Normal, QIcon::Off);
            }
            else if(itemStatus == qJobState::Abort)
            {
                icon.addFile(QString::fromUtf8(":/cerProd/icon/cerProd/png/resetproperty.png"), QSize(), QIcon::Normal, QIcon::Off);
            }
            else 
            {
                return QStyledItemDelegate::createEditor(parent, option, index);
            }
            actionButton->setIcon(icon);
            actionButton->setIconSize(QSize(19, 19));

            return actionButton;
        }
        else
        {
            return QStyledItemDelegate::createEditor(parent, option, index);
        }
    }
    else
    {
        return QStyledItemDelegate::createEditor(parent, option, index);
    }
}

在此函数内仔细查看。我需要根据qJobState更改按钮图标和按钮连接的插槽。当用户单击按钮时,状态将改变,我可以通过信号通知它。

因此,我尝试调用以下qTaskItem_C::updateActionButtonByStatus并进行更改。

void qTaskItem_C::updateActionButtonByStatus(qJob_C* job)
{  
    if(!getActionButton()) return;

    setTaskStatus(job->getJobState());

    //Change icon and connected slot acording to job state
    QIcon icon;
    if(getTaskStatus() == qJobState::Running || getTaskStatus() == qJobState::Queue)
    {
        printf("%s:%d\n",__FUNCTION__,__LINE__);

        disconnect(getActionButton(), SIGNAL(clicked()), this, SLOT(restartSlot()));
        connect(getActionButton(), SIGNAL(clicked()), this, SLOT(abortSlot()));
        icon.addFile(QString::fromUtf8(":/common/icon/common/png/stop.png"), QSize(), QIcon::Normal, QIcon::Off);

        if(!job->isJobAbortable()) getActionButton()->setDisabled(TRUE);
    }
    else if(getTaskStatus() == qJobState::Abort)
    {
        printf("%s:%d\n",__FUNCTION__,__LINE__);

        disconnect(getActionButton(), SIGNAL(clicked()), this, SLOT(abortSlot()));
        connect(getActionButton(), SIGNAL(clicked()), this, SLOT(restartSlot()));
        icon.addFile(QString::fromUtf8(":/cerProd/icon/cerProd/png/resetproperty.png"), QSize(), QIcon::Normal, QIcon::Off);

        if(!job->isJobRestartable()) getActionButton()->setDisabled(TRUE);
    }

    getActionButton()->setIcon(icon);
    getActionButton()->setIconSize(QSize(19, 19));
}

问题出在功能上方,但在表视图中图标和连接的插槽无法更新。

PS:完成所有这些操作后,我将在模型中调用emit dataChanged(getTopLeftIndex(), getBottomRightIndex())。表中的所有文本均在更新,但视图中的图标和连接的插槽未更新。

如何解决此问题。请任何人帮助我。这个问题浪费了我整周时间:/

0 个答案:

没有答案