Results 1 to 3 of 3

Thread: Spread of reading data with QTableWidget and QTableView

  1. #1
    Join Date
    May 2010
    Location
    China
    Posts
    66
    Thanks
    8
    Qt products
    Qt4
    Platforms
    Windows

    Wink Spread of reading data with QTableWidget and QTableView

    hi ,i use the QTableWidget to read data form csv files but i found that the speed is very slow ,so i inhert a widget form QTableView and use the model to add data but i did't see any improvement;


    gridview.h
    Qt Code:
    1. #ifndef GRIDVIEW_H
    2. #define GRIDVIEW_H
    3.  
    4. #include <QTableView>
    5. #include <QStandardItemModel>
    6.  
    7. class QTableModel;
    8.  
    9. class GridView : public QTableView
    10. {
    11. Q_OBJECT
    12.  
    13. public:
    14. GridView(QWidget *parent);
    15. ~GridView();
    16.  
    17. QString currentLocation() const;
    18. QString currentText() const;
    19. int currentRow() const;
    20. int currentColumn() const;
    21. void clear();
    22. void setText(int row,int colum,const QString &text);
    23. QString text(int row,int column);
    24. QItemSelectionRange selectedRange() const;
    25. QList<QStandardItem*> selectedItems();
    26. QString isNumber(bool& checkResult);
    27. void setRowCount(int row);
    28. void setColumnCount(int column);
    29.  
    30. int rowsCount()
    31. {
    32. return rowCount;
    33. }
    34.  
    35. int columnCount()
    36. {
    37. return columnsCount;
    38. }
    39.  
    40.  
    41.  
    42. public slots:
    43. void cut();
    44. void copy();
    45. void paste();
    46. void del();
    47. void selectCurrentRow();
    48. void selectCurrentColumn();
    49. QString statistics(bool& convertSucess);
    50.  
    51.  
    52.  
    53.  
    54. signals:
    55. void modified();
    56.  
    57. private slots:
    58. void rowsHandle(int rowAction);
    59. void columnsHandle(int columnAction);
    60.  
    61.  
    62. private:
    63.  
    64. int rowCount;
    65. int columnsCount;
    66.  
    67. void updateGridHeader();
    68.  
    69. };
    70.  
    71. #endif // GRIDVIEW_H
    To copy to clipboard, switch view to plain text mode 


    gridview.cpp
    Qt Code:
    1. #include "gridview.h"
    2. #include <QtGui>
    3. #include "cell.h"
    4. #include <gsl/gsl_statistics.h>
    5. #include <QStandardItem>
    6.  
    7. GridView::GridView(QWidget *parent)
    8. : QTableView(parent)
    9. {
    10.  
    11.  
    12.  
    13. model=new QStandardItemModel(this);
    14. this->setModel(model);
    15. updateGridHeader();
    16.  
    17. QScrollBar* vscroll=this->verticalScrollBar();
    18. if (vscroll)
    19. {
    20. connect(vscroll,SIGNAL(actionTriggered(int)),this,SLOT(rowsHandle(int)));
    21. }
    22.  
    23. QScrollBar *hscroll=this->horizontalScrollBar();
    24. //æ·»åŠ åˆ—
    25. if (hscroll)
    26. {
    27. connect(hscroll,SIGNAL(actionTriggered(int)),this,SLOT(columnsHandle(int)));
    28. }
    29. clear();
    30.  
    31. }
    32.  
    33. GridView::~GridView()
    34. {
    35.  
    36. }
    37.  
    38. void GridView::clear()
    39. {
    40. rowCount=50;
    41. columnsCount=20;
    42.  
    43.  
    44. model->setRowCount(rowCount);
    45. model->setColumnCount(columnsCount);
    46.  
    47. }
    48. QString GridView::currentLocation() const
    49. {
    50.  
    51.  
    52. int row=this->currentIndex().row();
    53. int column=this->currentIndex().column();
    54.  
    55. int c=column/26;
    56. QString text=QString(QChar('A' + column%26));
    57. if (c>0)
    58. {
    59. text=QString(QChar('A'+(c-1)));
    60. text+=QString(QChar('A' + column%26));
    61. }
    62.  
    63.  
    64. text+=QString::number(row,10);
    65.  
    66.  
    67. return text;
    68.  
    69. }
    70.  
    71. QString GridView::currentText() const
    72. {
    73. int row=this->currentIndex().row();
    74. int column=this->currentIndex().column();
    75.  
    76. QStandardItem* item=model->item(row,column);
    77.  
    78. if (item)
    79. {
    80. item->data( Qt::DisplayRole).toString();
    81. }
    82. else
    83. {
    84. return "";
    85. }
    86.  
    87.  
    88. }
    89. void GridView::setText(int row,int colum,const QString &text)
    90. {
    91. QStandardItem *itemID = new QStandardItem(text);//QString::number(j)));
    92. model->setItem(row,colum,itemID);
    93. }
    94. void GridView::selectCurrentColumn()
    95. {
    96. selectColumn(currentIndex().column());
    97.  
    98. }
    99.  
    100. void GridView::selectCurrentRow()
    101. {
    102. selectRow(currentIndex().row());
    103.  
    104. }
    105. void GridView::updateGridHeader()
    106. {
    107.  
    108. for (int i=0;i<model->columnCount();i++)
    109. {
    110. int c=i/26;
    111.  
    112. QString text=QString(QChar('A'+(i)%26));
    113. if (c>0)
    114. {
    115. text=QString(QChar('A'+(c-1)));
    116. text+=QString(QChar('A' + (i)%26));
    117. }
    118.  
    119. model->setHeaderData(i,Qt::Horizontal,text);
    120.  
    121. }
    122. }
    123.  
    124.  
    125. QItemSelectionRange GridView::selectedRange() const
    126. {
    127. const QList<QItemSelectionRange> ranges = selectionModel()->selection();
    128. return ranges.first();
    129.  
    130.  
    131. }
    132.  
    133. QString GridView::text(int row,int column)
    134. {
    135. QStandardItem* item=model->item(row,column);
    136.  
    137. if (item)
    138. {
    139. item->data( Qt::DisplayRole).toString();
    140. }
    141. else
    142. {
    143. return "";
    144. }
    145.  
    146. }
    147. QList<QStandardItem*> GridView::selectedItems()
    148. {
    149. QModelIndexList indexes = selectionModel()->selectedIndexes();
    150. QList<QStandardItem*> items;
    151. for (int i = 0; i < indexes.count(); ++i) {
    152. QModelIndex index = indexes.at(i);
    153. if (isIndexHidden(index))
    154. continue;
    155. QStandardItem *item =model->item(index.row(),index.column());
    156. if (item)
    157. items.append(item);
    158. }
    159. return items;
    160.  
    161.  
    162. }
    163. void GridView::cut()
    164. {
    165. copy();
    166. del();
    167.  
    168.  
    169. }
    170. void GridView::paste()
    171. {
    172. QItemSelectionRange range=selectedRange();
    173. QString str = QApplication::clipboard()->text();
    174. str=str.trimmed();
    175. QStringList rows = str.split('\n');
    176. int numRows = rows.count();
    177. int numColumns = rows.first().count('\t') + 1;
    178.  
    179. if ((range.top()+numRows)>rowCount)
    180. {
    181. model->setRowCount(range.top()+numRows);
    182. }
    183. if ((range.left()+numColumns)>columnsCount)
    184. {
    185. model->setRowCount(range.left()+numColumns);
    186. }
    187.  
    188. updateGridHeader();
    189.  
    190. if (range.height() * range.width() != 1&& (range.height() != numRows|| range.width() != numColumns)) {
    191. QMessageBox::information(this, tr("Spreadsheet"),
    192. tr("The information cannot be pasted because the copy "
    193. "and paste areas aren't the same size."));
    194. return;
    195. }
    196.  
    197. for (int i = 0; i < numRows; ++i)
    198. {
    199. QStringList columns = rows[i].split('\t');
    200. for (int j = 0; j < numColumns; ++j)
    201. {
    202. int row = range.top() + i;
    203. int column = range.left() + j;
    204.  
    205.  
    206. if (row < rowCount&& column<columnsCount)
    207. {
    208. setText(row, column, columns[j]);
    209. }
    210. }
    211. }
    212.  
    213.  
    214.  
    215.  
    216.  
    217.  
    218. }
    219. void GridView::copy()
    220. {
    221. QItemSelectionRange range=selectedRange();
    222. QString str;
    223.  
    224. for (int i=0;i<range.height();i++)
    225. {
    226. if (i>0)
    227. {
    228. str+="\n";
    229. for (int j=0;j<range.width();j++)
    230. {
    231. if (j>0)
    232. {
    233. str+="\t";
    234. str+=text(range.top()+i,range.left()+j);
    235. }
    236. }
    237. }
    238.  
    239. }
    240.  
    241. QApplication::clipboard()->setText(str);
    242.  
    243.  
    244. }
    245. void GridView::del()
    246. {
    247. QList<QStandardItem *> items = selectedItems();
    248. if (!items.isEmpty())
    249. {
    250. foreach (QStandardItem *item, items)
    251. delete item;
    252. }
    253.  
    254. }
    255. void GridView::columnsHandle(int columnAction)
    256. {
    257. if (columnAction==QAbstractSlider::SliderSingleStepAdd)
    258. {
    259. if (this->horizontalScrollBar()->value()==this->horizontalScrollBar()->maximum())
    260. {
    261. int count=model->columnCount()+1;
    262. model->setColumnCount(count);
    263. }
    264. }
    265. updateGridHeader();
    266. }
    267.  
    268. void GridView::rowsHandle(int rowAction)
    269. {
    270. if (rowAction==QAbstractSlider::SliderSingleStepAdd)
    271. {
    272. if (this->verticalScrollBar()->value()==this->verticalScrollBar()->maximum())
    273. {
    274. int count=model->rowCount()+1;
    275. model->setRowCount(count);
    276. }
    277. }
    278. }
    279.  
    280. QString GridView::isNumber(bool& checkResult)
    281. {
    282. QString retrunReport;
    283. QList<QStandardItem*> selecteditem=selectedItems();
    284.  
    285. QList<double> castedNumber;
    286.  
    287. for (int i=0;i<selectedItems().size();i++)
    288. {
    289. bool castSuccess=true;
    290.  
    291. double castedDouble=selecteditem[i]->text().toDouble(&castSuccess);
    292. if (!castSuccess)
    293. {
    294. retrunReport+=QString("The Item of [<font color=red>%1</font>,<font color=red>%2</font>] is not a number; <br>").arg(selecteditem[i]->row()+1).arg(selecteditem[i]->column()+1);
    295. checkResult=false;
    296. }
    297. else
    298. {
    299. castedNumber+=castedDouble;
    300. }
    301. }
    302.  
    303. return retrunReport;
    304.  
    305. }
    306.  
    307. QString GridView:: statistics(bool& convertSucess)
    308. {
    309. QString retrunReport;
    310. /*retrunReport=+(new QDateTime)->toString(Qt::DateFormat::ISODate);
    311. retrunReport=+"<br>";*/
    312.  
    313. QList<QStandardItem*> selecteditem=selectedItems();
    314. if (selecteditem.size()==0)
    315. {
    316. retrunReport+=tr("There isn't any selected items existed.<br>");
    317. convertSucess=false;
    318. }
    319. QList<double> castedNumber;
    320.  
    321. for (int i=0;i<selectedItems().size();i++)
    322. {
    323. bool castSuccess=true;
    324.  
    325. double castedDouble=selecteditem[i]->text().toDouble(&castSuccess);
    326. if (!castSuccess)
    327. {
    328. retrunReport+=QString("<font color=red>ERROR</font> The Item of [<font color=red>%1</font>,<font color=red>%2</font>] is not a number; <br>").arg(selecteditem[i]->row()+1).arg(selecteditem[i]->column()+1);
    329. }
    330. else
    331. {
    332. castedNumber+=castedDouble;
    333. }
    334. }
    335.  
    336. double *statsticNumber=new double[castedNumber.size()];
    337.  
    338. for (int i=0;i<castedNumber.size();i++)
    339. {
    340. statsticNumber[i]=castedNumber[i];
    341. }
    342. if (castedNumber.size()>0)
    343. {
    344. retrunReport+=QString("The <font color=blue><strong>mean</strong></font> of these number is : <font color=red><strong>%1</strong></font><br />").arg(gsl_stats_mean(statsticNumber,1,castedNumber.size()));
    345. }
    346.  
    347.  
    348. delete statsticNumber;
    349. return retrunReport;
    350.  
    351.  
    352. }
    353.  
    354. void GridView::setColumnCount(int column)
    355. {
    356. model->setColumnCount(column);
    357.  
    358.  
    359. }
    360.  
    361. void GridView::setRowCount(int row)
    362. {
    363. model->setRowCount(row);
    364.  
    365. }
    366.  
    367. int GridView::currentRow() const
    368. {
    369. return currentIndex().row();
    370.  
    371. }
    372.  
    373. int GridView::currentColumn() const
    374. {
    375. return currentIndex().column();
    376.  
    377.  
    378. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by xiongxiongchuan; 26th June 2010 at 06:27.

  2. #2
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: Spread of reading data with QTableWidget and QTableView

    you can't see any improvements since QTableWidget uses a QStandardItemModel internal too. So there is no difference. Better use your own custom model, then you will probably see more efficiency. For a hint see QxtCsvModel.

  3. #3
    Join Date
    May 2010
    Location
    China
    Posts
    66
    Thanks
    8
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Spread of reading data with QTableWidget and QTableView

    i have create a mode and insert data like this ,but it is still very slow model->setItem(i, 13, new QStandardItem("14"));
    table->setModel(model);
    Last edited by xiongxiongchuan; 26th June 2010 at 09:47.

Similar Threads

  1. Replies: 3
    Last Post: 1st February 2011, 11:57
  2. Reading data from socket
    By ComaWhite in forum Qt Programming
    Replies: 1
    Last Post: 4th December 2008, 18:22
  3. Replies: 7
    Last Post: 28th June 2007, 17:18
  4. Reading/writing QTableWidget
    By Salazaar in forum Newbie
    Replies: 15
    Last Post: 23rd June 2007, 13:20
  5. reading and writing data from a QTableWidget
    By zorro68 in forum Qt Programming
    Replies: 4
    Last Post: 29th January 2007, 20:51

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.