列表标题栏添加CheckBox(自定义HanderView的时候实现)

时间:2023-03-08 20:41:25

前段时间项目上的要求,要实现一个列表(见下图1)。类似网页上的列表,可以通过选中标题栏的复选框,实现全选或者全不选的功能。但是看了很久,都没看到Qt哪个方法可以实现在标题栏添加控件。

列表标题栏添加CheckBox(自定义HanderView的时候实现)

图1

要实现这样的效果,也许我们首先想到的,就是直接生成一个CheckBox,用setGeometry()设置它的位置就可以了。当然这样是可以的,也是最简单的。但是有个问题:这样做,CheckBox就固定死了,而且没有跟标题栏连城一体,不会随着标题栏一起移动。结果如下图2

列表标题栏添加CheckBox(自定义HanderView的时候实现)

显然,这样的效果有点不爽。

后面想到了一种比较好的办法,就是自定义一个heander。通过setHeader()设置给列表。下面是我的部分实现代码

  1. class MyCheckBox:public QCheckBox
  2. {
  3. Q_OBJECT
  4. public:
  5. MyCheckBox(QWidget *parent /*= NULL*/):QCheckBox(parent)
  6. {
  7. }
  8. ~MyCheckBox(){}
  9. protected:
  10. void mouseMoveEvent(QMouseEvent *e)
  11. {
  1. //HeaderView::mouseMoveEvent(e);
  2. QRect boxRect = this->rect();
  3. QPoint pos = e->pos();
  4. if (boxRect.contains(pos))
  5. {
  6. setCursor(Qt::ArrowCursor);
  7. }
  8. }
  9. private:
  10. };
  11. class HanderView :public QHeaderView
  12. {
  13. Q_OBJECT
  14. public:
  15. HanderView( Qt::Orientation orientation,QWidget *parent /*= NULL*/):QHeaderView(orientation,parent)
  16. {
  17. m_pCheckBox = new MyCheckBox(this);
  18. hasPaint = false;
  19. }
  20. ~HanderView()
  21. {
  22. }
  23. private slots:
  24. protected:
  25. void resizeEvent(QResizeEvent *event)
  26. {
  27. int leftPos = this->sectionViewportPosition(0);
  28. m_pCheckBox->setGeometry(leftPos + 5,0,50,this->height());
  29. int sectionMinSize = 50;
  30. this->setMinimumSectionSize(sectionMinSize + sectionSizeFromContents(0).width());
  31. this->setDefaultSectionSize(sectionMinSize + sectionSizeFromContents(0).width());
  32. }
  33. void paintEvent(QPaintEvent *e)
  34. {
  35. QHeaderView::paintEvent(e);
  36. int leftPos = this->sectionViewportPosition(0);
  37. m_pCheckBox->setGeometry(leftPos + 5,0,50,this->height());
  38. }
  39. private:
  40. MyCheckBox   *m_pCheckBox;
  41. bool hasPaint;
  42. };
  1. class MyTreeWidget:public QTreeWidget
  2. {
  3. Q_OBJECT
  4. public:
  5. MyTreeWidget(QWidget *parent = NULL);
  6. ~MyTreeWidget();
  7. protected:
  8. private:
  9. HanderView   *m_pHeader;
  10. };
  1. MyTreeWidget::MyTreeWidget(QWidget *parent /* = NULL */):QTreeWidget(parent)
  2. {
  3. m_pHeader = new HanderView(Qt::Horizontal,parent);
  4. this->setHeader(m_pHeader);
  5. QStringList list ;
  6. list<<"文件名"<<"文件大小"<<"文件类型"<<"创建日期";
  7. this->setHeaderLabels(list);
  8. header()->setDefaultAlignment(Qt::AlignCenter);
  9. }

1、这里是为了保证鼠标在标题栏上是箭头状,因为Box靠近分割线,不这么做的话,鼠标移动到Box上面的时候也可能是Qt::SplitHCursor。这样对用户感觉有点不爽。大家可以试试注释这些代码看看就知道了

  1. void mouseMoveEvent(QMouseEvent *e)
  2. {
  1. //HeaderView::mouseMoveEvent(e);
  2. QRect boxRect = this->rect();
  3. QPoint pos = e->pos();
  4. if (boxRect.contains(pos))
  5. {
  6. setCursor(Qt::ArrowCursor);
  7. }

2、

  1. int leftPos = this->sectionViewportPosition(0);
  2. m_pCheckBox->setGeometry(leftPos + 5,0,50,this->height());
  3. int sectionMinSize = 50;
  4. this->setMinimumSectionSize(sectionMinSize + sectionSizeFromContents(0).width());
  5. this->setDefaultSectionSize(sectionMinSize + sectionSizeFromContents(0).width());

设置列的最小宽度。

  1. sectionSizeFromContents(0).width()这个可以根据标题栏的字符的长度调整列的宽度。

3、

  1. int leftPos = this->sectionViewportPosition(0);
  2. m_pCheckBox->setGeometry(leftPos + 5,0,50,this->height());

这里保证移动滚动条的时候,Box会跟着标题栏动。

效果如图3,图4

列表标题栏添加CheckBox(自定义HanderView的时候实现)列表标题栏添加CheckBox(自定义HanderView的时候实现)

图3                                                                                              图4

4、我们也可以把Box放在其他列,改一下index就可以了

  1. int leftPos = this->sectionViewportPosition(1);
  2. m_pCheckBox->setGeometry(leftPos + 5,0,50,this->height());
  3. int sectionMinSize = 50;
  4. this->setMinimumSectionSize(sectionMinSize + sectionSizeFromContents(1).width());
  5. this->setDefaultSectionSize(sectionMinSize + sectionSizeFromContents(1).width());
  1. int leftPos = this->sectionViewportPosition(1);
  2. m_pCheckBox->setGeometry(leftPos + 5,0,50,this->height());

结果如图5,图6

列表标题栏添加CheckBox(自定义HanderView的时候实现)列表标题栏添加CheckBox(自定义HanderView的时候实现)

图5                                                                                                                                        图6

http://blog.****.net/hai200501019/article/details/9150691

相关文章