32

Qt ModelView教程——设置表头与可编辑Table

 5 years ago
source link: http://mp.weixin.qq.com/s?__biz=MzUxMTk4MzY3MA%3D%3D&%3Bmid=2247484281&%3Bidx=1&%3Bsn=91618cf434fc4536eb3474ff8959f353
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

点击上方蓝字可直接关注!方便下次阅读。如果对你有帮助,可以点个在看,让它可以帮助到更多老铁~

这篇文章是在高铁上写的。

这次继续和大家分享Qt Model/View的一些使用方法。 Qt 帮助文档的整体目录如下:

MveIZfN.png!web

一、 设置 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();

}

效果如下:

MVFv2eq.png!web

二、 可编辑 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);


}

效果如下:

MZJb2ii.png!web

fi6ZbeJ.png!web

三、 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);

}

最后,学不可以已!

MZzeIjR.png!web


Recommend

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK