Commit 4b80fa53 authored by Alvaro Sanchez's avatar Alvaro Sanchez

Fix listView update issue in qtCheckItemComboBox.

(cherry-picked from 6708614d)
parent 76bef1e7
......@@ -63,7 +63,6 @@ void qtCheckItemComboBox::init()
{
this->m_displayItem = new QStandardItem;
this->m_displayItem->setFlags(Qt::ItemIsEnabled);
this->m_displayItem->setCheckable(false);
this->m_displayItem->setText("0 " + m_displayTextExt);
QStandardItemModel* itemModel = qobject_cast<QStandardItemModel*>(this->model());
if (itemModel)
......@@ -93,7 +92,15 @@ void qtCheckItemComboBox::updateText()
: QString::number(numSel) + " " + m_displayTextExt;
this->m_displayItem->setText(displayText);
this->view()->model()->setData(this->view()->model()->index(0, 0), displayText, Qt::DisplayRole);
this->view()->update();
// For the item list, all the painting operations take place in the viewport
// (since it is a QAbstractItemView), so use its update() to reflect any changes.
// http://doc.qt.io/qt-5/qabstractitemview.html#update
this->view()->viewport()->update();
// Additionally, update the baseclass to reflect the changes in the
// actual combobox (selected item name and button).
this->update();
}
void qtCheckItemComboBox::hidePopup()
......@@ -109,6 +116,27 @@ void qtCheckItemComboBox::showPopup()
this->QComboBox::showPopup();
}
bool qtCheckItemComboBox::eventFilter(QObject* editor, QEvent* evt)
{
if (evt->type() == QEvent::MouseButtonRelease)
{
const int index = view()->currentIndex().row();
auto itemModel = qobject_cast<QStandardItemModel*>(this->model());
QStandardItem* item = itemModel->item(index);
if (item->isCheckable())
{
const auto newState =
itemData(index, Qt::CheckStateRole) == Qt::Checked ? Qt::Unchecked : Qt::Checked;
setItemData(index, newState, Qt::CheckStateRole);
}
return true;
}
return QObject::eventFilter(editor, evt);
}
////////////////////////////////////////////////////////////////////////////////
qtModelEntityItemCombo::qtModelEntityItemCombo(
qtModelEntityItem* entitem, QWidget* inParent, const QString& displayExt)
: qtCheckItemComboBox(inParent, displayExt)
......@@ -195,7 +223,6 @@ void qtModelEntityItemCombo::init()
// this, SLOT(attributeFilterChanged(QStandardItem*)));
this->blockSignals(false);
this->view()->viewport()->installEventFilter(this);
// this->view()->setSelectionMode(QAbstractItemView::ExtendedSelection);
this->updateText();
this->hidePopup();
......@@ -210,36 +237,10 @@ void qtModelEntityItemCombo::init()
void qtModelEntityItemCombo::showPopup()
{
this->setModel(new QStandardItemModel());
this->init();
this->qtCheckItemComboBox::showPopup();
}
bool qtModelEntityItemCombo::eventFilter(QObject* editor, QEvent* evt)
{
if (evt->type() == QEvent::MouseButtonRelease)
{
int index = view()->currentIndex().row();
/*
// with the help of styles, check if checkbox rect contains 'pos'
QMouseEvent* e = dynamic_cast<QMouseEvent*>(evt);
QStyleOptionButton opt;
opt.rect = view()->visualRect(view()->currentIndex());
QRect r = style()->subElementRect(QStyle::SE_ViewItemCheckIndicator, &opt);
if(r.contains(e->pos()))
{
*/
if (itemData(index, Qt::CheckStateRole) == Qt::Checked)
setItemData(index, Qt::Unchecked, Qt::CheckStateRole);
else
setItemData(index, Qt::Checked, Qt::CheckStateRole);
// }
return true;
}
return QObject::eventFilter(editor, evt);
}
void qtModelEntityItemCombo::itemCheckChanged(const QModelIndex& topLeft, const QModelIndex&)
{
QStandardItemModel* itemModel = qobject_cast<QStandardItemModel*>(this->model());
......@@ -297,6 +298,7 @@ void qtModelEntityItemCombo::itemCheckChanged(const QModelIndex& topLeft, const
}
}
////////////////////////////////////////////////////////////////////////////////
qtMeshItemCombo::qtMeshItemCombo(qtMeshItem* entitem, QWidget* inParent, const QString& displayExt)
: qtCheckItemComboBox(inParent, displayExt)
, m_MeshItem(entitem)
......@@ -386,7 +388,6 @@ void qtMeshItemCombo::init()
// this, SLOT(attributeFilterChanged(QStandardItem*)));
this->blockSignals(false);
this->view()->viewport()->installEventFilter(this);
// this->view()->setSelectionMode(QAbstractItemView::ExtendedSelection);
this->updateText();
this->hidePopup();
......@@ -405,31 +406,6 @@ void qtMeshItemCombo::showPopup()
this->qtCheckItemComboBox::showPopup();
}
bool qtMeshItemCombo::eventFilter(QObject* editor, QEvent* evt)
{
if (evt->type() == QEvent::MouseButtonRelease)
{
int index = view()->currentIndex().row();
/*
// with the help of styles, check if checkbox rect contains 'pos'
QMouseEvent* e = dynamic_cast<QMouseEvent*>(evt);
QStyleOptionButton opt;
opt.rect = view()->visualRect(view()->currentIndex());
QRect r = style()->subElementRect(QStyle::SE_ViewItemCheckIndicator, &opt);
if(r.contains(e->pos()))
{
*/
if (itemData(index, Qt::CheckStateRole) == Qt::Checked)
setItemData(index, Qt::Unchecked, Qt::CheckStateRole);
else
setItemData(index, Qt::Checked, Qt::CheckStateRole);
// }
return true;
}
return QObject::eventFilter(editor, evt);
}
void qtMeshItemCombo::itemCheckChanged(const QModelIndex& topLeft, const QModelIndex&)
{
QStandardItemModel* itemModel = qobject_cast<QStandardItemModel*>(this->model());
......
......@@ -33,8 +33,8 @@ class SMTKQTEXT_EXPORT qtCheckableComboItemDelegate : public QStyledItemDelegate
Q_OBJECT
public:
qtCheckableComboItemDelegate(QWidget* owner);
virtual void paint(
QPainter* painter, const QStyleOptionViewItem& option, const QModelIndex& index) const;
void paint(
QPainter* painter, const QStyleOptionViewItem& option, const QModelIndex& index) const override;
};
//A sublcass of QComboBox to set text when hidePopup
......@@ -43,11 +43,14 @@ class SMTKQTEXT_EXPORT qtCheckItemComboBox : public QComboBox
Q_OBJECT
public:
qtCheckItemComboBox(QWidget* parentW, const QString& displayExt);
virtual void hidePopup();
virtual void showPopup();
void hidePopup() override;
void showPopup() override;
virtual void init();
virtual void updateText();
protected:
bool eventFilter(QObject* editor, QEvent* event) override;
private:
QStandardItem* m_displayItem;
QString m_displayTextExt;
......@@ -59,15 +62,12 @@ class SMTKQTEXT_EXPORT qtModelEntityItemCombo : public qtCheckItemComboBox
Q_OBJECT
public:
qtModelEntityItemCombo(qtModelEntityItem* item, QWidget* parent, const QString& displayExt);
virtual void showPopup();
virtual void init();
void showPopup() override;
void init() override;
protected slots:
virtual void itemCheckChanged(const QModelIndex& topLeft, const QModelIndex& bottomRight);
protected:
virtual bool eventFilter(QObject* editor, QEvent* event);
private:
qtModelEntityItem* m_ModelEntityItem;
};
......@@ -78,15 +78,12 @@ class SMTKQTEXT_EXPORT qtMeshItemCombo : public qtCheckItemComboBox
Q_OBJECT
public:
qtMeshItemCombo(qtMeshItem* item, QWidget* parent, const QString& displayExt);
virtual void showPopup();
virtual void init();
void showPopup() override;
void init() override;
protected slots:
virtual void itemCheckChanged(const QModelIndex& topLeft, const QModelIndex& bottomRight);
protected:
virtual bool eventFilter(QObject* editor, QEvent* event);
private:
qtMeshItem* m_MeshItem;
};
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment