更改 QHeaderView 数据 [英] Change QHeaderView data

查看:36
本文介绍了更改 QHeaderView 数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试修改 QTableWidget 中 QHeaderView (Horizo​​ntal) 的文本.

I try to modify the text of my QHeaderView (Horizontal) in my QTableWidget.

第一个问题:是否可以像 QTableWidgetItem 一样将其设置为可编辑?

First question: Is it possible to set it editable like a QTableWidgetItem ?

第二个问题:如果不可能,我该怎么做,我尝试像这样重新绘制它:

Second question: If it's not possible, how can I do that, I tried to repaint it like this:

void EditableHeaderView::paintSection(QPainter *painter, const QRect &rect, int logicalIndex) const
{
    painter->save();
    QHeaderView::paintSection(painter, rect, logicalIndex);
    painter->restore();

    painter->setPen(Qt::SolidLine);
    painter->drawText(rect, Qt::AlignCenter, m_sValues[logicalIndex]);
}

但是标题索引被绘制在我的文本后面.

But the header index is painted behind my text.

我尝试过的另一个解决方案是:

Another solution that I tried is:

void EditableHeaderView::mySectionDoubleClicked( int section )
{
    if (section != -1) // Not on a section
        m_sValues[section] = QInputDialog::getText(this, tr("Enter a value"), tr("Enter a value"), QLineEdit::Normal, "");

    QAbstractItemModel* model = this->model();
    model->setHeaderData(section, this->orientation(), m_sValues[section]);
    this->setModel(model);
}

但这不起作用...

我希望有人有解决方案.

I hope someone have a solution.

谢谢!

推荐答案

我不知道为什么您的解决方案不起作用,但我找到了一个非常简单的解决方法:

I don't know why your solution doesn't works but I found a very simple workaround:

QString res =  QInputDialog::getText(this, tr("Enter a value"), tr("Enter a value"), QLineEdit::Normal, "");
setHorizontalHeaderItem(logicalIndex, new QTableWidgetItem(res));

感谢您的帮助!

这篇关于更改 QHeaderView 数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆