How can I insert a checkbox into the header of my view?
文章转自王牌软件
站长推荐:NSetup一键部署软件
一键式完成美化安装包制作,自动增量升级,数据统计,数字签名。应对各种复杂场景,脚本模块化拆分,常规复杂的脚本代码,图形化设置。无需专业的研发经验,轻松完成项目部署。(www.nsetup.cn)
只回答业务咨询
站长推荐:NSetup一键部署软件
一键式完成美化安装包制作,自动增量升级,数据统计,数字签名。应对各种复杂场景,脚本模块化拆分,常规复杂的脚本代码,图形化设置。无需专业的研发经验,轻松完成项目部署。(www.nsetup.cn)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 |
//class MyHeader : public QHeaderView //myheader.h #ifndef MYHEADER_H #define MYHEADER_H #include #include //enum{CHECKBOX_ON,CHECKBOX_NOCHANGE,CHECKBOX_OFF}; class MyHeader : public QHeaderView { Q_OBJECT public: MyHeader(Qt::Orientation orientation,QWidget *parent); ~MyHeader(); protected: void paintSection(QPainter *painter, const QRect &rect, int logicalIndex) const; void mousePressEvent(QMouseEvent *event); void mouseDoubleClickEvent(QMouseEvent *event); void mouseMoveEvent(QMouseEvent *e); void leaveEvent ( QEvent * e); bool insideCheckBox(const QRect &rect, const QPoint &pos) const; QSize sizeHint() const; QSize sectionSizeFromContents(int logicalIndex) const; public: void setCheckState(Qt::CheckState state); void setCheckBoxVisible(bool visible); private: bool m_visible; Qt::CheckState m_state; signals: void theHeadPressed(Qt::CheckState m_state); }; #endif // MYHEADER_H /////////////////////////////////////////////////////////////////////////////////// //myheader.cpp #include "myheader.h" #include #define UNIVERSAL_PADDING 3 MyHeader::MyHeader(Qt::Orientation orientation,QWidget *parent) : QHeaderView(orientation,parent) { } MyHeader::~MyHeader() { } void MyHeader::paintSection(QPainter *painter, const QRect &rect, int logicalIndex) const { qDebug()<<"Brush:"; painter->save(); QHeaderView::paintSection(painter, rect, logicalIndex); painter->restore(); if (logicalIndex == 0 && m_visible) { QStyleOptionButton option; option.rect = rect; if (QApplication::isRightToLeft()) { option.rect.setRight(rect.right() - UNIVERSAL_PADDING); } else { option.rect.setLeft(rect.left() + UNIVERSAL_PADDING); } option.rect.setLeft(rect.left() + UNIVERSAL_PADDING); qDebug()<<"m_state:"<subElementRect(QStyle::SE_CheckBoxIndicator, &option); if (insideCheckBox(rect, pos)) { option.state |= QStyle::State_HasFocus; } painter->save(); this->style()->drawControl(QStyle::CE_CheckBox, &option, painter); painter->restore(); } } void MyHeader::mouseMoveEvent(QMouseEvent *event) { headerDataChanged(Qt::Horizontal, 0, 0); QHeaderView::mouseMoveEvent(event); } void MyHeader::leaveEvent(QEvent *event) { headerDataChanged(Qt::Horizontal, 0, 0); QHeaderView::leaveEvent(event); // kDebug(); } void MyHeader::mousePressEvent(QMouseEvent *event) { if (!m_visible) { return; } const QStyle *style = QApplication::style(); QStyleOptionButton option; option.rect.setSize(sizeHint()); option.rect.setWidth(viewport()->width()); QRect rect = style->subElementRect(QStyle::SE_CheckBoxIndicator, &option); QPoint pos = mapFromGlobal(QCursor::pos()); if (insideCheckBox(rect, pos)) { if (m_state == Qt::Checked) { m_state = Qt::Unchecked; } else { m_state = Qt::Checked; } emit theHeadPressed(m_state); headerDataChanged(Qt::Horizontal, 0, 0); } else { QHeaderView::mousePressEvent(event); } } void MyHeader::mouseDoubleClickEvent(QMouseEvent *event) { if (!m_visible) { return; } const QStyle *style = QApplication::style(); QStyleOptionButton option; option.rect.setSize(sizeHint()); option.rect.setWidth(viewport()->width()); QRect rect = style->subElementRect(QStyle::SE_CheckBoxIndicator, &option); QPoint pos = mapFromGlobal(QCursor::pos()); if (insideCheckBox(rect, pos)) { if (m_state == Qt::Checked) { m_state = Qt::Unchecked; } else { m_state = Qt::Checked; } emit theHeadPressed(m_state); headerDataChanged(Qt::Horizontal, 0, 0); } else { QHeaderView::mouseDoubleClickEvent(event); } } bool MyHeader::insideCheckBox(const QRect &rect, const QPoint &pos) const { // kDebug() << rect << pos; if ((pos.x() >= rect.x() && (pos.x() <= rect.x() + rect.width())) && (pos.y() >= rect.y() && (pos.y() <= rect.y() + rect.height()))) { return true; } return false; } QSize MyHeader::sizeHint() const { const QStyle *style = QApplication::style(); QStyleOptionButton option; QRect rect = style->subElementRect(QStyle::SE_CheckBoxIndicator, &option); QSize size = QHeaderView::sizeHint(); // kDebug() << size << rect; if (size.height() < (rect.height() + 2 * UNIVERSAL_PADDING)) { size.setHeight(rect.height() + 2 * UNIVERSAL_PADDING); } return size; } QSize MyHeader::sectionSizeFromContents(int logicalIndex) const { QSize size = QHeaderView::sectionSizeFromContents(logicalIndex); if (logicalIndex == 0) { const QStyle *style = QApplication::style(); QStyleOptionButton option; QRect rect = style->subElementRect(QStyle::SE_CheckBoxIndicator, &option); QString text = model()->headerData(0, Qt::Horizontal).toString(); QFontMetrics metric = QFontMetrics(QFont()); int textSize = metric.width(text); int minimunSize = textSize + 2 * (rect.width() + 2 * (UNIVERSAL_PADDING + 1)); if (size.width() < minimunSize) { size.setWidth(minimunSize); } } return size; } void MyHeader::setCheckState(Qt::CheckState state) { m_state = state; headerDataChanged(Qt::Horizontal, 0, 0); } void MyHeader::setCheckBoxVisible(bool visible) { m_visible = visible; headerDataChanged(Qt::Horizontal, 0, 0); } ////////////////////////////////////////////////////////////////////////////////// //test_tablewidget.h #ifndef TEST_TABLEWIDGET_H #define TEST_TABLEWIDGET_H #include #include "ui_test_tablewidget.h" #include "myheader.h" class test_tableWidget : public QMainWindow { Q_OBJECT public: test_tableWidget(QWidget *parent = 0, Qt::WFlags flags = 0); ~test_tableWidget(); private: Ui::test_tableWidgetClass ui; MyHeader *myHeader; Qt::CheckState getAllCheckBoxStetus(); private slots: void on_pushButton_clicked(); void tableWidgetPressed(); void changeAllCheckBoxCheckStetus(Qt::CheckState m_state); }; #endif // TEST_TABLEWIDGET_H ////////////////////////////////////////////////////////////////////////////////////////// //test_tablewidget.cpp #include "test_tablewidget.h" #include "taskwidget.h" #include "myheader.h" #include #define ROW_COUNT 6 test_tableWidget::test_tableWidget(QWidget *parent, Qt::WFlags flags) : QMainWindow(parent, flags) { ui.setupUi(this); ui.checkBox11->setCheckState(Qt::PartiallyChecked); //创建行列数 ui.tableWidget->setRowCount(ROW_COUNT); ui.tableWidget->setColumnCount(6); ui.tableWidget->setSelectionBehavior(QAbstractItemView::SelectRows);//正行选中方式 ui.tableWidget->setSelectionMode(QAbstractItemView::ExtendedSelection);//按下Ctrl or Shift可以选中多行 //ui.tableWidget->verticalHeader()->setVisible(false);//隐藏列表头 //设置 大小 ui.tableWidget->resize(350, 250); myHeader = new MyHeader(Qt::Horizontal, ui.tableWidget); connect(myHeader, SIGNAL(theHeadPressed(Qt::CheckState )),this, SLOT(changeAllCheckBoxCheckStetus(Qt::CheckState))); ui.tableWidget->setHorizontalHeader(myHeader); //设置表头文字 QStringList header; header.append(""); header.append("Two"); header.append("Three"); ui.tableWidget->setHorizontalHeaderLabels(header); //connect(ui.tableWidget, SIGNAL(thisCheckBoxPressed()), this,SLOT(tableWidgetPressed())); // 对表头文字的字体、颜色进行设置 // QTableWidgetItem *columnHeaderItem0 = ui.tableWidget->horizontalHeaderItem(0); //获得水平方向表头的Item对象 // columnHeaderItem0->setFont(QFont("Helvetica")); //设置字体 // columnHeaderItem0->setBackgroundColor(QColor(0,60,10)); //设置单元格背景颜色 // columnHeaderItem0->setTextColor(QColor(200,111,30)); //设置文字颜色 //在单元格里加入控件 // QComboBox *comBox = new QComboBox(); // comBox->addItem("Y"); // comBox->addItem("N"); // ui.tableWidget->setCellWidget(0,2,comBox); for (int i = 0;isetCellWidget(i,0,checkBox); } // 单元格设置字体颜色和背景颜色 及字体字符 // QTableWidgetItem *item = new QTableWidgetItem("Apple"); // item->setBackgroundColor(QColor(0, 60, 10)); // item->setTextColor(QColor(200, 111, 100)); // item->setFont(QFont("Helvetica")); // ui.tableWidget->setItem(0, 3, item); //合并单元格效果的实现 //ui.tableWidget->setSpan(3, 1, 4, 1);// 其参数为: 要改变单元格的 1行数 2列数 要合并的 3行数 4列数 //指定 某个行或者列的大小 //ui.tableWidget->setColumnWidth(3,200); //ui.tableWidget->setRowHeight(3,60); //将行和列的大小设为与内容相匹配 //ui.tableWidget->resizeColumnsToContents(); //ui.tableWidget->resizeRowsToContents(); } test_tableWidget::~test_tableWidget() { } void test_tableWidget::on_pushButton_clicked() { taskWidget* task = new taskWidget(); ui.tableWidget->setCellWidget(1,1,task); } Qt::CheckState test_tableWidget::getAllCheckBoxStetus() { int checkedCount = 0; int tableWidgetRowCount = ui.tableWidget->rowCount(); Qt::CheckState m_state; for (int i = 0;icellWidget ( i, 0); QCheckBox *checkBox = (QCheckBox*)widget; if (checkBox->isChecked()) { checkedCount ++; } } if (checkedCount == 0) { return m_state = Qt::Unchecked; } else if (checkedCount < tableWidgetRowCount) { return m_state = Qt::PartiallyChecked; } else { return m_state = Qt::Checked; } } void test_tableWidget::tableWidgetPressed() { myHeader->setCheckState(getAllCheckBoxStetus()); } void test_tableWidget::changeAllCheckBoxCheckStetus(Qt::CheckState m_state) { int tableWidgetRowCount = ui.tableWidget->rowCount(); for (int i = 0;icellWidget ( i, 0); QCheckBox *checkBox = (QCheckBox*)widget; checkBox->setCheckState(m_state); } } ////////////////////////////////////////////////////////////////////////////////// |
学习日记,兼职软件设计,软件修改,毕业设计。
本文出自 学习日记,转载时请注明出处及相应链接。
本文永久链接: https://www.softwareace.cn/?p=424