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

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

这次继续和大家分享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 

#include 


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); }

最后,学不可以已!