-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmainwindow.cpp
131 lines (125 loc) · 4.1 KB
/
mainwindow.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QFile>
#include <QTextStream>
#include <QDebug>
#include <QKeyEvent>
#include <QInputDialog>
#include <QMessageBox>
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
csvModel = new QStandardItemModel(this);
ui->tableView->setModel(csvModel);
QFile file(":/exampleTable.csv"); //To read csv file
if ( !file.open(QFile::ReadOnly | QFile::Text) ) {
qDebug() << "File not exists";
} else {
QTextStream in(&file);
while (!in.atEnd())
{
QString line = in.readLine();
QList<QStandardItem *> standardItemsList;
for (QString item : line.split(",")) {
standardItemsList.append(new QStandardItem(item));
}
csvModel->insertRow(csvModel->rowCount(), standardItemsList);
}
file.close();
}
connect(this, SIGNAL(openDialog()), this, SLOT(createDialogSlot())); //signal slot to create a window to search
connect(this, SIGNAL(searchString(QString)), this, SLOT(searchStringSlot(QString))); //to search a string
QObject::connect(csvModel, SIGNAL(itemChanged(QStandardItem*)), this, SLOT(onChanged(QStandardItem*)));
}
MainWindow::~MainWindow()
{
delete ui;
delete csvModel;
}
void MainWindow::keyPressEvent(QKeyEvent *e) //keypressevent to take Control + F event
{
if ( (e->key() == Qt::Key_F) && QApplication::keyboardModifiers() && Qt::ControlModifier)
{
emit openDialog();
}
else
{
qDebug()<<"Not Correct Key";
}
}
void MainWindow::createDialogSlot()
{
qDebug()<<Q_FUNC_INFO;
QInputDialog *newDialog = new QInputDialog();
if(QInputDialog::Accepted){
QString bar = newDialog->getText(this,tr("QInputDialog::getText()"),tr("Keyword:"),QLineEdit::Normal);
qDebug()<<"Bar"<<bar;
emit searchString(bar);
}
}
void MainWindow::selectedClear() //to clear for second search
{
qDebug()<<Q_FUNC_INFO<<lastSearched.count();
for(int k=0; k<lastSearched.count(); k++)
{
QModelIndex modelIndex = lastSearched.at(k);
((QStandardItemModel*)modelIndex.model())->item(modelIndex.row(),modelIndex.column())->setData(QBrush(Qt::transparent),Qt::BackgroundRole);
}
lastSearched.clear();
}
void MainWindow::searchStringSlot(QString str) //to search a string in table
{
selectedClear();
qDebug()<<Q_FUNC_INFO<<str;
for (int index = 0; index < csvModel->columnCount(); index++)
{
QList<QStandardItem*> foundLst = csvModel->findItems(str, Qt::MatchExactly, index);
int count = foundLst.count();
for(int k=0; k<count; k++)
{
countFlag=true;
QModelIndex modelIndex = csvModel->indexFromItem(foundLst[k]);
lastSearched.append(modelIndex);
((QStandardItemModel*)modelIndex.model())->item(modelIndex.row(),index)->setData(QBrush(Qt::yellow),Qt::BackgroundRole);
}
}
if(countFlag==false)
{
QMessageBox msgBox;
msgBox.setText("Not Found..");
msgBox.exec();
}
}
void MainWindow::onChanged(QStandardItem *tempItem)
{
qDebug()<<Q_FUNC_INFO;
tempItem->text();
tempItem->column();
int row = tempItem->row();
qDebug()<<"text::"<<tempItem->text()<<"Column"<<tempItem->column()<<"Row"<<tempItem->row();
QFile file("/Users/suru1994/DemosQT/csvDemo/exampleTable.csv"); //To write Data in csv file
if ( !file.open(QFile::WriteOnly|QFile::Text)) {
qDebug() << "File not exists";
}
else
{
for( int row = 0; row < csvModel->rowCount(); ++row )
{
QString line;
for ( int col = 0; col < csvModel->columnCount(); ++col )
{
// qDebug() << csvModel->index(row,col).data();
line.append(csvModel->index(row,col).data().toString());
if(col+1 != csvModel->columnCount())
{
line.append(",");
}
}
QTextStream out(&file);
out <<line<<"\n";
}
}
file.close();
}