├── ModelDelegate.pro ├── README.md ├── itemdelegate.cpp ├── itemdelegate.h ├── main.cpp ├── tableview.cpp ├── tableview.h ├── widget.cpp ├── widget.h └── widget.ui /ModelDelegate.pro: -------------------------------------------------------------------------------- 1 | #------------------------------------------------- 2 | # 3 | # Project created by QtCreator 2018-06-04T16:14:02 4 | # 5 | #------------------------------------------------- 6 | 7 | QT += core gui 8 | 9 | greaterThan(QT_MAJOR_VERSION, 4): QT += widgets 10 | 11 | TARGET = ModelDelegate 12 | TEMPLATE = app 13 | 14 | # The following define makes your compiler emit warnings if you use 15 | # any feature of Qt which has been marked as deprecated (the exact warnings 16 | # depend on your compiler). Please consult the documentation of the 17 | # deprecated API in order to know how to port your code away from it. 18 | DEFINES += QT_DEPRECATED_WARNINGS 19 | 20 | # You can also make your code fail to compile if you use deprecated APIs. 21 | # In order to do so, uncomment the following line. 22 | # You can also select to disable deprecated APIs only up to a certain version of Qt. 23 | #DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0 24 | 25 | 26 | SOURCES += \ 27 | main.cpp \ 28 | widget.cpp \ 29 | tableview.cpp \ 30 | itemdelegate.cpp 31 | 32 | HEADERS += \ 33 | widget.h \ 34 | tableview.h \ 35 | itemdelegate.h 36 | 37 | FORMS += \ 38 | widget.ui 39 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # ModelDelegate 2 | 在QTableView中使用各种自定义委托 包括ComboBox、DoubleSpinBox、CheckBox以及设置列只读 3 | -------------------------------------------------------------------------------- /itemdelegate.cpp: -------------------------------------------------------------------------------- 1 | #include "itemdelegate.h" 2 | 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | #include 11 | #include 12 | #include 13 | #include 14 | 15 | /**************************************************** 16 | * ComboDelegate 17 | ****************************************************/ 18 | ComboDelegate::ComboDelegate(QObject *parent) : 19 | QItemDelegate(parent) 20 | { 21 | } 22 | 23 | void ComboDelegate::setItems(QStringList items) 24 | { 25 | m_sItemList = items; 26 | } 27 | 28 | QWidget *ComboDelegate::createEditor(QWidget *parent,const QStyleOptionViewItem &/*option*/,const QModelIndex &/*index*/) const 29 | { 30 | QComboBox *editor = new QComboBox(parent); 31 | editor->addItems(m_sItemList); 32 | editor->installEventFilter(const_cast(this)); 33 | return editor; 34 | } 35 | 36 | void ComboDelegate::setEditorData(QWidget *editor,const QModelIndex &index) const 37 | { 38 | QString str =index.model()->data(index).toString(); 39 | 40 | QComboBox *box = static_cast(editor); 41 | int i=box->findText(str); 42 | box->setCurrentIndex(i); 43 | } 44 | 45 | void ComboDelegate::setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const 46 | { 47 | QComboBox *box = static_cast(editor); 48 | QString str = box->currentText(); 49 | model->setData(index,str); 50 | } 51 | 52 | void ComboDelegate::updateEditorGeometry(QWidget *editor,const QStyleOptionViewItem &option, const QModelIndex &/*index*/) const 53 | { 54 | editor->setGeometry(option.rect); 55 | } 56 | 57 | /**************************************************** 58 | * SpinBoxDelegate 59 | ****************************************************/ 60 | SpinBoxDelegate::SpinBoxDelegate(QObject *parent) : 61 | QItemDelegate(parent) 62 | { 63 | //初始化最大最小值 64 | minValue = 1.00; 65 | maxValue = 100.00; 66 | } 67 | 68 | void SpinBoxDelegate::setRange(double min, double max) 69 | { 70 | minValue = min; 71 | maxValue = max; 72 | } 73 | 74 | QWidget *SpinBoxDelegate::createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const 75 | { 76 | QDoubleSpinBox *editor = new QDoubleSpinBox(parent); 77 | editor->setMinimum(minValue); 78 | editor->setMaximum(maxValue); 79 | editor->setSingleStep(0.01); 80 | return editor; 81 | } 82 | 83 | void SpinBoxDelegate::setEditorData(QWidget *editor, const QModelIndex &index) const 84 | { 85 | double value = index.model()->data(index, Qt::EditRole).toDouble(); 86 | QDoubleSpinBox *spinBox = static_cast (editor); 87 | spinBox->setValue(value); 88 | } 89 | 90 | void SpinBoxDelegate::setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const 91 | { 92 | QDoubleSpinBox *spinBox = static_cast (editor); 93 | spinBox->interpretText(); 94 | double value = spinBox->value(); 95 | model->setData(index, value, Qt::EditRole); 96 | } 97 | 98 | void SpinBoxDelegate::updateEditorGeometry(QWidget *editor, const QStyleOptionViewItem &option, const QModelIndex &index) const 99 | { 100 | editor->setGeometry(option.rect); 101 | } 102 | 103 | /**************************************************** 104 | * CheckBoxDelegate 105 | ****************************************************/ 106 | static QRect CheckBoxRect(const QStyleOptionViewItem &viewItemStyleOptions)/*const*/ 107 | { 108 | //绘制按钮所需要的参数 109 | QStyleOptionButton checkBoxStyleOption; 110 | //按照给定的风格参数 返回元素子区域 111 | QRect checkBoxRect = QApplication::style()->subElementRect( QStyle::SE_CheckBoxIndicator, &checkBoxStyleOption); 112 | //返回QCheckBox坐标 113 | QPoint checkBoxPoint(viewItemStyleOptions.rect.x() + viewItemStyleOptions.rect.width() / 2 - checkBoxRect.width() / 2, 114 | viewItemStyleOptions.rect.y() + viewItemStyleOptions.rect.height() / 2 - checkBoxRect.height() / 2); 115 | //返回QCheckBox几何形状 116 | return QRect(checkBoxPoint, checkBoxRect.size()); 117 | } 118 | 119 | CheckBoxDelegate::CheckBoxDelegate(QObject *parent): 120 | QStyledItemDelegate(parent) 121 | { 122 | 123 | } 124 | 125 | void CheckBoxDelegate::setColumn(int col) 126 | { 127 | column = col; 128 | } 129 | 130 | // 绘制复选框 131 | void CheckBoxDelegate::paint(QPainter* painter, const QStyleOptionViewItem& option,const QModelIndex& index)const 132 | { 133 | bool checked = index.model()->data(index, Qt::DisplayRole).toBool(); 134 | 135 | if(index.column() == column){ 136 | QStyleOptionButton checkBoxStyleOption; 137 | checkBoxStyleOption.state |= QStyle::State_Enabled; 138 | checkBoxStyleOption.state |= checked? QStyle::State_On : QStyle::State_Off; 139 | checkBoxStyleOption.rect = CheckBoxRect(option); 140 | 141 | QApplication::style()->drawControl(QStyle::CE_CheckBox,&checkBoxStyleOption,painter); 142 | }else{ 143 | QStyledItemDelegate::paint(painter, option, index); 144 | } 145 | } 146 | 147 | // 响应鼠标事件,更新数据 148 | bool CheckBoxDelegate::editorEvent(QEvent *event, 149 | QAbstractItemModel *model, 150 | const QStyleOptionViewItem &option, 151 | const QModelIndex &index) { 152 | if(index.column() == column){ 153 | if((event->type() == QEvent::MouseButtonRelease) || 154 | (event->type() == QEvent::MouseButtonDblClick)){ 155 | QMouseEvent *mouseEvent = static_cast(event); 156 | if(mouseEvent->button() != Qt::LeftButton || 157 | !CheckBoxRect(option).contains(mouseEvent->pos())){ 158 | return true; 159 | } 160 | if(event->type() == QEvent::MouseButtonDblClick){ 161 | return true; 162 | } 163 | }else if(event->type() == QEvent::KeyPress){ 164 | if(static_cast(event)->key() != Qt::Key_Space && 165 | static_cast(event)->key() != Qt::Key_Select){ 166 | return false; 167 | } 168 | }else{ 169 | return false; 170 | } 171 | 172 | bool checked = index.model()->data(index, Qt::DisplayRole).toBool(); 173 | int data = checked ? 0 : 1; //互逆 174 | return model->setData(index, data, Qt::EditRole); 175 | }else{ 176 | return QStyledItemDelegate::editorEvent(event, model, option, index); 177 | } 178 | } 179 | 180 | /**************************************************** 181 | * DateDelegate 182 | ****************************************************/ 183 | DateDelegate::DateDelegate(QObject *parent) : 184 | QItemDelegate(parent) 185 | { 186 | } 187 | 188 | QWidget *DateDelegate::createEditor(QWidget *parent,const QStyleOptionViewItem &/*option*/,const QModelIndex &/*index*/) const 189 | { 190 | QDateEdit *editor = new QDateEdit(parent); 191 | editor->setDisplayFormat("yyyy-MM-dd"); 192 | editor->setCalendarPopup(true); 193 | return editor; 194 | } 195 | 196 | void DateDelegate::setEditorData(QWidget *editor,const QModelIndex &index) const 197 | { 198 | QString str =index.model()->data(index).toString(); 199 | 200 | QDateEdit *pDate = static_cast(editor); 201 | pDate->setDate(QDate::fromString(str,"yyyy-MM-dd")); 202 | } 203 | 204 | void DateDelegate::setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const 205 | { 206 | QDateEdit *pDate = static_cast(editor); 207 | QString str = pDate->date().toString("yyyy-MM-dd"); 208 | model->setData(index,str); 209 | } 210 | 211 | void DateDelegate::updateEditorGeometry(QWidget *editor,const QStyleOptionViewItem &option, const QModelIndex &/*index*/) const 212 | { 213 | editor->setGeometry(option.rect); 214 | } 215 | 216 | /**************************************************** 217 | * TimeDelegate 218 | ****************************************************/ 219 | TimeDelegate::TimeDelegate(QObject *parent) : 220 | QItemDelegate(parent) 221 | { 222 | } 223 | 224 | QWidget *TimeDelegate::createEditor(QWidget *parent,const QStyleOptionViewItem &/*option*/,const QModelIndex &/*index*/) const 225 | { 226 | QTimeEdit *editor = new QTimeEdit(parent); 227 | editor->setDisplayFormat("hh:mm:ss"); 228 | editor->setCalendarPopup(true); 229 | return editor; 230 | } 231 | 232 | void TimeDelegate::setEditorData(QWidget *editor,const QModelIndex &index) const 233 | { 234 | QString str =index.model()->data(index).toString(); 235 | 236 | QTimeEdit *pTime = static_cast(editor); 237 | pTime->setTime(QTime::fromString(str,"hh:mm:ss")); 238 | } 239 | 240 | void TimeDelegate::setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const 241 | { 242 | QTimeEdit *pTime = static_cast(editor); 243 | QString str = pTime->time().toString("hh:mm:ss"); 244 | model->setData(index,str); 245 | } 246 | 247 | void TimeDelegate::updateEditorGeometry(QWidget *editor,const QStyleOptionViewItem &option, const QModelIndex &/*index*/) const 248 | { 249 | editor->setGeometry(option.rect); 250 | } 251 | 252 | /**************************************************** 253 | * DoubleLineEditDelegate 254 | ****************************************************/ 255 | DoubleLineEditDelegate::DoubleLineEditDelegate(QObject *parent) : 256 | QItemDelegate(parent) 257 | { 258 | min = 0; 259 | max = 99; 260 | } 261 | 262 | void DoubleLineEditDelegate::setRange(double bottom, double top, int decimals) 263 | { 264 | min = bottom; 265 | max = top; 266 | dec = decimals; 267 | } 268 | 269 | QWidget *DoubleLineEditDelegate::createEditor(QWidget *parent,const QStyleOptionViewItem &/*option*/,const QModelIndex &/*index*/) const 270 | { 271 | QLineEdit *editor = new QLineEdit(parent); 272 | editor->setValidator(new QDoubleValidator(min, max, dec)); 273 | return editor; 274 | } 275 | 276 | void DoubleLineEditDelegate::setEditorData(QWidget *editor,const QModelIndex &index) const 277 | { 278 | QString str =index.model()->data(index).toString(); 279 | 280 | QLineEdit *lineEdit = static_cast(editor); 281 | lineEdit->setText(str); 282 | } 283 | 284 | void DoubleLineEditDelegate::setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const 285 | { 286 | QLineEdit *lineEdit = static_cast(editor); 287 | QString str = lineEdit->text(); 288 | model->setData(index,str); 289 | } 290 | 291 | void DoubleLineEditDelegate::updateEditorGeometry(QWidget *editor,const QStyleOptionViewItem &option, const QModelIndex &/*index*/) const 292 | { 293 | editor->setGeometry(option.rect); 294 | } 295 | -------------------------------------------------------------------------------- /itemdelegate.h: -------------------------------------------------------------------------------- 1 | #ifndef ITEMDELEGATE_H 2 | #define ITEMDELEGATE_H 3 | 4 | #include 5 | #include 6 | 7 | /* 8 | * ReadOnly 9 | */ 10 | class ReadOnlyDelegate : public QItemDelegate 11 | { 12 | Q_OBJECT 13 | public: 14 | ReadOnlyDelegate(QObject *parent = 0): QItemDelegate(parent) {} 15 | 16 | void setItems(QStringList items); 17 | 18 | QWidget *createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const 19 | { 20 | return NULL; 21 | } 22 | }; 23 | 24 | /* 25 | * ComboBox 26 | */ 27 | class ComboDelegate : public QItemDelegate 28 | { 29 | Q_OBJECT 30 | public: 31 | ComboDelegate(QObject *parent = 0); 32 | 33 | void setItems(QStringList items); 34 | 35 | QWidget *createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const; 36 | void setEditorData(QWidget *editor, const QModelIndex &index) const; 37 | void setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const; 38 | void updateEditorGeometry(QWidget *editor, const QStyleOptionViewItem &option, const QModelIndex &index) const; 39 | 40 | private: 41 | QStringList m_sItemList; //下拉框列表项 42 | }; 43 | 44 | /* 45 | * DoubleSpinBox 46 | */ 47 | class SpinBoxDelegate : public QItemDelegate 48 | { 49 | Q_OBJECT 50 | public: 51 | SpinBoxDelegate(QObject *parent = 0); 52 | 53 | void setRange(double min, double max); 54 | 55 | QWidget *createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const; 56 | void setEditorData(QWidget *editor, const QModelIndex &index) const; 57 | void setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const; 58 | void updateEditorGeometry(QWidget *editor, const QStyleOptionViewItem &option, const QModelIndex &index) const; 59 | 60 | private: 61 | double minValue; //最小值 62 | double maxValue; //最大值 63 | }; 64 | 65 | /* 66 | * CheckBox 67 | */ 68 | class CheckBoxDelegate : public QStyledItemDelegate 69 | { 70 | Q_OBJECT 71 | public: 72 | CheckBoxDelegate(QObject *parent = 0); 73 | 74 | void setColumn(int col); 75 | 76 | protected: 77 | void paint(QPainter* painter,const QStyleOptionViewItem& option,const QModelIndex& index) const; 78 | bool editorEvent(QEvent *event,QAbstractItemModel *model,const QStyleOptionViewItem &option,const QModelIndex &index); 79 | 80 | private: 81 | int column; //设置复选框的列 82 | }; 83 | 84 | /* 85 | * calendar 86 | */ 87 | class DateDelegate : public QItemDelegate 88 | { 89 | Q_OBJECT 90 | public: 91 | DateDelegate(QObject *parent = 0); 92 | 93 | QWidget *createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const; 94 | void setEditorData(QWidget *editor, const QModelIndex &index) const; 95 | void setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const; 96 | void updateEditorGeometry(QWidget *editor, const QStyleOptionViewItem &option, const QModelIndex &index) const; 97 | }; 98 | 99 | /* 100 | * TimeEdit 101 | */ 102 | class TimeDelegate : public QItemDelegate 103 | { 104 | Q_OBJECT 105 | public: 106 | TimeDelegate(QObject *parent = 0); 107 | 108 | QWidget *createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const; 109 | void setEditorData(QWidget *editor, const QModelIndex &index) const; 110 | void setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const; 111 | void updateEditorGeometry(QWidget *editor, const QStyleOptionViewItem &option, const QModelIndex &index) const; 112 | }; 113 | 114 | /* 115 | * DoubleLineEdit 116 | */ 117 | class DoubleLineEditDelegate : public QItemDelegate 118 | { 119 | Q_OBJECT 120 | public: 121 | DoubleLineEditDelegate(QObject *parent = 0); 122 | 123 | void setRange(double bottom, double top, int decimals = 0); 124 | 125 | QWidget *createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const; 126 | void setEditorData(QWidget *editor, const QModelIndex &index) const; 127 | void setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const; 128 | void updateEditorGeometry(QWidget *editor, const QStyleOptionViewItem &option, const QModelIndex &index) const; 129 | 130 | private: 131 | int min; //最小值 132 | int max; //最大值 133 | int dec; //小数位数 134 | }; 135 | 136 | #endif // ITEMDELEGATE_H 137 | -------------------------------------------------------------------------------- /main.cpp: -------------------------------------------------------------------------------- 1 | #include "widget.h" 2 | #include 3 | #include 4 | 5 | int main(int argc, char *argv[]) 6 | { 7 | QApplication a(argc, argv); 8 | 9 | QTextCodec *codec = QTextCodec::codecForName("GBK"); 10 | QTextCodec::setCodecForLocale(codec); 11 | 12 | Widget w; 13 | w.show(); 14 | 15 | return a.exec(); 16 | } 17 | -------------------------------------------------------------------------------- /tableview.cpp: -------------------------------------------------------------------------------- 1 | #include "tableview.h" 2 | 3 | #include 4 | 5 | #include "itemdelegate.h" 6 | 7 | #define MAXCOL 6 8 | 9 | TableView::TableView(QStringList items) 10 | { 11 | resize(600,300); 12 | 13 | model = new QStandardItemModel(4,MAXCOL); 14 | setModel(model); 15 | 16 | model->setHeaderData(0,Qt::Horizontal,QObject::tr("姓名")); 17 | model->setHeaderData(1,Qt::Horizontal,QObject::tr("生日")); 18 | model->setHeaderData(2,Qt::Horizontal,QObject::tr("职业")); 19 | model->setHeaderData(3,Qt::Horizontal,QObject::tr("收入")); 20 | model->setHeaderData(4,Qt::Horizontal,QObject::tr("日期")); 21 | model->setHeaderData(5,Qt::Horizontal,QObject::tr("时间")); 22 | 23 | #if 1 24 | for(int col = 0; col < MAXCOL; ++col) 25 | { 26 | if(col == 0) 27 | { 28 | ReadOnlyDelegate *readOnly = new ReadOnlyDelegate; 29 | setItemDelegateForColumn(col,readOnly); 30 | } 31 | if(col == 1) 32 | { 33 | DoubleLineEditDelegate *lineEdit = new DoubleLineEditDelegate; 34 | lineEdit->setRange(0, 999, 6); 35 | setItemDelegateForColumn(col, lineEdit); 36 | } 37 | if(col == 2) 38 | { 39 | SpinBoxDelegate *spinBox = new SpinBoxDelegate; 40 | setItemDelegateForColumn(col,spinBox); 41 | } 42 | if(col == 3) 43 | { 44 | ComboDelegate *combo = new ComboDelegate; 45 | combo->setItems(items); 46 | setItemDelegateForColumn(col,combo); 47 | } 48 | if(col == 4){ 49 | DateDelegate *date = new DateDelegate; 50 | setItemDelegateForColumn(col, date); 51 | } 52 | if(col == 5){ 53 | TimeDelegate *time = new TimeDelegate; 54 | setItemDelegateForColumn(col, time); 55 | } 56 | for(int row = 0; row < 4; ++row) 57 | { 58 | QModelIndex index = model->index(row, col, QModelIndex()); 59 | if(col == 1) 60 | model->setData(index, 0); 61 | else 62 | model->setData(index, QString("%1%2").arg(row).arg(col)); 63 | } 64 | } 65 | #else 66 | QFile file("E:\Qt\MyDelegate\a.txt"); 67 | QTextStream out(&file); 68 | if(!file.open(QIODevice::ReadOnly | QIODevice::Text)) 69 | return; 70 | while(out.atEnd()){ 71 | QString data = out.readLine(); 72 | QStringList dataList = data.split(","); 73 | int rows = model->rowCount(); 74 | model->setRowCount(rows + 1); 75 | for(int col = 0; col < 4; ++col){ 76 | QModelIndex index = model->index(rows, col, QModelIndex()); 77 | model->setData(index,dataList.at(col)); 78 | } 79 | } 80 | #endif 81 | } 82 | 83 | QString TableView::GetItemData(int row, int col) 84 | { 85 | QModelIndex index = model->index(row,col,QModelIndex()); 86 | return model->data(index).toString(); 87 | } 88 | -------------------------------------------------------------------------------- /tableview.h: -------------------------------------------------------------------------------- 1 | #ifndef TABLEVIEW_H 2 | #define TABLEVIEW_H 3 | 4 | #include 5 | 6 | class QStandardItemModel; 7 | 8 | class TableView : public QTableView 9 | { 10 | Q_OBJECT 11 | 12 | public: 13 | TableView(QStringList items); 14 | 15 | QString GetItemData(int row, int col); 16 | 17 | private: 18 | QStandardItemModel *model; 19 | }; 20 | 21 | #endif // TABLEVIEW_H 22 | -------------------------------------------------------------------------------- /widget.cpp: -------------------------------------------------------------------------------- 1 | #include "widget.h" 2 | #include "ui_widget.h" 3 | 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | #include 11 | 12 | #include "tableview.h" 13 | 14 | Widget::Widget(QWidget *parent) : 15 | QWidget(parent), 16 | ui(new Ui::Widget) 17 | { 18 | ui->setupUi(this); 19 | resize(500,300); 20 | 21 | QHBoxLayout *hlayout = new QHBoxLayout(this); 22 | m_pTabWidget = new QTabWidget(this); 23 | hlayout->addWidget(m_pTabWidget); 24 | 25 | QPushButton *btn = new QPushButton(tr("获取数据"),this); 26 | connect(btn, SIGNAL(clicked(bool)), this, SLOT(onBtn_clicked())); 27 | hlayout->addWidget(btn); 28 | 29 | QStringList items; 30 | items.append("音乐"); 31 | items.append("美术"); 32 | items.append("体育"); 33 | items.append("表演"); 34 | TableView *table1 = new TableView(items); 35 | m_pTabWidget->addTab(table1,tr("tab1")); 36 | 37 | items.clear(); 38 | items.append("农民1"); 39 | items.append("工人1"); 40 | items.append("教师1"); 41 | items.append("音乐2"); 42 | TableView *table2 = new TableView(items); 43 | m_pTabWidget->addTab(table2,tr("tab2")); 44 | 45 | setLayout(hlayout); 46 | } 47 | 48 | Widget::~Widget() 49 | { 50 | delete ui; 51 | } 52 | 53 | void Widget::onBtn_clicked() 54 | { 55 | TableView *table = static_cast(m_pTabWidget->currentWidget()); 56 | for(int row = 0; row < 4; ++row) 57 | { 58 | QString data = table->GetItemData(0, row); 59 | qDebug() << "data:" << data; 60 | } 61 | } 62 | -------------------------------------------------------------------------------- /widget.h: -------------------------------------------------------------------------------- 1 | #ifndef WIDGET_H 2 | #define WIDGET_H 3 | 4 | #include 5 | #include 6 | 7 | class TableView; 8 | 9 | namespace Ui { 10 | class Widget; 11 | } 12 | 13 | class Widget : public QWidget 14 | { 15 | Q_OBJECT 16 | 17 | public: 18 | explicit Widget(QWidget *parent = 0); 19 | ~Widget(); 20 | 21 | private slots: 22 | void onBtn_clicked(); 23 | 24 | private: 25 | Ui::Widget *ui; 26 | 27 | QTabWidget *m_pTabWidget; 28 | }; 29 | 30 | #endif // WIDGET_H 31 | -------------------------------------------------------------------------------- /widget.ui: -------------------------------------------------------------------------------- 1 | 2 | Widget 3 | 4 | 5 | 6 | 0 7 | 0 8 | 400 9 | 300 10 | 11 | 12 | 13 | Widget 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | --------------------------------------------------------------------------------