вот тебе кусок кода из моего проекта
выкинь лишнее и будет тебе один из вариантов работы с файлами
Код | void CSelectResult::exportCard() { QPopupMenu oMenu(this); oMenu.insertItem("Ýìáîñèíã", 0, 0); oMenu.insertItem("Íàìàãíè÷èâàíèå", 1, 1); int m_nType = oMenu.exec(mapToGlobal (QPoint(toolButtonExport->x(), toolButtonExport->y() + 32)));
QString soFilter; if (0 == m_nType) soFilter = "*.csv"; else soFilter = "*.iso";
QString soFileName = QFileDialog::getSaveFileName("filename", soFilter, this); if (!soFileName.isEmpty()) { QFile oFile; oFile.setName(soFileName + (m_nType == 0 ? ".csv" : ".iso")); oFile.open(IO_WriteOnly); QTextStream stream( &oFile ); stream.setEncoding(QTextStream::Latin1);
QSqlCursor *opCursor = m_opTable->sqlCursor(); QString soLine; QSqlQuery oQuery(m_opDB);
if (0 == m_nType) soLine = "kod;last_name;first_name;full_name\n"; else if (1 == m_nType) soLine = "[SETUP]\x0D\x0A\x43\x41RDTYPE =0\x0D\x0APARITY1 =1\x0D\x0APARITY2 =1\x0D\x0APARITY3 =1\x0D\x0A\x42PC1 =7\x0D\x0A\x42PC2 =5\x0D\x0A\x42PC3 =5\x0D\x0ASS1 =5\x0D\x0ASS2 =11\x0D\x0ASS3 =11\x0D\x0A\x45S1 =31\x0D\x0A\x45S2 =15\x0D\x0A\x45S3 =15\x0D\x0A\x0D\x0A[DATA]\x0D\x0A";
QString soSQL; stream << soLine; while (opCursor->next()) { int n = opCursor->value(0).toInt(); soSQL.sprintf("SELECT dc.id, dc.last_name, dc.first_name, dt.descript, dc.mag_stripe FROM d_client dc, d_card_type dt " " WHERE dc.card_type = dt.id AND dc.id = %d", n);
if (oQuery.exec(soSQL) && oQuery.next()) { if (0 == m_nType) { soLine = oQuery.value(0).toString() + ";" + oQuery.value(1).toString() + ";" + oQuery.value(2).toString() + ";" + oQuery.value(3).toString() + "\x0D\x0A"; } else if (1 == m_nType) { soLine = "\x01" + oQuery.value(0).toString() + "/" + translitWord(oQuery.value(1).toString()) + "=" + translitWord(oQuery.value(2).toString()) + "\x0D\x0A" + "\x02:<>" + oQuery.value(4).toString() + "\x0D\x0A\x03\x0D\x0A\x0D\x0A"; } stream << soLine.local8Bit(); } }
oFile.close(); } }
|
Добавлено @ 09:31 вот за однои и косяк в своем коде нашел 
Код | QPopupMenu oMenu(this); oMenu.insertItem("Ýìáîñèíã", 0, 0); oMenu.insertItem("Íàìàãíè÷èâàíèå", 1, 1); int m_nType = oMenu.exec(mapToGlobal (QPoint(toolButtonExport->x(), toolButtonExport->y() + 32)));
QString soFilter; if (0 == m_nType) soFilter = "*.csv"; else soFilter = "*.iso";
|
попап меню можно закрыть не выбирая ни какой из его элементов |