Hello. I am writing a small program for working with notes. The minimum functionality is now implemented: a note can be created, edited and deleted. For GUI I have Qt, the base is SQLite. In general, everything works and does what I want. But my programming experience tends to zero, so I suspect that there are many flaws that I cannot fix, because I don’t know where to look. I would like you to point out all the errors / problems / flaws that are evident. Interested in everything from the logic of class division to possible memory leaks.
Now there are essentially 12 classes:
- 4 windows:
MattyNotesMainWindow,MattySettingsDialog,addNoteDialog,MattyMessageBox - class for working with DB
DbManager - class for compiling SQL queries
QueryConstructor - proper class note
MattyNote - constructor class for visual display of the
MattyGroupBoxnote - class that deals with sorting and displaying notes
NoteHolder - class to manage css
MattyStyleSheetEditor - class to store lines and characters
Constants - watch
MattyClocks
I understand that it is unlikely that anyone will read all the code, so maybe you can look at the classes and their methods and say that it’s too much, or look at any one class and point out the flaws in it.
For example, the functions of connecting to the database, editing an existing note, and extracting all notes from the database:
bool DbManager::connect(const QString & path) { MattyNotesDb = QSqlDatabase::addDatabase("QSQLITE"); MattyNotesDb.setDatabaseName(path); if(QFile::exists(path)) { if (!MattyNotesDb.open()) { showIsNotOpenedError(); MattyNotesDb.close(); return false; } else { PathToDb = MattyNotesDb.databaseName(); } return true; } else { return false; } } bool DbManager::editNote(MattyNote & Note, int NoteId) { if (connected()) { QueryConstructor Edit; Edit.setTableName(QStringLiteral("Notes")); Edit.addWhereFieldValue(QStringLiteral("NoteId"), QString::number(NoteId)); QMap<QString, QString> NoteTemp; NoteTemp["NoteTitle"] = "\'" + Note.getTitle() + "\'"; NoteTemp["NoteType"] = "\'" + Note.getType() + "\'"; NoteTemp["NoteText"] = "\'" + Note.getText() + "\'"; NoteTemp["EventTime"] = "\'" + Note.getEventTime() + "\'"; NoteTemp["EventDate"] = "\'" + Note.getEventDate() + "\'"; NoteTemp["CrTime"] = "\'" + Note.getCrTime() + "\'"; NoteTemp["CrDate"] = "\'" + Note.getCrDate() + "\'"; NoteTemp["TypeId"] = QString::number(Note.getTypeId()); Edit.setWhatToSetFieldValue(NoteTemp); // QSqlQuery editNoteQuery; return editNoteQuery.exec(Edit.constructUpdateQuery()); } else { return false; } } QVector<MattyNoteRow> DbManager::showNotes() { if (connected()) { QVector<MattyNoteRow> VectorOfNoteRows; QueryConstructor SelectAllNotes; SelectAllNotes.setTableName(QStringLiteral("Notes")); SelectAllNotes.setOrderByClause("NoteId", Descending); QSqlQuery getAllNotesQuery(MattyNotesDb); if( getAllNotesQuery.exec(SelectAllNotes.constructSelectQuery())) { while (getAllNotesQuery.next()) { MattyNoteRow Row; Row.NoteId=getAllNotesQuery.value("NoteId").toInt(); Row.NoteTitle=getAllNotesQuery.value("NoteTitle").toString(); Row.NoteType=getAllNotesQuery.value("NoteType").toString(); Row.NoteText=getAllNotesQuery.value("NoteText").toString(); Row.EventTime=getAllNotesQuery.value("EventTime").toString(); Row.EventDate=getAllNotesQuery.value("EventDate").toString(); Row.CrTime=getAllNotesQuery.value("CrTime").toString(); Row.CrDate=getAllNotesQuery.value("CrDate").toString(); Row.TypeId=getAllNotesQuery.value("TypeId").toInt(); VectorOfNoteRows.push_back(Row); } } else { QMessageBox::critical(NULL, QObject::tr("Error"), getAllNotesQuery.lastError().text()); } return VectorOfNoteRows; } else { return QVector<MattyNoteRow>(); } } Sending notes to the form:
void NoteHolder::publishNotes(QWidget* ParentWidget) { erasePublishedNotes(ParentWidget); getAllNotes(); QVector<class MattyNote>::iterator NoteNumber; int i; for (NoteNumber = ListOfAllNotes.begin(), i=0; NoteNumber < ListOfAllNotes.end();NoteNumber++, i++) { MattyGroupBox* MyGroupBox = new MattyGroupBox(*NoteNumber, ParentWidget); ParentWidget->layout()->addWidget(MyGroupBox); } } void NoteHolder::erasePublishedNotes(QWidget* ParentWidget) { MattyGroupBox* MgbTemp; while ((MgbTemp = ParentWidget->findChild<MattyGroupBox*>()) != 0) { delete MgbTemp; } QGroupBox* GbTemp; while ((GbTemp = ParentWidget->findChild<QGroupBox*>()) != 0) { delete GbTemp; } } void NoteHolder::getAllNotes() { TotalNoteCount = 0; if (!ListOfAllNotes.isEmpty()) ListOfAllNotes.clear(); QVector<struct MattyNoteRow> ListOfRows = DbManager::showNotes(); for (int i = 0; i < ListOfRows.length();i++) { MattyNote TempNote(ListOfRows[i]); ListOfAllNotes.append(TempNote); TotalNoteCount++; } } All source code can be seen here: GitHub
Division into classes, their methods, challenges, dependencies, etc. here: Doxygen
user.nameanduser.emailfor a particular project in.gitconfig, but throughgit config --local user.name=...- Nick Volynkin ♦