内容简介:点击上方蓝字可直接关注!方便下次阅读。如果对你有帮助,可以点个在看,让它可以帮助到更多老铁~这篇文章是在高铁上写的。这次继续和大家分享Qt Model/View的一些使用方法。
点击上方蓝字可直接关注!方便下次阅读。如果对你有帮助,可以点个在看,让它可以帮助到更多老铁~
这篇文章是在高铁上写的。
这次继续和大家分享Qt Model/View的一些使用方法。 Qt 帮助文档的整体目录如下:
一、 设置 Table 的行和列表头
只需在只读表的基础上加上
QVariant headerData(int section, Qt::Orientation orientation, int role) const Q_DECL_OVERRIDE;
并重新实现即可。
QVariant MyModel::headerData(int section, Qt::Orientation orientation, int role) const
{
if (role == Qt::DisplayRole)
{
if (orientation == Qt::Horizontal)
{
switch (section)
{
case 0:
return QString("first");
case 1:
return QString("second");
case 2:
return QString("third");
}
}
if (orientation == Qt::Vertical)
{
switch (section)
{
case 0:
return QString("first");
case 1:
return QString("second");
}
}
}
return QVariant();
}
效果如下:
二、 可编辑 Table 的实现
为了让之前只读表具备可编辑的功能,需要重新实现两个虚方法 setData() and flags() 。
使用一个 QString 类型的二维数组来存储数据,并且当编辑完单元格内容时,向 window title 发送文本信息,使得 window title 随着单元格内容改变而改变。
#include <QAbstractTableModel>
#include <QString>
const int COLS= 3;
const int ROWS= 2;
class MyModel : public QAbstractTableModel
{
Q_OBJECT
public:
MyModel(QObject *parent);
int rowCount(const QModelIndex &parent = QModelIndex()) const Q_DECL_OVERRIDE ;
int columnCount(const QModelIndex &parent = QModelIndex()) const Q_DECL_OVERRIDE;
QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const Q_DECL_OVERRIDE;
bool setData(const QModelIndex & index, const QVariant & value, int role = Qt::EditRole) Q_DECL_OVERRIDE;
Qt::ItemFlags flags(const QModelIndex & index) const Q_DECL_OVERRIDE ;
private:
QString m_gridData[ROWS][COLS]; //holds text entered into QTableView
signals:
void editCompleted(const QString &);
};
每次编辑单元格的时候 setData() 就会被调用。 index 参数会告诉我们具体哪个单元格被编辑、 value 参数可以让我们获得单元格内具体的内容
bool MyModel::setData(const QModelIndex & index, const QVariant & value, int role)
{
if (role == Qt::EditRole)
{
//save value from editor to member m_gridData
m_gridData[index.row()][index.column()] = value.toString();
//for presentation purposes only: build and emit a joined string
QString result;
for (int row= 0; row < ROWS; row++)
{
for(int col= 0; col < COLS; col++)
{
result += m_gridData[row][col] + " ";
}
}
emit editCompleted( result );
}
return true;
}
各种属性在 flags() 函数中调整。这两个属性 Qt::ItemIsSelectable | Qt::ItemIsEditable 足够我们这次使用了。
Qt::ItemFlags MyModel::flags(const QModelIndex &index) const
{
qDebug() << index.row() << index.column();
return Qt::ItemIsEditable | QAbstractTableModel::flags(index);
}
效果如下:
三、 MainWindow 中的设置
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
{
tableView = new QTableView(this);
setCentralWidget(tableView);
QAbstractTableModel *myModel = new MyModel(this);
tableView->setModel(myModel);
//transfer changes to the model to the window title
connect(myModel, SIGNAL(editCompleted(const QString &)), this, SLOT(setWindowTitle(const QString &)));
}
void MainWindow::showWindowTitle(const QString & title)
{
setWindowTitle(title);
}
最后,学不可以已!
以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,也希望大家多多支持 码农网
本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们。
HTML5移动应用开发入门经典
凯瑞恩 / 林星 / 人民邮电出版社 / 2013-3 / 55.00元
《HTML5移动应用开发入门经典》总共分为24章,以示例的方式对如何使用HTML5及相关技术进行移动应用开发做了全面而细致的介绍。《HTML5移动应用开发入门经典》首先讲解了HTML5的起源以及它为什么适用于移动设备,然后讲解了HTML5的基本元素以及所做的改进、canvas(画布)、视音频、微格式、微数据、拖曳等新增特性,还讲解了WebSocket、WebWorkers、Web存储、离线Web应......一起来看看 《HTML5移动应用开发入门经典》 这本书的介绍吧!