Qtablewidget Export to CSV table

Source: Internet
Author: User
Tags rowcount

Job-hopping to the new company, began to work the site development, access to new applications. There are many applications that need to export tables to a table, and you can export the table to a file in CSV format. Share with the rest of the crowd;

[CPP]View PlainCopy 
  1. Lass tabletoexcle: public qdialog
  2. {
  3. Q_object
  4. Public
  5. Tabletoexcle (Qwidget *parent = 0, Qt::wflags flags = 0);
  6. ~tabletoexcle ();
  7. Private
  8. Ui::tabletoexcleclass UI;
  9. Private Slots:
  10. void Addrowslot ();
  11. void Delrowslot ();
  12. void Exportslot ();
  13. };

[CPP]View PlainCopy 
  1. Tabletoexcle::tabletoexcle (Qwidget *parent, qt::wflags flags)
  2. : Qdialog (parent, flags)
  3. {
  4. UI.SETUPUI (this);
  5. Ui.m_ptable->setcolumncount (4);
  6. Qtablewidgetitem * item = new Qtablewidgetitem ("0");
  7. Ui.m_ptable->sethorizontalheaderitem (0, item);
  8. item = New Qtablewidgetitem ("1");
  9. Ui.m_ptable->sethorizontalheaderitem (1, item);
  10. item = New Qtablewidgetitem ("2");
  11. Ui.m_ptable->sethorizontalheaderitem (2, item);
  12. item = New Qtablewidgetitem ("3");
  13. Ui.m_ptable->sethorizontalheaderitem (3, item);
  14. Ui.m_ptable->setselectionbehavior (qabstractitemview::selectrows);
  15. Connect (ui.m_paddbtn,signal (clicked ()),This,slot (Addrowslot ()));
  16. Connect (ui.m_pdelbtn,signal (clicked ()),This,slot (Delrowslot ()));
  17. Connect (ui.m_pexportbtn,signal (clicked ()),This,slot (Exportslot ()));
  18. }
  19. Tabletoexcle::~tabletoexcle ()
  20. {
  21. }
  22. void Tabletoexcle::addrowslot ()
  23. {
  24. Ui.m_ptable->insertrow (Ui.m_ptable->rowcount ());
  25. }
  26. void Tabletoexcle::d elrowslot ()
  27. {
  28. int index = Ui.m_ptable->currentrow ();
  29. if (Index >-1)
  30. {
  31. Ui.m_ptable->removerow (index);
  32. }
  33. }
  34. void Tabletoexcle::exportslot ()
  35. {
  36. QString FileName = Qfiledialog::getsavefilename (This, tr ("Save file"), "", tr ("file (*.csv)");
  37. if (!filename.isempty ())
  38. {
  39. QFile file (fileName);
  40. BOOL ret = File.Open (qiodevice::truncate |  QIODEVICE::WRITEONLY);
  41. if (!ret)
  42. return;
  43. Qtextstream Stream (&file);
  44. QString conTents;
  45. Qheaderview * Header = Ui.m_ptable->horizontalheader ();
  46. if (header)
  47. {
  48. For ( int i = 0; i < Header->count (); i++)
  49. {
  50. Qtablewidgetitem *item = Ui.m_ptable->horizontalheaderitem (i);
  51. if (!item)
  52. {
  53. continue;
  54. }
  55. ConTents + = Item->text () + ",";
  56. }
  57. ConTents + = "\ n";
  58. }
  59. For ( int i = 0; i < Ui.m_ptable->rowcount (); i++)
  60. {
  61. For ( int j = 0; J < Ui.m_ptable->columncount (); j + +)
  62. {
  63. qtablewidgetitem* item = Ui.m_ptable->item (i, j);
  64. if (!item)
  65. continue;
  66. QString str = Item->text ();
  67. Str.replace (",", "");
  68. ConTents + = str + ",";
  69. }
  70. ConTents + = "\ n";
  71. }
  72. Stream << conTents;
  73. File.close ();
  74. }
  75. if (Qmessagebox::yes = = Qmessagebox::information (0,qobject::tr ("File Export"), QString ("file export succeeded, do you want to open the file?") "), Qmessagebox::yes,qmessagebox::no))
  76. {
  77. Qsettings settings ("Hkey_local_machine\\software\\microsoft\\office", Qsettings::nativeformat);
  78. QString Szdefault, szpath;
  79. BOOL bsuccess;
  80. Szpath = Settings.value ("12.0/excel/installroot/path"). ToString ();
  81. if (Szpath.isempty ())
  82. Szpath = Settings.value ("11.0/excel/installroot/path"). ToString ();
  83. if (Szpath.isempty ())
  84. Szpath = Settings.value ("10.0/excel/installroot/path"). ToString ();
  85. if (Szpath.isempty ())
  86. Szpath = Settings.value ("9.0/excel/installroot/path"). ToString ();
  87. if (Szpath.isempty ())
  88. Szpath = Settings.value ("14.0/excel/installroot/path"). ToString ();
  89. if (Szpath.isempty ())
  90. {
  91. Qmessagebox::information (0, "prompt", "the system does not have Office installed, cannot view the failure report, please install Microsoft Office first.");
  92. return;
  93. }
  94. qprocess * proce = new qprocess;
  95. QString Szexcelexe = szpath + "Excel.exe";
  96. QString Szopen = "/safe";
  97. QString Szdoc = fileName;
  98. Qstringlist list;
  99. list<<szdoc;
  100. if (proce)
  101. {
  102. Proce->start (szexcelexe,list);
  103. proce->waitforstarted (5000); //need to wait for the boot to complete
  104. }<pre name="code" class="cpp" > delete proce;

http://blog.csdn.net/hai200501019/article/details/37538591

Qtablewidget Export to CSV table

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.