├── .gitattributes ├── .gitignore ├── README.md ├── TableViewAddButtonDemo ├── TableViewAddButtonDemo.pro ├── TableViewAddButtonDemo.pro.user ├── buttondelegate.cpp ├── buttondelegate.h ├── main.cpp ├── mainwindow.cpp ├── mainwindow.h ├── tablemodel.cpp ├── tablemodel.h ├── tableview.cpp └── tableview.h └── TableViewAddProgressDemo ├── TableViewAddProgressDemo.pro ├── TableViewAddProgressDemo.pro.user ├── main.cpp ├── mainwindow.cpp ├── mainwindow.h ├── progressbardelegate.cpp ├── progressbardelegate.h ├── tablemodel.cpp ├── tablemodel.h ├── tableview.cpp └── tableview.h /.gitattributes: -------------------------------------------------------------------------------- 1 | # Auto detect text files and perform LF normalization 2 | * text=auto 3 | 4 | # Custom for Visual Studio 5 | *.cs diff=csharp 6 | *.sln merge=union 7 | *.csproj merge=union 8 | *.vbproj merge=union 9 | *.fsproj merge=union 10 | *.dbproj merge=union 11 | 12 | # Standard to msysgit 13 | *.doc diff=astextplain 14 | *.DOC diff=astextplain 15 | *.docx diff=astextplain 16 | *.DOCX diff=astextplain 17 | *.dot diff=astextplain 18 | *.DOT diff=astextplain 19 | *.pdf diff=astextplain 20 | *.PDF diff=astextplain 21 | *.rtf diff=astextplain 22 | *.RTF diff=astextplain 23 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Windows image file caches 2 | Thumbs.db 3 | ehthumbs.db 4 | 5 | # Folder config file 6 | Desktop.ini 7 | 8 | # Recycle Bin used on file shares 9 | $RECYCLE.BIN/ 10 | 11 | # Windows Installer files 12 | *.cab 13 | *.msi 14 | *.msm 15 | *.msp 16 | 17 | # ========================= 18 | # Operating System Files 19 | # ========================= 20 | 21 | # OSX 22 | # ========================= 23 | 24 | .DS_Store 25 | .AppleDouble 26 | .LSOverride 27 | 28 | # Icon must ends with two \r. 29 | Icon 30 | 31 | # Thumbnails 32 | ._* 33 | 34 | # Files that might appear on external disk 35 | .Spotlight-V100 36 | .Trashes 37 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # QTableViewAddProgressBar 2 | qt tableview add button and progressbar 3 | -------------------------------------------------------------------------------- /TableViewAddButtonDemo/TableViewAddButtonDemo.pro: -------------------------------------------------------------------------------- 1 | #------------------------------------------------- 2 | # 3 | # Project created by QtCreator 2014-09-06T13:39:09 4 | # 5 | #------------------------------------------------- 6 | 7 | QT += core gui 8 | 9 | greaterThan(QT_MAJOR_VERSION, 4): QT += widgets 10 | 11 | TARGET = TableViewAddButtonDemo 12 | TEMPLATE = app 13 | 14 | 15 | SOURCES += main.cpp\ 16 | mainwindow.cpp \ 17 | tablemodel.cpp \ 18 | tableview.cpp \ 19 | buttondelegate.cpp 20 | 21 | HEADERS += mainwindow.h \ 22 | tablemodel.h \ 23 | tableview.h \ 24 | buttondelegate.h 25 | -------------------------------------------------------------------------------- /TableViewAddButtonDemo/TableViewAddButtonDemo.pro.user: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | ProjectExplorer.Project.ActiveTarget 7 | 0 8 | 9 | 10 | ProjectExplorer.Project.EditorSettings 11 | 12 | true 13 | false 14 | true 15 | 16 | Cpp 17 | 18 | CppGlobal 19 | 20 | 21 | 22 | QmlJS 23 | 24 | QmlJSGlobal 25 | 26 | 27 | 2 28 | UTF-8 29 | false 30 | 4 31 | false 32 | 80 33 | true 34 | true 35 | 1 36 | true 37 | false 38 | 0 39 | true 40 | 0 41 | 8 42 | true 43 | 1 44 | true 45 | true 46 | true 47 | false 48 | 49 | 50 | 51 | ProjectExplorer.Project.PluginSettings 52 | 53 | 54 | 55 | ProjectExplorer.Project.Target.0 56 | 57 | Desktop Qt 5.3.0 MinGW 32bit 58 | Desktop Qt 5.3.0 MinGW 32bit 59 | qt.53.win32_mingw482_kit 60 | 0 61 | 0 62 | 0 63 | 64 | F:/QtDemo/build-TableViewAddButtonDemo-Desktop_Qt_5_3_0_MinGW_32bit-Debug 65 | 66 | 67 | true 68 | qmake 69 | 70 | QtProjectManager.QMakeBuildStep 71 | false 72 | true 73 | 74 | false 75 | 76 | 77 | true 78 | Make 79 | 80 | Qt4ProjectManager.MakeStep 81 | 82 | false 83 | 84 | 85 | 86 | 2 87 | 构建 88 | 89 | ProjectExplorer.BuildSteps.Build 90 | 91 | 92 | 93 | true 94 | Make 95 | 96 | Qt4ProjectManager.MakeStep 97 | 98 | true 99 | clean 100 | 101 | 102 | 1 103 | 清理 104 | 105 | ProjectExplorer.BuildSteps.Clean 106 | 107 | 2 108 | false 109 | 110 | Debug 111 | 112 | Qt4ProjectManager.Qt4BuildConfiguration 113 | 2 114 | true 115 | 116 | 117 | F:/QtDemo/build-TableViewAddButtonDemo-Desktop_Qt_5_3_0_MinGW_32bit-Release 118 | 119 | 120 | true 121 | qmake 122 | 123 | QtProjectManager.QMakeBuildStep 124 | false 125 | true 126 | 127 | false 128 | 129 | 130 | true 131 | Make 132 | 133 | Qt4ProjectManager.MakeStep 134 | 135 | false 136 | 137 | 138 | 139 | 2 140 | 构建 141 | 142 | ProjectExplorer.BuildSteps.Build 143 | 144 | 145 | 146 | true 147 | Make 148 | 149 | Qt4ProjectManager.MakeStep 150 | 151 | true 152 | clean 153 | 154 | 155 | 1 156 | 清理 157 | 158 | ProjectExplorer.BuildSteps.Clean 159 | 160 | 2 161 | false 162 | 163 | Release 164 | 165 | Qt4ProjectManager.Qt4BuildConfiguration 166 | 0 167 | true 168 | 169 | 2 170 | 171 | 172 | 0 173 | 部署 174 | 175 | ProjectExplorer.BuildSteps.Deploy 176 | 177 | 1 178 | 在本地部署 179 | 180 | ProjectExplorer.DefaultDeployConfiguration 181 | 182 | 1 183 | 184 | 185 | 186 | false 187 | false 188 | false 189 | false 190 | true 191 | 0.01 192 | 10 193 | true 194 | 1 195 | 25 196 | 197 | 1 198 | true 199 | false 200 | true 201 | valgrind 202 | 203 | 0 204 | 1 205 | 2 206 | 3 207 | 4 208 | 5 209 | 6 210 | 7 211 | 8 212 | 9 213 | 10 214 | 11 215 | 12 216 | 13 217 | 14 218 | 219 | 2 220 | 221 | TableViewAddButtonDemo 222 | 223 | Qt4ProjectManager.Qt4RunConfiguration:F:/QtDemo/TableViewAddButtonDemo/TableViewAddButtonDemo.pro 224 | 225 | TableViewAddButtonDemo.pro 226 | false 227 | false 228 | 229 | 3768 230 | false 231 | true 232 | false 233 | false 234 | true 235 | 236 | 1 237 | 238 | 239 | 240 | ProjectExplorer.Project.TargetCount 241 | 1 242 | 243 | 244 | ProjectExplorer.Project.Updater.EnvironmentId 245 | {5251c9b7-2e02-4d7c-9918-07dfefc52a31} 246 | 247 | 248 | ProjectExplorer.Project.Updater.FileVersion 249 | 15 250 | 251 | 252 | -------------------------------------------------------------------------------- /TableViewAddButtonDemo/buttondelegate.cpp: -------------------------------------------------------------------------------- 1 | #include "buttondelegate.h" 2 | 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | 10 | ButtonDelegate::ButtonDelegate(QObject *parent) : 11 | QItemDelegate(parent) 12 | { 13 | } 14 | 15 | void ButtonDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const 16 | { 17 | QStyleOptionButton* button = m_btns.value(index); 18 | if (!button) { 19 | button = new QStyleOptionButton(); 20 | button->rect = option.rect.adjusted(4, 4, -4, -4); 21 | button->text = "X"; 22 | button->state |= QStyle::State_Enabled; 23 | 24 | (const_cast(this))->m_btns.insert(index, button); 25 | } 26 | painter->save(); 27 | 28 | if (option.state & QStyle::State_Selected) { 29 | painter->fillRect(option.rect, option.palette.highlight()); 30 | 31 | } 32 | painter->restore(); 33 | QApplication::style()->drawControl(QStyle::CE_PushButton, button, painter); 34 | 35 | 36 | } 37 | 38 | bool ButtonDelegate::editorEvent(QEvent *event, QAbstractItemModel *model, const QStyleOptionViewItem &option, const QModelIndex &index) 39 | { 40 | if (event->type() == QEvent::MouseButtonPress) { 41 | 42 | QMouseEvent* e =(QMouseEvent*)event; 43 | 44 | if (option.rect.adjusted(4, 4, -4, -4).contains(e->x(), e->y()) && m_btns.contains(index)) { 45 | m_btns.value(index)->state |= QStyle::State_Sunken; 46 | } 47 | } 48 | if (event->type() == QEvent::MouseButtonRelease) { 49 | QMouseEvent* e =(QMouseEvent*)event; 50 | 51 | if (option.rect.adjusted(4, 4, -4, -4).contains(e->x(), e->y()) && m_btns.contains(index)) { 52 | m_btns.value(index)->state &= (~QStyle::State_Sunken); 53 | 54 | QDialog *d = new QDialog(); 55 | 56 | d->setGeometry(0, 0, 200, 200); 57 | d->move(QApplication::desktop()->screenGeometry().center() - d->rect().center()); 58 | d->show(); 59 | } 60 | } 61 | } 62 | -------------------------------------------------------------------------------- /TableViewAddButtonDemo/buttondelegate.h: -------------------------------------------------------------------------------- 1 | #ifndef BUTTONDELEGATE_H 2 | #define BUTTONDELEGATE_H 3 | 4 | #include 5 | 6 | class ButtonDelegate : public QItemDelegate 7 | { 8 | Q_OBJECT 9 | public: 10 | explicit ButtonDelegate(QObject *parent = 0); 11 | 12 | void paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const; 13 | bool editorEvent(QEvent *event, QAbstractItemModel *model, const QStyleOptionViewItem &option, const QModelIndex &index); 14 | 15 | signals: 16 | 17 | public slots: 18 | 19 | private: 20 | QMap m_btns; 21 | 22 | }; 23 | 24 | #endif // BUTTONDELEGATE_H 25 | -------------------------------------------------------------------------------- /TableViewAddButtonDemo/main.cpp: -------------------------------------------------------------------------------- 1 | #include "mainwindow.h" 2 | #include 3 | 4 | int main(int argc, char *argv[]) 5 | { 6 | QApplication a(argc, argv); 7 | MainWindow w; 8 | w.show(); 9 | 10 | return a.exec(); 11 | } 12 | -------------------------------------------------------------------------------- /TableViewAddButtonDemo/mainwindow.cpp: -------------------------------------------------------------------------------- 1 | #include "mainwindow.h" 2 | 3 | #include "tableview.h" 4 | #include "tablemodel.h" 5 | 6 | #include 7 | #include 8 | #include 9 | #include 10 | 11 | MainWindow::MainWindow(QWidget *parent) 12 | : QWidget(parent) 13 | { 14 | init(); 15 | } 16 | 17 | MainWindow::~MainWindow() 18 | { 19 | delete tv; 20 | } 21 | 22 | void MainWindow::init() 23 | { 24 | this->resize(800, 600); 25 | tv = new TableView(this); 26 | QVBoxLayout* layout = new QVBoxLayout(); 27 | 28 | layout->addWidget(tv); 29 | this->setLayout(layout); 30 | } 31 | 32 | -------------------------------------------------------------------------------- /TableViewAddButtonDemo/mainwindow.h: -------------------------------------------------------------------------------- 1 | #ifndef MAINWINDOW_H 2 | #define MAINWINDOW_H 3 | 4 | #include 5 | 6 | class TableView; 7 | class QTimer; 8 | 9 | class MainWindow : public QWidget 10 | { 11 | Q_OBJECT 12 | 13 | public: 14 | MainWindow(QWidget *parent = 0); 15 | ~MainWindow(); 16 | 17 | private: 18 | void init(); 19 | 20 | private: 21 | TableView *tv; 22 | }; 23 | 24 | #endif // MAINWINDOW_H 25 | -------------------------------------------------------------------------------- /TableViewAddButtonDemo/tablemodel.cpp: -------------------------------------------------------------------------------- 1 | #include "tablemodel.h" 2 | 3 | TableModel::TableModel(QObject *parent) : 4 | QAbstractTableModel(parent) 5 | { 6 | } 7 | 8 | TableModel::~TableModel() 9 | { 10 | 11 | } 12 | 13 | 14 | int TableModel::rowCount(const QModelIndex &parent) const 15 | { 16 | return m_data.size(); 17 | } 18 | 19 | int TableModel::columnCount(const QModelIndex &parent) const 20 | { 21 | return m_HorizontalHeader.count(); 22 | } 23 | 24 | QVariant TableModel::data(const QModelIndex &index, int role) const 25 | { 26 | if (!index.isValid()) 27 | return QVariant(); 28 | if (role == Qt::DisplayRole) { 29 | int ncol = index.column(); 30 | int nrow = index.row(); 31 | QStringList values = m_data.at(nrow); 32 | if (values.size() > ncol) 33 | return values.at(ncol); 34 | else 35 | return QVariant(); 36 | } 37 | return QVariant(); 38 | } 39 | 40 | Qt::ItemFlags TableModel::flags(const QModelIndex &index) const 41 | { 42 | if (!index.isValid()) 43 | return Qt::NoItemFlags; 44 | 45 | Qt::ItemFlags flag = QAbstractItemModel::flags(index); 46 | 47 | // flag|=Qt::ItemIsEditable // 设置单元格可编辑,此处注释,单元格无法被编辑 48 | return flag; 49 | } 50 | 51 | void TableModel::setHorizontalHeader(const QStringList &headers) 52 | { 53 | m_HorizontalHeader = headers; 54 | } 55 | 56 | 57 | QVariant TableModel::headerData(int section, Qt::Orientation orientation, int role) const 58 | { 59 | if (role == Qt::DisplayRole && orientation == Qt::Horizontal) { 60 | return m_HorizontalHeader.at(section); 61 | } 62 | return QAbstractTableModel::headerData(section, orientation, role); 63 | } 64 | 65 | void TableModel::setData(const QVector &data) 66 | { 67 | m_data = data; 68 | } 69 | -------------------------------------------------------------------------------- /TableViewAddButtonDemo/tablemodel.h: -------------------------------------------------------------------------------- 1 | #ifndef TABLEMODEL_H 2 | #define TABLEMODEL_H 3 | 4 | #include 5 | 6 | class TableModel : public QAbstractTableModel 7 | { 8 | Q_OBJECT 9 | public: 10 | explicit TableModel(QObject *parent = 0); 11 | int rowCount(const QModelIndex &parent) const; 12 | int columnCount(const QModelIndex &parent) const; 13 | QVariant data(const QModelIndex &index, int role) const; 14 | Qt::ItemFlags flags(const QModelIndex &index) const; 15 | void setHorizontalHeader(const QStringList& headers); 16 | QVariant headerData(int section, Qt::Orientation orientation, int role) const; 17 | void setData(const QVector& data); 18 | QVector& DataVector() {return m_data;} 19 | ~TableModel(void); 20 | 21 | signals: 22 | 23 | public slots: 24 | 25 | private: 26 | QStringList m_HorizontalHeader; 27 | QVector m_data; 28 | 29 | }; 30 | 31 | #endif // TABLEMODEL_H 32 | -------------------------------------------------------------------------------- /TableViewAddButtonDemo/tableview.cpp: -------------------------------------------------------------------------------- 1 | #include "tableview.h" 2 | 3 | #include "tablemodel.h" 4 | #include "buttondelegate.h" 5 | 6 | TableView::TableView(QWidget *parent) : 7 | QTableView(parent) 8 | { 9 | iniData(); 10 | } 11 | 12 | TableView::~TableView() 13 | { 14 | delete m_model; 15 | } 16 | 17 | void TableView::iniData() 18 | { 19 | m_model = new TableModel(); 20 | this->setModel(m_model); 21 | QStringList headers; 22 | headers << "Id" << "Progress"; 23 | m_model->setHorizontalHeader(headers); 24 | 25 | QVector data; 26 | data.append(QStringList() << "1" << "22"); 27 | data.append(QStringList() << "2" << "32"); 28 | data.append(QStringList() << "3" << "2"); 29 | data.append(QStringList() << "4" << "80"); 30 | data.append(QStringList() << "5" << "40"); 31 | m_model->setData(data); 32 | 33 | m_buttonDelegate = new ButtonDelegate(this); 34 | this->setItemDelegateForColumn(1, m_buttonDelegate); 35 | emit m_model->layoutChanged(); 36 | this->setColumnWidth(1, 500); 37 | } 38 | -------------------------------------------------------------------------------- /TableViewAddButtonDemo/tableview.h: -------------------------------------------------------------------------------- 1 | #ifndef TABLEVIEW_H 2 | #define TABLEVIEW_H 3 | 4 | #include 5 | 6 | class TableModel; 7 | class ButtonDelegate; 8 | 9 | class TableView : public QTableView 10 | { 11 | Q_OBJECT 12 | public: 13 | explicit TableView(QWidget *parent = 0); 14 | TableModel* tableModel() {return m_model;} 15 | 16 | ~TableView(); 17 | 18 | signals: 19 | 20 | public slots: 21 | 22 | private: 23 | void iniData(); 24 | 25 | private: 26 | TableModel *m_model; 27 | ButtonDelegate *m_buttonDelegate; 28 | 29 | }; 30 | 31 | #endif // TABLEVIEW_H 32 | -------------------------------------------------------------------------------- /TableViewAddProgressDemo/TableViewAddProgressDemo.pro: -------------------------------------------------------------------------------- 1 | #------------------------------------------------- 2 | # 3 | # Project created by QtCreator 2014-09-06T13:39:09 4 | # 5 | #------------------------------------------------- 6 | 7 | QT += core gui 8 | 9 | CONFIG += c++11 10 | greaterThan(QT_MAJOR_VERSION, 4): QT += widgets 11 | 12 | TARGET = TableViewAddProgressDemo 13 | TEMPLATE = app 14 | 15 | 16 | SOURCES += main.cpp\ 17 | mainwindow.cpp \ 18 | progressbardelegate.cpp \ 19 | tablemodel.cpp \ 20 | tableview.cpp 21 | 22 | HEADERS += mainwindow.h \ 23 | progressbardelegate.h \ 24 | tablemodel.h \ 25 | tableview.h 26 | -------------------------------------------------------------------------------- /TableViewAddProgressDemo/TableViewAddProgressDemo.pro.user: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | ProjectExplorer.Project.ActiveTarget 7 | 0 8 | 9 | 10 | ProjectExplorer.Project.EditorSettings 11 | 12 | true 13 | false 14 | true 15 | 16 | Cpp 17 | 18 | CppGlobal 19 | 20 | 21 | 22 | QmlJS 23 | 24 | QmlJSGlobal 25 | 26 | 27 | 2 28 | UTF-8 29 | false 30 | 4 31 | false 32 | 80 33 | true 34 | true 35 | 1 36 | true 37 | false 38 | 0 39 | true 40 | 0 41 | 8 42 | true 43 | 1 44 | true 45 | true 46 | true 47 | false 48 | 49 | 50 | 51 | ProjectExplorer.Project.PluginSettings 52 | 53 | 54 | 55 | ProjectExplorer.Project.Target.0 56 | 57 | Desktop Qt 5.3.0 MinGW 32bit 58 | Desktop Qt 5.3.0 MinGW 32bit 59 | qt.53.win32_mingw482_kit 60 | 0 61 | 0 62 | 0 63 | 64 | F:/QtDemo/build-TableViewAddProgressDemo-Desktop_Qt_5_3_0_MinGW_32bit-Debug 65 | 66 | 67 | true 68 | qmake 69 | 70 | QtProjectManager.QMakeBuildStep 71 | false 72 | true 73 | 74 | false 75 | 76 | 77 | true 78 | Make 79 | 80 | Qt4ProjectManager.MakeStep 81 | 82 | false 83 | 84 | 85 | 86 | 2 87 | 构建 88 | 89 | ProjectExplorer.BuildSteps.Build 90 | 91 | 92 | 93 | true 94 | Make 95 | 96 | Qt4ProjectManager.MakeStep 97 | 98 | true 99 | clean 100 | 101 | 102 | 1 103 | 清理 104 | 105 | ProjectExplorer.BuildSteps.Clean 106 | 107 | 2 108 | false 109 | 110 | Debug 111 | 112 | Qt4ProjectManager.Qt4BuildConfiguration 113 | 2 114 | true 115 | 116 | 117 | F:/QtDemo/build-TableViewAddProgressDemo-Desktop_Qt_5_3_0_MinGW_32bit-Release 118 | 119 | 120 | true 121 | qmake 122 | 123 | QtProjectManager.QMakeBuildStep 124 | false 125 | true 126 | 127 | false 128 | 129 | 130 | true 131 | Make 132 | 133 | Qt4ProjectManager.MakeStep 134 | 135 | false 136 | 137 | 138 | 139 | 2 140 | 构建 141 | 142 | ProjectExplorer.BuildSteps.Build 143 | 144 | 145 | 146 | true 147 | Make 148 | 149 | Qt4ProjectManager.MakeStep 150 | 151 | true 152 | clean 153 | 154 | 155 | 1 156 | 清理 157 | 158 | ProjectExplorer.BuildSteps.Clean 159 | 160 | 2 161 | false 162 | 163 | Release 164 | 165 | Qt4ProjectManager.Qt4BuildConfiguration 166 | 0 167 | true 168 | 169 | 2 170 | 171 | 172 | 0 173 | 部署 174 | 175 | ProjectExplorer.BuildSteps.Deploy 176 | 177 | 1 178 | 在本地部署 179 | 180 | ProjectExplorer.DefaultDeployConfiguration 181 | 182 | 1 183 | 184 | 185 | 186 | false 187 | false 188 | false 189 | false 190 | true 191 | 0.01 192 | 10 193 | true 194 | 1 195 | 25 196 | 197 | 1 198 | true 199 | false 200 | true 201 | valgrind 202 | 203 | 0 204 | 1 205 | 2 206 | 3 207 | 4 208 | 5 209 | 6 210 | 7 211 | 8 212 | 9 213 | 10 214 | 11 215 | 12 216 | 13 217 | 14 218 | 219 | 2 220 | 221 | TableViewAddProgressDemo 222 | 223 | Qt4ProjectManager.Qt4RunConfiguration:F:/QtDemo/TableViewAddProgressDemo/TableViewAddProgressDemo.pro 224 | 225 | TableViewAddProgressDemo.pro 226 | false 227 | false 228 | 229 | 3768 230 | false 231 | true 232 | false 233 | false 234 | true 235 | 236 | 1 237 | 238 | 239 | 240 | ProjectExplorer.Project.TargetCount 241 | 1 242 | 243 | 244 | ProjectExplorer.Project.Updater.EnvironmentId 245 | {5251c9b7-2e02-4d7c-9918-07dfefc52a31} 246 | 247 | 248 | ProjectExplorer.Project.Updater.FileVersion 249 | 15 250 | 251 | 252 | -------------------------------------------------------------------------------- /TableViewAddProgressDemo/main.cpp: -------------------------------------------------------------------------------- 1 | #include "mainwindow.h" 2 | #include 3 | 4 | int main(int argc, char *argv[]) 5 | { 6 | QApplication a(argc, argv); 7 | MainWindow w; 8 | w.show(); 9 | 10 | return a.exec(); 11 | } 12 | -------------------------------------------------------------------------------- /TableViewAddProgressDemo/mainwindow.cpp: -------------------------------------------------------------------------------- 1 | #include "mainwindow.h" 2 | 3 | #include "tableview.h" 4 | #include "tablemodel.h" 5 | 6 | #include 7 | #include 8 | #include 9 | #include 10 | 11 | MainWindow::MainWindow(QWidget *parent) 12 | : QWidget(parent) 13 | { 14 | init(); 15 | initTimer(); 16 | } 17 | 18 | MainWindow::~MainWindow() 19 | { 20 | delete tv; 21 | delete timer; 22 | } 23 | 24 | void MainWindow::init() 25 | { 26 | this->resize(800, 600); 27 | tv = new TableView(this); 28 | QVBoxLayout* layout = new QVBoxLayout(); 29 | 30 | layout->addWidget(tv); 31 | this->setLayout(layout); 32 | //this->layout()->addWidget(tv); 33 | 34 | 35 | } 36 | 37 | void MainWindow::initTimer() 38 | { 39 | timer = new QTimer(this); 40 | timer->setInterval(1000); 41 | connect(timer, SIGNAL(timeout()), this, SLOT(updateProgressValue())); 42 | timer->start(); 43 | } 44 | 45 | void MainWindow::updateProgressValue() 46 | { 47 | TableModel* model = tv->tableModel(); 48 | QVector& data = model->DataVector(); 49 | for (QStringList& v : data) { 50 | int value =v.at(1).toInt(); 51 | qDebug() << value; 52 | if (value < 100) { 53 | value += 2; 54 | qDebug() << value; 55 | v[1] = QString::number(value); 56 | emit model->layoutChanged(); 57 | } 58 | 59 | } 60 | } 61 | -------------------------------------------------------------------------------- /TableViewAddProgressDemo/mainwindow.h: -------------------------------------------------------------------------------- 1 | #ifndef MAINWINDOW_H 2 | #define MAINWINDOW_H 3 | 4 | #include 5 | 6 | class TableView; 7 | class QTimer; 8 | 9 | class MainWindow : public QWidget 10 | { 11 | Q_OBJECT 12 | 13 | public: 14 | MainWindow(QWidget *parent = 0); 15 | ~MainWindow(); 16 | 17 | private: 18 | void init(); 19 | void initTimer(); 20 | 21 | public slots: 22 | void updateProgressValue(); 23 | 24 | private: 25 | TableView *tv; 26 | QTimer *timer; 27 | }; 28 | 29 | #endif // MAINWINDOW_H 30 | -------------------------------------------------------------------------------- /TableViewAddProgressDemo/progressbardelegate.cpp: -------------------------------------------------------------------------------- 1 | #include "progressbardelegate.h" 2 | 3 | #include 4 | #include 5 | 6 | ProgressBarDelegate::ProgressBarDelegate(QObject *parent) : 7 | QItemDelegate(parent) 8 | { 9 | } 10 | 11 | 12 | void ProgressBarDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const 13 | { 14 | if(index.column() == 1) { 15 | int value = index.model()->data(index).toInt(); 16 | QStyleOptionProgressBarV2 progressBarOption; 17 | progressBarOption.rect = option.rect.adjusted(4, 4, -4, -4); 18 | progressBarOption.minimum = 0; 19 | progressBarOption.maximum = 100; 20 | progressBarOption.textAlignment = Qt::AlignRight; 21 | progressBarOption.textVisible = true; 22 | progressBarOption.progress = value; 23 | progressBarOption.text = tr("%1%").arg(progressBarOption.progress); 24 | 25 | painter->save(); 26 | if (option.state & QStyle::State_Selected) { 27 | painter->fillRect(option.rect, option.palette.highlight()); 28 | painter->setBrush(option.palette.highlightedText()); 29 | } 30 | QApplication::style()->drawControl(QStyle::CE_ProgressBar, &progressBarOption, painter); 31 | 32 | painter->restore(); 33 | 34 | } else { 35 | return QItemDelegate::paint (painter, option, index); 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /TableViewAddProgressDemo/progressbardelegate.h: -------------------------------------------------------------------------------- 1 | #ifndef PROGRESSBARDELEGATE_H 2 | #define PROGRESSBARDELEGATE_H 3 | 4 | #include 5 | 6 | class ProgressBarDelegate : public QItemDelegate 7 | { 8 | Q_OBJECT 9 | public: 10 | explicit ProgressBarDelegate(QObject *parent = 0); 11 | void paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const; 12 | 13 | signals: 14 | 15 | public slots: 16 | 17 | 18 | 19 | }; 20 | 21 | #endif // PROGRESSBARDELEGATE_H 22 | -------------------------------------------------------------------------------- /TableViewAddProgressDemo/tablemodel.cpp: -------------------------------------------------------------------------------- 1 | #include "tablemodel.h" 2 | 3 | TableModel::TableModel(QObject *parent) : 4 | QAbstractTableModel(parent) 5 | { 6 | } 7 | 8 | TableModel::~TableModel() 9 | { 10 | 11 | } 12 | 13 | 14 | int TableModel::rowCount(const QModelIndex &parent) const 15 | { 16 | return m_data.size(); 17 | } 18 | 19 | int TableModel::columnCount(const QModelIndex &parent) const 20 | { 21 | return m_HorizontalHeader.count(); 22 | } 23 | 24 | QVariant TableModel::data(const QModelIndex &index, int role) const 25 | { 26 | if (!index.isValid()) 27 | return QVariant(); 28 | if (role == Qt::DisplayRole) { 29 | int ncol = index.column(); 30 | int nrow = index.row(); 31 | QStringList values = m_data.at(nrow); 32 | if (values.size() > ncol) 33 | return values.at(ncol); 34 | else 35 | return QVariant(); 36 | } 37 | return QVariant(); 38 | } 39 | 40 | Qt::ItemFlags TableModel::flags(const QModelIndex &index) const 41 | { 42 | if (!index.isValid()) 43 | return Qt::NoItemFlags; 44 | 45 | Qt::ItemFlags flag = QAbstractItemModel::flags(index); 46 | 47 | // flag|=Qt::ItemIsEditable // 设置单元格可编辑,此处注释,单元格无法被编辑 48 | return flag; 49 | } 50 | 51 | void TableModel::setHorizontalHeader(const QStringList &headers) 52 | { 53 | m_HorizontalHeader = headers; 54 | } 55 | 56 | 57 | QVariant TableModel::headerData(int section, Qt::Orientation orientation, int role) const 58 | { 59 | if (role == Qt::DisplayRole && orientation == Qt::Horizontal) { 60 | return m_HorizontalHeader.at(section); 61 | } 62 | return QAbstractTableModel::headerData(section, orientation, role); 63 | } 64 | 65 | void TableModel::setData(const QVector &data) 66 | { 67 | m_data = data; 68 | } 69 | -------------------------------------------------------------------------------- /TableViewAddProgressDemo/tablemodel.h: -------------------------------------------------------------------------------- 1 | #ifndef TABLEMODEL_H 2 | #define TABLEMODEL_H 3 | 4 | #include 5 | 6 | class TableModel : public QAbstractTableModel 7 | { 8 | Q_OBJECT 9 | public: 10 | explicit TableModel(QObject *parent = 0); 11 | int rowCount(const QModelIndex &parent) const; 12 | int columnCount(const QModelIndex &parent) const; 13 | QVariant data(const QModelIndex &index, int role) const; 14 | Qt::ItemFlags flags(const QModelIndex &index) const; 15 | void setHorizontalHeader(const QStringList& headers); 16 | QVariant headerData(int section, Qt::Orientation orientation, int role) const; 17 | void setData(const QVector& data); 18 | QVector& DataVector() {return m_data;} 19 | ~TableModel(void); 20 | 21 | signals: 22 | 23 | public slots: 24 | 25 | private: 26 | QStringList m_HorizontalHeader; 27 | QVector m_data; 28 | 29 | }; 30 | 31 | #endif // TABLEMODEL_H 32 | -------------------------------------------------------------------------------- /TableViewAddProgressDemo/tableview.cpp: -------------------------------------------------------------------------------- 1 | #include "tableview.h" 2 | 3 | #include "tablemodel.h" 4 | #include "progressbardelegate.h" 5 | 6 | TableView::TableView(QWidget *parent) : 7 | QTableView(parent) 8 | { 9 | iniData(); 10 | } 11 | 12 | TableView::~TableView() 13 | { 14 | delete m_model; 15 | } 16 | 17 | void TableView::iniData() 18 | { 19 | m_model = new TableModel(); 20 | this->setModel(m_model); 21 | QStringList headers; 22 | headers << "Id" << "Progress"; 23 | m_model->setHorizontalHeader(headers); 24 | 25 | QVector data; 26 | data.append(QStringList() << "1" << "22"); 27 | data.append(QStringList() << "2" << "32"); 28 | data.append(QStringList() << "3" << "2"); 29 | data.append(QStringList() << "4" << "80"); 30 | data.append(QStringList() << "5" << "40"); 31 | m_model->setData(data); 32 | 33 | m_progressBarDelegate = new ProgressBarDelegate(this); 34 | this->setItemDelegate(m_progressBarDelegate); 35 | emit m_model->layoutChanged(); 36 | this->setColumnWidth(1, 500); 37 | } 38 | -------------------------------------------------------------------------------- /TableViewAddProgressDemo/tableview.h: -------------------------------------------------------------------------------- 1 | #ifndef TABLEVIEW_H 2 | #define TABLEVIEW_H 3 | 4 | #include 5 | 6 | class TableModel; 7 | class ProgressBarDelegate; 8 | 9 | class TableView : public QTableView 10 | { 11 | Q_OBJECT 12 | public: 13 | explicit TableView(QWidget *parent = 0); 14 | TableModel* tableModel() {return m_model;} 15 | 16 | ~TableView(); 17 | 18 | signals: 19 | 20 | public slots: 21 | 22 | private: 23 | void iniData(); 24 | 25 | private: 26 | TableModel *m_model; 27 | ProgressBarDelegate *m_progressBarDelegate; 28 | 29 | }; 30 | 31 | #endif // TABLEVIEW_H 32 | --------------------------------------------------------------------------------