#ifndef DATAMAP_H#define DATAMAP_H#include <QtGui>class QPushButton;class QTextEdit;class QLineEdit;class DataMap : public QWidget{ Q_OBJECTprivate: QMap<QString, QString>map; QMap<QString, QString>open_map; QLineEdit* pKey; QLineEdit* pText; QPushButton* pSave; QPushButton* pSaveData; QPushButton* pOpen; QTextEdit* txt;public: DataMap(QWidget* parent = 0);public slots: void slotSaveData(); void slotSave(); void slotOpen();};#endif // DATAMAP_H
DataMap::DataMap(QWidget* parent) : QWidget(parent){ setFixedSize(500, 90); pKey = new QLineEdit; pText = new QLineEdit; pSave = new QPushButton("Save"); pSaveData = new QPushButton("Save Data"); pOpen = new QPushButton("Open"); txt = new QTextEdit; QGridLayout* la = new QGridLayout; la->addWidget(pKey, 0, 0); la->addWidget(pText, 0, 1); la->addWidget(pSave, 0, 2); la->addWidget(pSaveData, 1, 0); la->addWidget(pOpen, 1, 1); la->addWidget(txt, 2, 0); setLayout(la); connect(pSave, SIGNAL(clicked()), SLOT(slotSave())); connect(pSaveData, SIGNAL(clicked()), SLOT(slotSaveData())); connect(pOpen, SIGNAL(clicked()), SLOT(slotOpen()));}
void DataMap::slotOpen(){ QFile pFile("wh.dat"); if(pFile.open(QIODevice::ReadOnly)){ QDataStream stream(&pFile); stream >> open_map; } else QMessageBox::warning(0, "error", "Error open"); pFile.close(); if(open_map.isEmpty()){ qDebug() << "map clean"; } else qDebug() << "map full"; QMap<QString, QString>::iterator it = open_map.find("A239"); QString str = it.value(); txt->setPlainText(str);}
C++ (Qt)QMap<QString, QString>::iterator it = open_map.find("A239"); if(it == open_map.end()) qDebug("Not found");else txt->setPlainText(it.value());
void DataMap::slotSave(){ QString str_key = pKey->text(); QString str_text = pText->text(); if(!map.contains(str_key)){ map.insert(str_key, str_text); } else QMessageBox::warning(0, "error", "error12"); pKey->setText(""); pText->setText("");}void DataMap::slotSaveData(){ QFile file("wh.dat"); if(file.open(QIODevice::WriteOnly)){ QDataStream stream(&file); stream << map; } else QMessageBox::warning(0, "error", "Error Write"); file.close();}