<> one , sketch

QTableWidget It's a control that we often use . because Table It can show the data clearly , Operation data , So in the process of using, we need to table There are many settings to meet our needs ,table It's actually a combination of multiple controls , For example, there is a horizontal meter , Vertical meter , scroll bar , Intermediate form, etc .

Table The style of , There are many interfaces , Please refer to Qt Assistant . It's very detailed . Here's how to Table Add custom on header CheckBox.

<> two , Code Road

The code is simple , rewrite QHeaderView class , Then set it to the corresponding Table Medium .

<> rewrite QHeaderView class
class CheckBoxHeaderView : public QHeaderView { Q_OBJECT public:
CheckBoxHeaderView(int checkColumnIndex, Qt::Orientation orientation, QWidget *
parent = 0) : QHeaderView(orientation, parent) { // default ComboBox; m_comboBox =
new QComboBox(this); m_comboBox->addItems(QStringList() << "123" << "456" <<
"789"); } // Get current comboBox written words ; QString getCurrentComboBoxText() { return
m_comboBox->currentText(); } // Set custom ComboBox; void
setComboBoxObject(QComboBox* object) { m_comboBox = object;
m_comboBox->setParent(this); } protected: void paintSection(QPainter *painter,
const QRect &rect, int logicalIndex) const { if (logicalIndex == 0) {
m_comboBox->setGeometry(rect); } else { QHeaderView::paintSection(painter,
rect, logicalIndex); } } private: QComboBox * m_comboBox; };
<> Simple test
class MyTableWidgetWidthComboBox : public QTableWidget { Q_OBJECT public:
MyTableWidgetWidthComboBox(QWidget *parent = Q_NULLPTR); private:
CheckBoxHeaderView * m_checkBoxHeaderView; };
MyTableWidgetWidthComboBox::MyTableWidgetWidthComboBox(QWidget *parent) :
QTableWidget(parent) { this->setAlternatingRowColors(true);
this->setColumnCount(3);
this->setSelectionMode(QAbstractItemView::SingleSelection);
this->setEditTriggers(QAbstractItemView::NoEditTriggers);
this->setSelectionBehavior(QAbstractItemView::SelectRows); // Custom header
m_checkBoxHeaderView = new CheckBoxHeaderView(0, Qt::Horizontal, this); //
custom ComboBox; QComboBox* comboBox = new QComboBox;
comboBox->addItems(QStringList() << "abc" << "def" << "789");
m_checkBoxHeaderView->setComboBoxObject(comboBox); // Set the header ;
this->setHorizontalHeader(m_checkBoxHeaderView);
this->setHorizontalHeaderLabels(QStringList() << "1" << "2" << "3"); }
<> design sketch

Technology