//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); } } //////////////////////////////////////////////////////////////////////////////////