注意:需要用 Qt 5 跑。Qt 4 要稍微改一下 .pro
檔。
Last active
September 17, 2017 20:48
-
-
Save uranusjr/6a946151d8a1efb85996 to your computer and use it in GitHub Desktop.
用 QTableView 與 QAbstractTableModel 與 Qt 的 Model-View 概念做出類似檔案總管的效果 https://www.ptt.cc/bbs/C_and_CPP/M.1435018167.A.85C.html
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <QApplication> | |
#include <QStyle> | |
#include "listtablemodel.h" | |
static const int gMaxRowCount = 25; | |
ListTableModel::ListTableModel(const QStringList &list, QObject *parent) : | |
QAbstractTableModel(parent), _items(list) | |
{ | |
} | |
int ListTableModel::columnCount(const QModelIndex &) const | |
{ | |
if (_items.size() < 1) | |
return 0; | |
// 仔細想一下規則,應該可以理解為什麼列數是這樣算。 | |
return (_items.size() - 1) / gMaxRowCount + 1; | |
} | |
int ListTableModel::rowCount(const QModelIndex &) const | |
{ | |
// 如果一列就放得完,那 table 就只需要那麼多行。 | |
if (_items.size() <= gMaxRowCount) | |
return _items.size(); | |
// 否則固定最多行數。 | |
return gMaxRowCount; | |
} | |
QVariant ListTableModel::data(const QModelIndex &index, int role) const | |
{ | |
// 按照你貼圖的需求,這邊需要設兩個東西: | |
// 1. 前面的 icon - Qt::DecorationRole | |
// 2. 後面的文字 - Qt::DisplayRole | |
// 這裡我就固定前面 icon 都使用標準的 directory icon,你自己照需求改。 | |
int i = index.column() * gMaxRowCount + index.row(); | |
if (i >= _items.size()) | |
return QVariant(); | |
switch (role) | |
{ | |
case Qt::DecorationRole: | |
return QApplication::style()->standardIcon(QStyle::SP_DirIcon); | |
case Qt::DisplayRole: | |
return _items.at(i); | |
default: | |
return QVariant(); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#ifndef LISTTABLEMODEL_H | |
#define LISTTABLEMODEL_H | |
#include <QAbstractTableModel> | |
#include <QStringList> | |
class ListTableModel : public QAbstractTableModel | |
{ | |
public: | |
// 改傳入任何你想的 list。 | |
explicit ListTableModel(const QStringList &list, QObject *parent = 0); | |
int columnCount(const QModelIndex &parent = QModelIndex()) const; | |
int rowCount(const QModelIndex &parent = QModelIndex()) const; | |
QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const; | |
private: | |
QStringList _items; | |
}; | |
#endif // LISTTABLEMODEL_H |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
QT = core gui widgets | |
HEADERS = \ | |
listtablemodel.h | |
SOURCES = \ | |
listtablemodel.cpp \ | |
main.cpp |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <QApplication> | |
#include <QHeaderView> | |
#include <QStringList> | |
#include <QTableView> | |
#include "listtablemodel.h" | |
int main(int argc, char *argv[]) | |
{ | |
QApplication app(argc, argv); | |
QStringList names = | |
QStringList() << "楠梓區" << "左營區" << "鼓山區" << "三民區" << "鹽埕區" | |
<< "前金區" << "新興區" << "苓雅區" << "前鎮區" << "旗津區" | |
<< "小港區" << "鳳山區" << "大寮區" << "鳥松區" << "林園區" | |
<< "仁武區" << "大樹區" << "大社區" << "岡山區" << "路竹區" | |
<< "橋頭區" << "梓官區" << "彌陀區" << "永安區" << "燕巢區" | |
<< "田寮區" << "阿蓮區" << "茄萣區" << "湖內區" << "旗山區" | |
<< "美濃區" << "內門區" << "杉林區" << "甲仙區" << "六龜區" | |
<< "茂林區" << "桃源區" << "那瑪夏區"; | |
ListTableModel model(names); | |
QTableView table; | |
table.setGridStyle(Qt::NoPen); // 把邊框消掉。 | |
table.horizontalHeader()->hide(); // 把標題列隱藏。 | |
table.verticalHeader()->hide(); // 把標題列隱藏。 | |
table.setModel(&model); | |
table.show(); | |
return app.exec(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment