ComboBox.cpp 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. #include "ComboBox.h"
  2. #include <QPainter>
  3. #include <QDebug>
  4. ComboListModel::ComboListModel(QObject *parent) : QAbstractListModel(parent) { }
  5. ComboListModel::~ComboListModel() { }
  6. QVariant ComboListModel::data(const QModelIndex &index, int role) const
  7. {
  8. if (Qt::DisplayRole == role) {
  9. return m_infoList.at(index.row()).name;
  10. } else if (Qt::DecorationRole == role) {
  11. return m_infoList.at(index.row()).icon;
  12. } else if (Qt::UserRole == role) {
  13. return m_infoList.at(index.row()).number;
  14. } else if (Qt::UserRole + 1 == role) {
  15. return m_infoList.at(index.row()).isonline;
  16. }
  17. return QVariant();
  18. }
  19. int ComboListModel::rowCount(const QModelIndex & /*parent*/) const
  20. {
  21. return m_infoList.size();
  22. }
  23. void ComboListModel::setModelData(const QList<UserInfo> &data)
  24. {
  25. if (!m_infoList.isEmpty())
  26. m_infoList.clear();
  27. m_infoList = data;
  28. }
  29. ComboListDelegate::ComboListDelegate(QObject *parent) : QStyledItemDelegate(parent) { }
  30. ComboListDelegate::~ComboListDelegate() { }
  31. void ComboListDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
  32. {
  33. if (index.isValid()) {
  34. painter->save();
  35. QString name = index.data(Qt::DisplayRole).toString();
  36. QVariant icondata = index.data(Qt::DecorationRole);
  37. QIcon icon = icondata.value<QIcon>();
  38. QString number = index.data(Qt::UserRole).toString();
  39. bool isonline = index.data(Qt::UserRole + 1).toBool();
  40. qDebug() << name << number << isonline;
  41. QRectF rect;
  42. rect.setX(option.rect.x());
  43. rect.setY(option.rect.y());
  44. rect.setWidth(option.rect.width() - 1);
  45. rect.setHeight(option.rect.height() - 1);
  46. QPainterPath path;
  47. path.moveTo(rect.topRight() - QPointF(m_radius, 0));
  48. path.lineTo(rect.topLeft() + QPointF(m_radius, 0));
  49. path.quadTo(rect.topLeft(), rect.topLeft() + QPointF(0, m_radius));
  50. path.lineTo(rect.bottomLeft() + QPointF(0, -m_radius));
  51. path.quadTo(rect.bottomLeft(), rect.bottomLeft() + QPointF(m_radius, 0));
  52. path.lineTo(rect.bottomRight() - QPointF(m_radius, 0));
  53. path.quadTo(rect.bottomRight(), rect.bottomRight() + QPointF(0, -m_radius));
  54. path.lineTo(rect.topRight() + QPointF(0, m_radius));
  55. path.quadTo(rect.topRight(), rect.topRight() + QPointF(-m_radius, -0));
  56. if (option.state.testFlag(QStyle::State_Selected)) {
  57. painter->setPen(QPen(Qt::blue));
  58. painter->setBrush(QColor(0, 255, 127));
  59. painter->drawPath(path);
  60. } else if (option.state.testFlag(QStyle::State_Raised)) {
  61. painter->setPen(QPen(Qt::green));
  62. painter->setBrush(QColor(0, 127, 255));
  63. painter->drawPath(path);
  64. } else {
  65. painter->setPen(QPen(Qt::gray));
  66. painter->setBrush(QColor(255, 255, 255));
  67. painter->drawPath(path);
  68. }
  69. QRect iconRect = QRect(rect.left() + 3, rect.top() + 3, 30, 30);
  70. QRect nameRect = QRect(iconRect.right() + 5, rect.top() + 3, rect.width() - 80, 20);
  71. QRect circle = QRect(nameRect.right() + 5, nameRect.top(), 10, 10);
  72. QRect numberRect = QRect(nameRect.x(), nameRect.y() + 20, 100, 20);
  73. qDebug() << iconRect << nameRect << circle << numberRect;
  74. painter->drawEllipse(circle);
  75. painter->setPen(QPen(QColor(0, 0, 0)));
  76. painter->setFont(QFont("微软雅黑", 9, QFont::Bold));
  77. painter->drawText(nameRect, Qt::AlignLeft, name);
  78. painter->drawText(numberRect, Qt::AlignLeft, number);
  79. painter->drawPixmap(iconRect, icon.pixmap(iconRect.size()));
  80. painter->restore();
  81. } else {
  82. qDebug() << "index is vaild?";
  83. }
  84. }
  85. QSize ComboListDelegate::sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const
  86. {
  87. Q_UNUSED(option)
  88. Q_UNUSED(index)
  89. return QSize(200, 40);
  90. }
  91. ComboTableModel::ComboTableModel(QObject *parent) : QAbstractTableModel(parent), m_column(1) { }
  92. QVariant ComboTableModel::data(const QModelIndex &index, int role) const
  93. {
  94. int row = index.row();
  95. int col = index.column();
  96. int offset = row * m_column + col;
  97. if ((row < 0) || (col < 0) || (offset >= m_infoList.count())) {
  98. return QVariant();
  99. }
  100. // Q_UNUSED(role);
  101. if (role == Qt::DisplayRole) {
  102. return m_infoList.at(offset).name;
  103. } else if (role == Qt::FontRole) {
  104. return QFont("微软雅黑", 15);
  105. } else if (role == Qt::DecorationRole) {
  106. return m_infoList.at(offset).icon;
  107. } else if (role == Qt::TextAlignmentRole) {
  108. return int(Qt::AlignHCenter | Qt::AlignVCenter);
  109. } else if (role == Qt::TextColorRole) {
  110. return QColor(255, 0, 0);
  111. } else if (Qt::UserRole == role) {
  112. return m_infoList.at(offset).number;
  113. } else if (Qt::UserRole + 1 == role) {
  114. return m_infoList.at(offset).isonline;
  115. }
  116. return QVariant();
  117. }
  118. // rowCount和columnCount必须重写
  119. int ComboTableModel::rowCount(const QModelIndex &parent) const
  120. {
  121. Q_UNUSED(parent);
  122. return (m_infoList.count() + m_column - 1) / m_column;
  123. }
  124. int ComboTableModel::columnCount(const QModelIndex &parent) const
  125. {
  126. Q_UNUSED(parent);
  127. return m_column;
  128. }
  129. QVariant ComboTableModel::headerData(int section, Qt::Orientation orientation, int role) const
  130. {
  131. return QVariant();
  132. }
  133. void ComboTableModel::setModelData(const QList<UserInfo> &data, int column)
  134. {
  135. if (!m_infoList.isEmpty())
  136. m_infoList.clear();
  137. if (data.count() < column) {
  138. m_column = data.count();
  139. } else {
  140. m_column = column;
  141. }
  142. m_infoList = data;
  143. }
  144. Qt::ItemFlags ComboTableModel::flags(const QModelIndex &index) const
  145. {
  146. int row = index.row();
  147. int col = index.column();
  148. int offset = row * m_column + col;
  149. if ((row < 0) || (col < 0) || (offset >= m_infoList.count())) {
  150. return Qt::NoItemFlags;
  151. }
  152. return QAbstractTableModel::flags(index);
  153. }
  154. ComboBox::ComboBox(QWidget *parent) : QComboBox(parent) { }
  155. ComboBoxMenu::ComboBoxMenu(QWidget *parent) : QWidget(parent) { }