【问题标题】:Change vertical header title更改垂直标题标题
【发布时间】:2018-10-22 07:09:31
【问题描述】:

如何将图片上显示的标题更改为“№”。谢谢。

【问题讨论】:

  • 您确定要垂直标题吗?您也可以在表格中添加一列“№”。
  • 是的,但是,当我从 1,2,3,4,5 中删除第 2,5 行时? № 1,3,4 没有排序?我试过这个。但是,我不想这样做。

标签: c++ qt qt5 qtableview


【解决方案1】:

该小部件是从QAbstractButton 继承的QTableCornerButton 类的对象,但它是不使用文本的私有Qt API 的一部分,因此您不能使用@987654326 的setText() @,所以另一种选择是建立一个QLabel,上面的布局:

#include <QtWidgets>

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    QTableView w;
    QStandardItemModel model(10, 10);
    w.setModel(&model);
    QAbstractButton *button =  w.findChild<QAbstractButton *>();
    if(button){
        QVBoxLayout *lay = new QVBoxLayout(button);
        lay->setContentsMargins(0, 0, 0, 0);
        QLabel *label = new QLabel("№");
        label->setContentsMargins(0, 0, 0, 0);
        lay->addWidget(label);
    }
    w.show();
    return a.exec();
}

【讨论】:

  • @sherox Qt Designer 是有限的,但你必须在 .cpp 中执行相同的逻辑,将 w.findChild&lt;QAbstractButton *&gt;() 更改为 ui-&gt;tableView-&gt;findChild&lt;QAbstractButton *&gt;()
  • 感谢您的回复。我正在使用 Qt 设计器。我有 QSqlQueryModel 和 tableView。我正在使用此代码“model->setHeaderData(0, Qt::Horizo​​ntal, QObject::tr("Title"));"为了它。我想更改垂直标题标题,如上所示。只需更改文本。
  • @sherox 正如您所指出的,您在红色矩形中标记的内容不是模型处理的,假设您的 QTableView 是 ui-&gt;tableView 仅在 Qt 生成的类的构造函数中添加以下内容设计师:: QAbstractButton *button = ui-&gt;tableView-&gt;findChild&lt;QAbstractButton *&gt;(); if(button){ QVBoxLayout *lay = new QVBoxLayout(button); lay-&gt;setContentsMargins(0, 0, 0, 0); QLabel *label = new QLabel("№"); label-&gt;setContentsMargins(0, 0, 0, 0); lay-&gt;addWidget(label); }
  • 成功了,谢谢先生。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-04-13
  • 1970-01-01
  • 2016-10-30
相关资源
最近更新 更多