当前位置:网站首页>Make qcombobox drop down a tree structure list

Make qcombobox drop down a tree structure list

2022-06-09 12:44:00 litanyuan

background

In the process of project development, we need to make QComboBox Drop down a tree list , Directly through setModel and setView Set up combox Control can realize , However, when you click the node arrow button, the drop-down box will also be hidden . So it needs to be realized again QComboBox To achieve their own control .

①. adopt QTreeComboBoxView Subclass QTreeWidget class , To rewrite mousePressEvent Events themselves implement the expansion and contraction of nodes , Otherwise, there will be event conflicts .

②. adopt QTreeListComboBox Subclass QComboBox class , To rewrite hidePopup Method , Judge whether the conditions are met .

QTreeComboBoxView

class QTreeComboBoxView : public QTreeWidget
{
    
  Q_OBJECT
public:
  QTreeComboBoxView(QWidget *parent = Q_NULLPTR) :QTreeWidget(parent) {
    }
  ~QTreeComboBoxView() {
    };
protected:
  void mousePressEvent(QMouseEvent *event) override;
signals:
  void treeMousePressed(bool inItem);
};

void QTreeComboBoxView::mousePressEvent(QMouseEvent *event)
{
    
  auto curIndex = currentIndex();
  auto rect = this->visualRect(curIndex);
  auto buttonRect = QRect(rect.left() - 20, rect.top(), 20, rect.height());
  if (buttonRect.contains(event->pos()))
  {
    
    if (isExpanded(curIndex)) setExpanded(curIndex, false);
    else setExpanded(curIndex, true);
    emit treeMousePressed( true );
  }
  else
    emit treeMousePressed(false);
}

QTreeListComboBox

class QTreeListComboBox : public QComboBox
{
    
  Q_OBJECT  
public:
  QTreeListComboBox( QWidget *parent = Q_NULLPTR);
  ~QTreeListComboBox() {
    };
protected:
  void hidePopup() override;
private:
  bool canHidePopup = true;// Allow shrinkage 
public:
  QTreeComboBoxView * displayTreeWidget = nullptr;
private:
  Ui::QTreeListComboBox ui;
};

QTreeListComboBox::QTreeListComboBox( QWidget *parent)
  : QComboBox(parent)
{
    
  ui.setupUi(this);

  displayTreeWidget = new QTreeComboBoxView(this);
  displayTreeWidget->setSelectionMode(QAbstractItemView::SingleSelection);
  displayTreeWidget->setHeaderHidden(true);

  this->setModel(displayTreeWidget->model());
  this->setView(displayTreeWidget);

  connect(displayTreeWidget, &QTreeComboBoxView::treeMousePressed, [&](bool inItem) {
    
    canHidePopup = !inItem;
  });

  connect(this, static_cast<void(QComboBox::*)(int)>(&QComboBox::activated), this, [&](int index) {
    
    hidePopup();
  });
}

void QTreeListComboBox::hidePopup()
{
    
  if (canHidePopup) QComboBox::hidePopup();    
}

Set the grouping node to disable selection

item->setFlags(item->flags() & ~Qt::ItemIsSelectable);

Code example

auto tree = ui.comboBox->displayTreeWidget;

 for (int i = 1;i < 4;i++ )
 {
    
    QTreeWidgetItem* item = new QTreeWidgetItem(tree, {
     QString::number(i) });
    item->setFlags(item->flags() & ~Qt::ItemIsSelectable);
    for (int j = 1; j < 5; j++)
    {
    
      QTreeWidgetItem* sub = new QTreeWidgetItem(item, {
     QString("%1.%2").arg(i).arg(j) });
    }  
 }

 Insert picture description here
 Insert picture description here

原网站

版权声明
本文为[litanyuan]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/160/202206091152532096.html