# Simple Qt GUI to list user-created categories and tasks

I've written a very simple program using Qt that I intend to continue working on as I continue learning C++ and Qt.

I'm mostly wondering about my general code and comment style, specifically in terms of C++, less so about the actual program (which is boring and featureless at the moment).

The main file of interest is mainwindow.cpp:

#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QInputDialog>
#include <iostream>

MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow) {
ui->setupUi(this);

// at startup, display that there's no categories
noCatItem = std::make_unique<QTreeWidgetItem>();
noCatItem->setIcon(0, QIcon(arrow));
noCatItem->setText(0, "No Catagories");

}

MainWindow::~MainWindow() {
delete ui;
}

/**
* @brief MainWindow::on_addCatButton_clicked Inserts a new category at the end of the current list of categories.
*/
unselectCategories();

bool ok;
auto catName = QInputDialog::getText(this, "Add Category", "Category Name:",
QLineEdit::Normal, QString(), &ok);

if (ok && !catName.isEmpty()) {
auto *newItem = new QTreeWidgetItem();

// if "no categories" is still there, remove it and make this the "active" category
if (curIsNoCat()) {
ui->catTreeWidget->takeTopLevelItem(0);
newItem->setIcon(0, QIcon(arrow));
}

newItem->setText(0, catName);
}
}

/**
*/
unselectCategories();

// if there's no current item, do nothing
if (!ui->catTreeWidget->currentItem()) return;

bool ok;
QLineEdit::Normal, QString(), &ok);

auto *cur = ui->catTreeWidget->currentItem();

auto *newItem = new QTreeWidgetItem(cur);
}
}

/**
* @brief MainWindow::on_nextButton_clicked Makes the next category active.
* The "next" category is the subsequently displayed one.
*/
void MainWindow::on_nextButton_clicked() {
unselectCategories();

// make the now previous category "inactive"
ui->catTreeWidget->topLevelItem(curCatIndex)->setIcon(0, QIcon());

// get the next category index
if (++curCatIndex >= ui->catTreeWidget->topLevelItemCount())
curCatIndex = 0; // wrap

// make the new category "active"
ui->catTreeWidget->topLevelItem(curCatIndex)->setIcon(0, QIcon(arrow));
}

/**
* @brief MainWindow::unselectCategories Deselcts all selected categories, which also makes "add task' un-clickable.
*/
void MainWindow::unselectCategories() {
// can only unselect if there's items selected
if (!ui->catTreeWidget->selectedItems().isEmpty()) {
ui->catTreeWidget->clearSelection();
}
}

/**
*/
void MainWindow::on_catTreeWidget_itemSelectionChanged() {
if (curIsNoCat()) return;
// if a top-level category is selected, enable the add tasks button
for (auto &item : ui->catTreeWidget->selectedItems()) {
if (!item->parent()) {
break;
}
}
}

/**
* @brief MainWindow::curIsNoCat Determines whether the current item is the "no category" item.
* @return true if "no categories" is still in the list.
*/
bool MainWindow::curIsNoCat() {
return ui->catTreeWidget->indexOfTopLevelItem(noCatItem.get()) != -1;
}

• It's very hard to review without the definitions of MainWindow and Ui::MainWindow. You should include these directly in the question, rather than behind a link. Nov 12, 2018 at 14:06
• If your problem is solved do not hesitate to validate the answer or/and to vote it up if it would useful. Nov 12, 2018 at 17:08
• Thanks for the heads up, Toby, I'm not overly familiar with Code Review and didn't want to post too many huge blobs of code. I'll keep that in mind going forward! Nov 14, 2018 at 15:22

## Include order :

You completely reversered order of includes. You could include standard header first, then 3rd party header and finally yours. Here's the two way and the explanation.

## Raw pointer

You use std::unique_ptr for some widget but not for all nor for ui, why? If you do, you don't want to delete ui anymore thanks to RAII.

## Use name that makes sense

Try to use full name, eg: curIsNoCat can be renamed to hasNoCategory

• You Can, per example, (1) make a setActiveCategory function, where you set the icon of the given element to make it active. (2) have a removedNoCategories which remove the "No categories" element and return true or false if it was present (and so, removed). (3) in on_addCatButton_clicked you change to if (removedNoCategories()) setActiveCategory (newWidget); and you got rid of comments making code explicit. (It's just a proof of concept, for the purpose of the example) Nov 11, 2018 at 0:49
• @Snowhawk you're right, I should not be so extreme about #include ordre. I've modified my response. Nov 11, 2018 at 10:20
• @Cameron Working with C++'s smart pointers and Qt's raw pointers can be jarring. I generally prefer to stick with the new QWidget syntax when dealing with UI widgets, if just so I don't accidentally add a shared_ptr to some parent widget using get().