TableView 的标题 [英] Header to a TableView
本文介绍了TableView 的标题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我到处浏览,但找不到有关如何在 Qt Creator 中为 TableView 创建某种类型的标题的任何信息.
I've been browsing everywhere and I just cannot find any information on how to create a certain type of header to a TableView in Qt Creator.
我希望它看起来像这样:
I want it to look similar to this:
推荐答案
简短回答:QTCreator 中没有设置可用于定义表视图的标题...
short answer: there is no settings in the QTCreator that you can set to define the Header of a table view...
长答案:那是一个带有自定义模型的 TableView.然后你需要定义一个继承 QAbstractTableModel 的新模型
long answer: That is a TableView with custom model. you need then to define a new Model that inherits the QAbstractTableModel
然后在 FooModel 头文件中覆盖 headerData 方法
and then in tne FooModel header override the headerData method
class FooModel : public QAbstractTableModel
{
Q_OBJECT
//...
QVariant headerData(int section, Qt::Orientation orientation, int role) const override;
//... more methods may be here
然后在 cpp 中:
QVariant FooModel::headerData(int section, Qt::Orientation orientation, int role) const
{
if (role == Qt::DisplayRole)
{
switch (section)
{
case 0:
return QString("Name");
case 1:
return QString("ID");
case 2:
return QString("HexID");
// etc etc
}
}
return QVariant();
}
最后在控制器中:
myFooModel = new FooModel(this);
ui->myTableView->setModel(myFooModel);
这篇关于TableView 的标题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文