I have an app which allows me to find files containing a certain phrase.
When i click on the submit Button the content of the QTableView should disappear and i fill the Table in a function.
Code: gui.cpp
<code>GUI::GUI(QWidget* parent) : QWidget(parent),
path(new QLabel("nnPath to directory", this)),
welcome(new QLabel("Application is finding log files with a given phrase.", this)),
text(new QLabel("nHello World", this)),
searchword(new QLabel("nSearch phrase", this)),
path_edit(new QLineEdit(this)),
searchword_edit(new QLineEdit(this)),
output_view(new QTableView(this)),
output_model(new QStandardItemModel(0, 3, this)),
submitButton(new QPushButton("Click/press Enter to get files")),
browseButton(new QPushButton("Browse....")),
reset_Dir(new QPushButton("Reset Directory"))
{
setWindowTitle("Log File Search Application");
setBaseSize(2000, 1100);
QVBoxLayout* mainLayout = new QVBoxLayout(this);
QString default_path ="a path";
//QLabel
welcome->setStyleSheet("color: black; font: bold 55px;");
text->setStyleSheet("color: black; font: 35px;");
path->setStyleSheet("font: 25px;");
searchword->setStyleSheet("font:25px;");
//QLineEdit
path_edit->setStyleSheet("font:25px; height: 50px;");
path_edit->setText(default_path);
searchword_edit->setStyleSheet("font:25px; height: 50px;");
//QPushButton
submitButton->setStyleSheet("background: darkgrey; height: 70px; width: 250px; font: 20px;");
browseButton->setStyleSheet("background: darkgrey; height: 40px; font: 20px;");
reset_Dir->setStyleSheet("background: darkgrey; height: 40px; font: 20px;");
//Output
output_model->setHorizontalHeaderLabels(QStringList() << "Date" << "Time" << "Filepath");
output_view->setModel(output_model);
output_view->setStyleSheet("background-color: white; font: 16px; color: black;");
output_view->setColumnWidth(0, width() / 3);
output_view->setColumnWidth(1, width() / 3);
output_view->horizontalHeader()->setSectionResizeMode(2, QHeaderView::Stretch); //column stretches when window is resized
output_view->setSortingEnabled(true); //sorting table columns
output_view->setFocusPolicy(Qt::NoFocus); //TabFocus is never set at QTableView
//QHBoxLayout: /questions/42100394/how-to-align-children-in-a-qhboxlayout-left-center-and-right
QHBoxLayout* path_layout = new QHBoxLayout();
path_layout->addWidget(path_edit, 3);
path_layout->addWidget(browseButton, 0.1);
path_layout->addWidget(reset_Dir, 0.1);
QHBoxLayout* searchBtn_layout = new QHBoxLayout();
searchBtn_layout->addWidget(submitButton);
searchBtn_layout->addStretch();
//to 'click' with Keyboard
submitButton->setAutoDefault(true);
browseButton->setAutoDefault(true);
reset_Dir->setAutoDefault(true);
//connections
connect(browseButton, &QPushButton::clicked, this, &GUI::getDir);
connect(submitButton, &QPushButton::clicked, this, &GUI::checkInput);
connect(searchword_edit, &QLineEdit::returnPressed, this, &GUI::checkInput);
connect(path_edit, &QLineEdit::returnPressed, this, &GUI::checkInput);
connect(reset_Dir, &QPushButton::clicked, this, &GUI::resetDir);
//add to VBox
mainLayout->addWidget(welcome);
mainLayout->addWidget(text);
mainLayout->addWidget(path);
mainLayout->addLayout(path_layout);
mainLayout->addWidget(searchword);
mainLayout->addWidget(searchword_edit);
mainLayout->addLayout(searchBtn_layout);
mainLayout->addWidget(output_view);
setTabOrder(path_edit, browseButton);
setTabOrder(browseButton, reset_Dir);
setLayout(mainLayout);
}
void GUI::checkInput() {
QMessageBox msg;
if (path_edit->text().isEmpty() && searchword_edit->text().isEmpty()) {
msg.setText("You need to fill out the form!");
msg.exec();
}
else if (path_edit->text().isEmpty())
{
msg.setText("You need to give a path!");
msg.exec();
}
else if (searchword_edit->text().isEmpty())
{
msg.setText("You need to give a phrase!");
msg.exec();
}
else
{
validInput(path_edit, searchword_edit, output_model, output_view);
}
}
void GUI::validInput(QLineEdit* path, QLineEdit* search, QStandardItemModel* model, QTableView* view) {
QString qstr = path->text();
std::string path_string = qstr.toStdString();
QString qstr2 = search->text();
std::string search_string = qstr2.toStdString();
callDir(path_string, search_string, model, view);
}
void GUI::getDir() {
QString default_path = "C:/Users/sehos/AppData/Local/CADstar/CS.Core/cache/logs";
QString dirPath = QFileDialog::getExistingDirectory(nullptr, tr("Open Dir"), QDir::currentPath());
if (!dirPath.isEmpty()) path_edit->setText(dirPath);
}
</code>
<code>GUI::GUI(QWidget* parent) : QWidget(parent),
path(new QLabel("nnPath to directory", this)),
welcome(new QLabel("Application is finding log files with a given phrase.", this)),
text(new QLabel("nHello World", this)),
searchword(new QLabel("nSearch phrase", this)),
path_edit(new QLineEdit(this)),
searchword_edit(new QLineEdit(this)),
output_view(new QTableView(this)),
output_model(new QStandardItemModel(0, 3, this)),
submitButton(new QPushButton("Click/press Enter to get files")),
browseButton(new QPushButton("Browse....")),
reset_Dir(new QPushButton("Reset Directory"))
{
setWindowTitle("Log File Search Application");
setBaseSize(2000, 1100);
QVBoxLayout* mainLayout = new QVBoxLayout(this);
QString default_path ="a path";
//QLabel
welcome->setStyleSheet("color: black; font: bold 55px;");
text->setStyleSheet("color: black; font: 35px;");
path->setStyleSheet("font: 25px;");
searchword->setStyleSheet("font:25px;");
//QLineEdit
path_edit->setStyleSheet("font:25px; height: 50px;");
path_edit->setText(default_path);
searchword_edit->setStyleSheet("font:25px; height: 50px;");
//QPushButton
submitButton->setStyleSheet("background: darkgrey; height: 70px; width: 250px; font: 20px;");
browseButton->setStyleSheet("background: darkgrey; height: 40px; font: 20px;");
reset_Dir->setStyleSheet("background: darkgrey; height: 40px; font: 20px;");
//Output
output_model->setHorizontalHeaderLabels(QStringList() << "Date" << "Time" << "Filepath");
output_view->setModel(output_model);
output_view->setStyleSheet("background-color: white; font: 16px; color: black;");
output_view->setColumnWidth(0, width() / 3);
output_view->setColumnWidth(1, width() / 3);
output_view->horizontalHeader()->setSectionResizeMode(2, QHeaderView::Stretch); //column stretches when window is resized
output_view->setSortingEnabled(true); //sorting table columns
output_view->setFocusPolicy(Qt::NoFocus); //TabFocus is never set at QTableView
//QHBoxLayout: /questions/42100394/how-to-align-children-in-a-qhboxlayout-left-center-and-right
QHBoxLayout* path_layout = new QHBoxLayout();
path_layout->addWidget(path_edit, 3);
path_layout->addWidget(browseButton, 0.1);
path_layout->addWidget(reset_Dir, 0.1);
QHBoxLayout* searchBtn_layout = new QHBoxLayout();
searchBtn_layout->addWidget(submitButton);
searchBtn_layout->addStretch();
//to 'click' with Keyboard
submitButton->setAutoDefault(true);
browseButton->setAutoDefault(true);
reset_Dir->setAutoDefault(true);
//connections
connect(browseButton, &QPushButton::clicked, this, &GUI::getDir);
connect(submitButton, &QPushButton::clicked, this, &GUI::checkInput);
connect(searchword_edit, &QLineEdit::returnPressed, this, &GUI::checkInput);
connect(path_edit, &QLineEdit::returnPressed, this, &GUI::checkInput);
connect(reset_Dir, &QPushButton::clicked, this, &GUI::resetDir);
//add to VBox
mainLayout->addWidget(welcome);
mainLayout->addWidget(text);
mainLayout->addWidget(path);
mainLayout->addLayout(path_layout);
mainLayout->addWidget(searchword);
mainLayout->addWidget(searchword_edit);
mainLayout->addLayout(searchBtn_layout);
mainLayout->addWidget(output_view);
setTabOrder(path_edit, browseButton);
setTabOrder(browseButton, reset_Dir);
setLayout(mainLayout);
}
void GUI::checkInput() {
QMessageBox msg;
if (path_edit->text().isEmpty() && searchword_edit->text().isEmpty()) {
msg.setText("You need to fill out the form!");
msg.exec();
}
else if (path_edit->text().isEmpty())
{
msg.setText("You need to give a path!");
msg.exec();
}
else if (searchword_edit->text().isEmpty())
{
msg.setText("You need to give a phrase!");
msg.exec();
}
else
{
validInput(path_edit, searchword_edit, output_model, output_view);
}
}
void GUI::validInput(QLineEdit* path, QLineEdit* search, QStandardItemModel* model, QTableView* view) {
QString qstr = path->text();
std::string path_string = qstr.toStdString();
QString qstr2 = search->text();
std::string search_string = qstr2.toStdString();
callDir(path_string, search_string, model, view);
}
void GUI::getDir() {
QString default_path = "C:/Users/sehos/AppData/Local/CADstar/CS.Core/cache/logs";
QString dirPath = QFileDialog::getExistingDirectory(nullptr, tr("Open Dir"), QDir::currentPath());
if (!dirPath.isEmpty()) path_edit->setText(dirPath);
}
</code>
GUI::GUI(QWidget* parent) : QWidget(parent),
path(new QLabel("nnPath to directory", this)),
welcome(new QLabel("Application is finding log files with a given phrase.", this)),
text(new QLabel("nHello World", this)),
searchword(new QLabel("nSearch phrase", this)),
path_edit(new QLineEdit(this)),
searchword_edit(new QLineEdit(this)),
output_view(new QTableView(this)),
output_model(new QStandardItemModel(0, 3, this)),
submitButton(new QPushButton("Click/press Enter to get files")),
browseButton(new QPushButton("Browse....")),
reset_Dir(new QPushButton("Reset Directory"))
{
setWindowTitle("Log File Search Application");
setBaseSize(2000, 1100);
QVBoxLayout* mainLayout = new QVBoxLayout(this);
QString default_path ="a path";
//QLabel
welcome->setStyleSheet("color: black; font: bold 55px;");
text->setStyleSheet("color: black; font: 35px;");
path->setStyleSheet("font: 25px;");
searchword->setStyleSheet("font:25px;");
//QLineEdit
path_edit->setStyleSheet("font:25px; height: 50px;");
path_edit->setText(default_path);
searchword_edit->setStyleSheet("font:25px; height: 50px;");
//QPushButton
submitButton->setStyleSheet("background: darkgrey; height: 70px; width: 250px; font: 20px;");
browseButton->setStyleSheet("background: darkgrey; height: 40px; font: 20px;");
reset_Dir->setStyleSheet("background: darkgrey; height: 40px; font: 20px;");
//Output
output_model->setHorizontalHeaderLabels(QStringList() << "Date" << "Time" << "Filepath");
output_view->setModel(output_model);
output_view->setStyleSheet("background-color: white; font: 16px; color: black;");
output_view->setColumnWidth(0, width() / 3);
output_view->setColumnWidth(1, width() / 3);
output_view->horizontalHeader()->setSectionResizeMode(2, QHeaderView::Stretch); //column stretches when window is resized
output_view->setSortingEnabled(true); //sorting table columns
output_view->setFocusPolicy(Qt::NoFocus); //TabFocus is never set at QTableView
//QHBoxLayout: /questions/42100394/how-to-align-children-in-a-qhboxlayout-left-center-and-right
QHBoxLayout* path_layout = new QHBoxLayout();
path_layout->addWidget(path_edit, 3);
path_layout->addWidget(browseButton, 0.1);
path_layout->addWidget(reset_Dir, 0.1);
QHBoxLayout* searchBtn_layout = new QHBoxLayout();
searchBtn_layout->addWidget(submitButton);
searchBtn_layout->addStretch();
//to 'click' with Keyboard
submitButton->setAutoDefault(true);
browseButton->setAutoDefault(true);
reset_Dir->setAutoDefault(true);
//connections
connect(browseButton, &QPushButton::clicked, this, &GUI::getDir);
connect(submitButton, &QPushButton::clicked, this, &GUI::checkInput);
connect(searchword_edit, &QLineEdit::returnPressed, this, &GUI::checkInput);
connect(path_edit, &QLineEdit::returnPressed, this, &GUI::checkInput);
connect(reset_Dir, &QPushButton::clicked, this, &GUI::resetDir);
//add to VBox
mainLayout->addWidget(welcome);
mainLayout->addWidget(text);
mainLayout->addWidget(path);
mainLayout->addLayout(path_layout);
mainLayout->addWidget(searchword);
mainLayout->addWidget(searchword_edit);
mainLayout->addLayout(searchBtn_layout);
mainLayout->addWidget(output_view);
setTabOrder(path_edit, browseButton);
setTabOrder(browseButton, reset_Dir);
setLayout(mainLayout);
}
void GUI::checkInput() {
QMessageBox msg;
if (path_edit->text().isEmpty() && searchword_edit->text().isEmpty()) {
msg.setText("You need to fill out the form!");
msg.exec();
}
else if (path_edit->text().isEmpty())
{
msg.setText("You need to give a path!");
msg.exec();
}
else if (searchword_edit->text().isEmpty())
{
msg.setText("You need to give a phrase!");
msg.exec();
}
else
{
validInput(path_edit, searchword_edit, output_model, output_view);
}
}
void GUI::validInput(QLineEdit* path, QLineEdit* search, QStandardItemModel* model, QTableView* view) {
QString qstr = path->text();
std::string path_string = qstr.toStdString();
QString qstr2 = search->text();
std::string search_string = qstr2.toStdString();
callDir(path_string, search_string, model, view);
}
void GUI::getDir() {
QString default_path = "C:/Users/sehos/AppData/Local/CADstar/CS.Core/cache/logs";
QString dirPath = QFileDialog::getExistingDirectory(nullptr, tr("Open Dir"), QDir::currentPath());
if (!dirPath.isEmpty()) path_edit->setText(dirPath);
}
detection.cpp
<code>namespace fs = std::filesystem;
bool checkForWord(std::string filename, std::string word, std::set<std::string>& files) {
int offset;
std::string line;
std::ifstream file;
file.open(filename);
if (file.is_open()) {
bool validWord = false;
while (getline(file, line)) {
if ((offset = line.find(word, 0)) != std::string::npos) {
files.insert("n" + filename);
validWord = true;
}
}
file.close();
return validWord;
}
else QMessageBox::warning(nullptr, "File not found!", "File could not be opened!");
}
void printOutput(std::set<std::string> files, QStandardItemModel* model, QTableView* view)
{
model->setRowCount(files.size());
int row = 0;
for (const auto& str : files)
{
//extracting filename from filepath
size_t lastSlash = str.find_last_of("/\");
if (lastSlash == std::string::npos) lastSlash = -1;
std::string fName = str.substr(lastSlash +1); //YYYY-MM-DD_hh-mm-ss.txt
size_t timeUnderScore = fName.find("_");
std::string time = fName.substr(timeUnderScore+1,8);
std::replace(time.begin(), time.end(), '-', ':'); //hh-mm-ss -> hh:mm:ss
size_t dateUnderScore = fName.find("_");
std::string date = fName.substr(0,dateUnderScore);
std::replace(date.begin(), date.end(), '-', '/'); //YYYY-MM-DD -> YYYY/MM/DD
//creating QStandardItem to set in QTableView
auto* displayDate = new QStandardItem(QString::fromStdString(date));
auto* displayTime = new QStandardItem(QString::fromStdString(time));
auto* fileItem = new QStandardItem(QString::fromStdString(str));
//flags to set the Table read-only
displayDate->setFlags(Qt::ItemIsEditable);
displayTime->setFlags(Qt::ItemIsEditable);
fileItem->setFlags(Qt::ItemIsEditable);
model->setItem(row, 0, displayDate);
model->setItem(row, 1, displayTime);
model->setItem(row, 2, fileItem);
//Output with QMessageBox
/*
std::string combine;
for (const auto& str : files) {
combine += str + "n";
}
//The Filepaths are printed.
QMessageBox msg;
QString filestr = QString::fromStdString(combine);
msg.setText(QString::fromStdString(combine));
msg.exec();
*/
row++;
}
}
void callDir(std::string dir, std::string search, QStandardItemModel* model, QTableView* view) {
std::string folderPath = dir;
std::set<std::string> txtFiles;
std::set<std::string> files;
bool validW = false;
view->clearMask();
try {
for (const auto& entry : fs::directory_iterator(folderPath)) {
if (entry.path().extension() == ".txt") txtFiles.insert(entry.path().string());
}
}
catch (const std::exception& e)
{
QMessageBox::critical(nullptr, "Invalid Path", "Path is invalid!!");
}
for (const std::string& file : txtFiles) {
if (checkForWord(file, search, files)) validW = true;
}
if (!validW) QMessageBox::critical(nullptr, "Word not found", QString("The word '%1' given could not be found in any files.").arg(QString::fromStdString(search)));
else if (!files.empty()) printOutput(files, model, view);
}
</code>
<code>namespace fs = std::filesystem;
bool checkForWord(std::string filename, std::string word, std::set<std::string>& files) {
int offset;
std::string line;
std::ifstream file;
file.open(filename);
if (file.is_open()) {
bool validWord = false;
while (getline(file, line)) {
if ((offset = line.find(word, 0)) != std::string::npos) {
files.insert("n" + filename);
validWord = true;
}
}
file.close();
return validWord;
}
else QMessageBox::warning(nullptr, "File not found!", "File could not be opened!");
}
void printOutput(std::set<std::string> files, QStandardItemModel* model, QTableView* view)
{
model->setRowCount(files.size());
int row = 0;
for (const auto& str : files)
{
//extracting filename from filepath
size_t lastSlash = str.find_last_of("/\");
if (lastSlash == std::string::npos) lastSlash = -1;
std::string fName = str.substr(lastSlash +1); //YYYY-MM-DD_hh-mm-ss.txt
size_t timeUnderScore = fName.find("_");
std::string time = fName.substr(timeUnderScore+1,8);
std::replace(time.begin(), time.end(), '-', ':'); //hh-mm-ss -> hh:mm:ss
size_t dateUnderScore = fName.find("_");
std::string date = fName.substr(0,dateUnderScore);
std::replace(date.begin(), date.end(), '-', '/'); //YYYY-MM-DD -> YYYY/MM/DD
//creating QStandardItem to set in QTableView
auto* displayDate = new QStandardItem(QString::fromStdString(date));
auto* displayTime = new QStandardItem(QString::fromStdString(time));
auto* fileItem = new QStandardItem(QString::fromStdString(str));
//flags to set the Table read-only
displayDate->setFlags(Qt::ItemIsEditable);
displayTime->setFlags(Qt::ItemIsEditable);
fileItem->setFlags(Qt::ItemIsEditable);
model->setItem(row, 0, displayDate);
model->setItem(row, 1, displayTime);
model->setItem(row, 2, fileItem);
//Output with QMessageBox
/*
std::string combine;
for (const auto& str : files) {
combine += str + "n";
}
//The Filepaths are printed.
QMessageBox msg;
QString filestr = QString::fromStdString(combine);
msg.setText(QString::fromStdString(combine));
msg.exec();
*/
row++;
}
}
void callDir(std::string dir, std::string search, QStandardItemModel* model, QTableView* view) {
std::string folderPath = dir;
std::set<std::string> txtFiles;
std::set<std::string> files;
bool validW = false;
view->clearMask();
try {
for (const auto& entry : fs::directory_iterator(folderPath)) {
if (entry.path().extension() == ".txt") txtFiles.insert(entry.path().string());
}
}
catch (const std::exception& e)
{
QMessageBox::critical(nullptr, "Invalid Path", "Path is invalid!!");
}
for (const std::string& file : txtFiles) {
if (checkForWord(file, search, files)) validW = true;
}
if (!validW) QMessageBox::critical(nullptr, "Word not found", QString("The word '%1' given could not be found in any files.").arg(QString::fromStdString(search)));
else if (!files.empty()) printOutput(files, model, view);
}
</code>
namespace fs = std::filesystem;
bool checkForWord(std::string filename, std::string word, std::set<std::string>& files) {
int offset;
std::string line;
std::ifstream file;
file.open(filename);
if (file.is_open()) {
bool validWord = false;
while (getline(file, line)) {
if ((offset = line.find(word, 0)) != std::string::npos) {
files.insert("n" + filename);
validWord = true;
}
}
file.close();
return validWord;
}
else QMessageBox::warning(nullptr, "File not found!", "File could not be opened!");
}
void printOutput(std::set<std::string> files, QStandardItemModel* model, QTableView* view)
{
model->setRowCount(files.size());
int row = 0;
for (const auto& str : files)
{
//extracting filename from filepath
size_t lastSlash = str.find_last_of("/\");
if (lastSlash == std::string::npos) lastSlash = -1;
std::string fName = str.substr(lastSlash +1); //YYYY-MM-DD_hh-mm-ss.txt
size_t timeUnderScore = fName.find("_");
std::string time = fName.substr(timeUnderScore+1,8);
std::replace(time.begin(), time.end(), '-', ':'); //hh-mm-ss -> hh:mm:ss
size_t dateUnderScore = fName.find("_");
std::string date = fName.substr(0,dateUnderScore);
std::replace(date.begin(), date.end(), '-', '/'); //YYYY-MM-DD -> YYYY/MM/DD
//creating QStandardItem to set in QTableView
auto* displayDate = new QStandardItem(QString::fromStdString(date));
auto* displayTime = new QStandardItem(QString::fromStdString(time));
auto* fileItem = new QStandardItem(QString::fromStdString(str));
//flags to set the Table read-only
displayDate->setFlags(Qt::ItemIsEditable);
displayTime->setFlags(Qt::ItemIsEditable);
fileItem->setFlags(Qt::ItemIsEditable);
model->setItem(row, 0, displayDate);
model->setItem(row, 1, displayTime);
model->setItem(row, 2, fileItem);
//Output with QMessageBox
/*
std::string combine;
for (const auto& str : files) {
combine += str + "n";
}
//The Filepaths are printed.
QMessageBox msg;
QString filestr = QString::fromStdString(combine);
msg.setText(QString::fromStdString(combine));
msg.exec();
*/
row++;
}
}
void callDir(std::string dir, std::string search, QStandardItemModel* model, QTableView* view) {
std::string folderPath = dir;
std::set<std::string> txtFiles;
std::set<std::string> files;
bool validW = false;
view->clearMask();
try {
for (const auto& entry : fs::directory_iterator(folderPath)) {
if (entry.path().extension() == ".txt") txtFiles.insert(entry.path().string());
}
}
catch (const std::exception& e)
{
QMessageBox::critical(nullptr, "Invalid Path", "Path is invalid!!");
}
for (const std::string& file : txtFiles) {
if (checkForWord(file, search, files)) validW = true;
}
if (!validW) QMessageBox::critical(nullptr, "Word not found", QString("The word '%1' given could not be found in any files.").arg(QString::fromStdString(search)));
else if (!files.empty()) printOutput(files, model, view);
}
I already tried using
<code>`model->clear();
model->removeRows(0, output_model->rowCount());`
</code>
<code>`model->clear();
model->removeRows(0, output_model->rowCount());`
</code>
`model->clear();
model->removeRows(0, output_model->rowCount());`
Maybe I placed it in the wrong place when it should work but it did not work for me.
New contributor
mallocFreestyle is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.