Qt 自定义可编辑的表头 table HeaderView

双击表头进入编辑状态,示例:

#include <QHeaderView>

/* 自定义表头 双击可编辑 */
class CusEditableHeaderView : public QHeaderView
{
	Q_OBJECT

public:
	explicit CusEditableHeaderView(Qt::Orientation orientation, QWidget * parent = 0)
		: QHeaderView(orientation, parent) {
		//可移动
		setSectionsMovable(true);
        //最后一列顶边
		setStretchLastSection(true);
        //编辑框
		m_lineEdit = new QLineEdit(this);
        //默认隐藏
		m_lineEdit->setHidden(true);

		connect(this, &QHeaderView::sectionDoubleClicked, this, [&](int logicalIdx){
			//双击显示编辑框
            m_lineEdit->setHidden(false);
            //编辑框大小同当前section
			m_lineEdit->resize(sectionSize(logicalIdx),height());
            //编辑框移到当前位置
			m_lineEdit->move(sectionPosition(logicalIdx), 0);
            //获取当前内容
			QVariant var =  model()->headerData(logicalIdx, Qt::Horizontal);
            //填充至编辑框
			m_lineEdit->setText(var.toString());
			m_curSection = logicalIdx;

			/*qDebug() <<CN("逻辑位置(move过后值不变)")<< logicalIdx
				<<CN("item从左到右计算的位置") << sectionPosition(logicalIdx) 
				<< CN("虚拟位置(move过后值改变为新的位置)") << visualIndex(logicalIdx);*/
		});

		connect(m_lineEdit, &QLineEdit::editingFinished, this, [&](){
			m_lineEdit->setHidden(true);
			model()->setHeaderData(m_curSection, Qt::Horizontal, m_lineEdit->text());
			emit signal_edtFshed(m_lineEdit->text());
		});
	}

signals:
	void signal_edtFshed(QString txt);

private:
	QLineEdit *m_lineEdit;
	int m_curSection;
};

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,下面是用Qt定义表头实现表头下拉框的步骤: 1.创建一个自定义表头类,继承自QHeaderView。 2.在自定义表头类的构造函数中,创建一个QComboBox控件,并将其添加到表头中。例如,可以使用以下代码实现: ``` MyHeaderView::MyHeaderView(Qt::Orientation orientation, QWidget *parent) : QHeaderView(orientation, parent) { m_comboBox = new QComboBox(this); m_comboBox->setEditable(true); m_comboBox->setMinimumWidth(100); connect(m_comboBox, SIGNAL(currentIndexChanged(int)), this, SLOT(onComboBoxIndexChanged(int))); } void MyHeaderView::paintSection(QPainter *painter, const QRect &rect, int logicalIndex) const { painter->save(); QHeaderView::paintSection(painter, rect, logicalIndex); if (logicalIndex == m_index) { QRect comboBoxRect = rect.adjusted(4, 4, -4, -4); m_comboBox->setGeometry(comboBoxRect); } painter->restore(); } ``` 其中,m_comboBox是QComboBox的指针成员变量,m_index是当前列的索引号。在paintSection()函数中,先调用父类的paintSection()函数,绘制表头。然后根据当前列的索引号,调整QComboBox的位置和大小。 3.重写mousePressEvent()函数,用于判断用户是否点击了QComboBox控件。例如,可以使用以下代码实现: ``` void MyHeaderView::mousePressEvent(QMouseEvent *event) { QHeaderView::mousePressEvent(event); int index = logicalIndexAt(event->pos()); if (index == m_index) { QRect comboBoxRect = sectionViewportPosition(index).adjusted(4, 4, -4, -4); if (comboBoxRect.contains(event->pos())) { m_comboBox->setGeometry(comboBoxRect); m_comboBox->showPopup(); } } } ``` 其中,logicalIndexAt()函数用于获取用户点击的列索引号,sectionViewportPosition()函数用于获取该列的可视位置。 4.在自定义表头类中添加一个设置下拉框选项的函数,例如: ``` void MyHeaderView::setComboBoxItems(const QStringList &items) { m_comboBox->clear(); m_comboBox->addItems(items); } ``` 在该函数中,使用QComboBox的clear()函数清空选项,然后使用addItems()函数添加新的选项。 5.在自定义表头类中添加一个槽函数,用于处理QComboBox的currentIndexChanged()信号。例如: ``` void MyHeaderView::onComboBoxIndexChanged(int index) { emit comboBoxIndexChanged(m_index, m_comboBox->currentText()); } ``` 在该函数中,使用emit关键字发送comboBoxIndexChanged()信号,该信号包含当前列的索引号和QComboBox的文本内容。 6.在主程序中,创建自定义表头对象,并将其设置为QTableView控件的表头。例如: ``` MyHeaderView *headerView = new MyHeaderView(Qt::Horizontal, ui->tableView); headerView->setComboBoxItems(QStringList() << "Option 1" << "Option 2" << "Option 3"); ui->tableView->setHorizontalHeader(headerView); connect(headerView, SIGNAL(comboBoxIndexChanged(int,QString)), this, SLOT(onComboBoxIndexChanged(int,QString))); ``` 其中,创建自定义表头对象,并设置其下拉框选项。然后,将自定义表头对象设置为QTableView控件的水平表头。最后,将comboBoxIndexChanged()信号连接到主程序中的槽函数,用于处理用户在下拉框中选择的选项。 以上就是使用Qt定义表头实现表头下拉框的基本步骤,希望能对您有所帮助。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值