Hi all,
Please take a look at these files:
List.h:
#ifndef LIST_H
#define LIST_H
#include <QMainWindow>
#include "task.h"
#include <QVector>
{
Q_OBJECT
public:
explicit List
(QWidget *parent
= nullptr
);
void updateStatus();
~List();
public slots:
void addTask();
void removeTask(Task*);
void taskStatusChanged();
private:
QVector<Task*> mTasks;
};
#endif // LIST_H
#ifndef LIST_H
#define LIST_H
#include <QMainWindow>
#include "task.h"
#include <QVector>
class QLabel;
class QPushButton;
class QHBoxLayout;
class QVBoxLayout;
class List : public QMainWindow
{
Q_OBJECT
public:
explicit List(QWidget *parent = nullptr);
void updateStatus();
~List();
public slots:
void addTask();
void removeTask(Task*);
void taskStatusChanged();
private:
QVector<Task*> mTasks;
QLabel* statusLabel;
QPushButton* addTaskButtun;
QHBoxLayout* hBox;
QVBoxLayout* vBox;
};
#endif // LIST_H
To copy to clipboard, switch view to plain text mode
List.cpp:
#include "list.h"
#include <QPushButton>
#include <QLabel>
#include <QHBoxLayout>
#include <QVBoxLayout>
#include <QString>
#include <QInputDialog>
#include <QLineEdit>
{
statusLabel
= new QLabel(tr
("Status: 0 todo / 0 done"));
hBox->addWidget(statusLabel);
hBox->addStretch();
hBox->addWidget(addTaskButtun);
vBox->addLayout(hBox);
vBox->addStretch();
widget->setLayout(vBox);
setCentralWidget(widget);
connect(addTaskButtun, &QPushButton::clicked, this, &List::addTask);
updateStatus();
}
//************************************************
void List::addTask() {
bool ok;
QLineEdit::Normal, tr
("Untitled task"),
&ok
);
if(ok && !name.isEmpty()) {
Task* task = new Task(name);
connect(task, &Task::removed, this, &List::removeTask);
connect(task, &Task::statusChanged, this, &List::taskStatusChanged);
mTasks.append(task);
vBox->addWidget(task);
updateStatus();
}
}
//*********************************************
void List::removeTask(Task* task) {
mTasks.removeOne(task);
vBox->removeWidget(task);
delete task;
updateStatus();
}
//**************************************
void List::taskStatusChanged() {
updateStatus();
}
//************************************
void List::updateStatus() {
int completedCount = 0;
for(auto t: mTasks)
if(t->isCompleted())
completedCount++;
int todoCount = mTasks.size() - completedCount;
statusLabel
->setText
(QString("Statuse: %1 todo / %2 completed") .arg(todoCount) .arg(completedCount));
}
//*****************************
List::~List()
{
delete vBox;
}
#include "list.h"
#include <QPushButton>
#include <QLabel>
#include <QHBoxLayout>
#include <QVBoxLayout>
#include <QString>
#include <QInputDialog>
#include <QLineEdit>
List::List(QWidget *parent)
: QMainWindow(parent), mTasks()
{
statusLabel = new QLabel(tr("Status: 0 todo / 0 done"));
addTaskButtun = new QPushButton(tr("Add task"));
hBox = new QHBoxLayout;
hBox->addWidget(statusLabel);
hBox->addStretch();
hBox->addWidget(addTaskButtun);
vBox = new QVBoxLayout;
vBox->addLayout(hBox);
vBox->addStretch();
QWidget *widget = new QWidget;
widget->setLayout(vBox);
setCentralWidget(widget);
connect(addTaskButtun, &QPushButton::clicked, this, &List::addTask);
updateStatus();
}
//************************************************
void List::addTask() {
bool ok;
QString name = QInputDialog::getText(this, tr("Add Task"), tr("Task name"),
QLineEdit::Normal, tr("Untitled task"), &ok);
if(ok && !name.isEmpty()) {
Task* task = new Task(name);
connect(task, &Task::removed, this, &List::removeTask);
connect(task, &Task::statusChanged, this, &List::taskStatusChanged);
mTasks.append(task);
vBox->addWidget(task);
updateStatus();
}
}
//*********************************************
void List::removeTask(Task* task) {
mTasks.removeOne(task);
vBox->removeWidget(task);
delete task;
updateStatus();
}
//**************************************
void List::taskStatusChanged() {
updateStatus();
}
//************************************
void List::updateStatus() {
int completedCount = 0;
for(auto t: mTasks)
if(t->isCompleted())
completedCount++;
int todoCount = mTasks.size() - completedCount;
statusLabel->setText(QString("Statuse: %1 todo / %2 completed")
.arg(todoCount) .arg(completedCount));
}
//*****************************
List::~List()
{
delete vBox;
}
To copy to clipboard, switch view to plain text mode
Task.h:
#ifndef TASK_H
#define TASK_H
#include <QWidget>
#include <QString>
{
Q_OBJECT
public:
explicit Task
(const QString
&,
QWidget* parent
= nullptr
);
void setName(const QString&);
bool isCompleted() const;
~Task();
public slots:
void rename();
signals:
void removed(Task*);
void statusChanged(Task*);
private slots:
void checked(bool);
private:
};
#endif // TASK_H
#ifndef TASK_H
#define TASK_H
#include <QWidget>
#include <QString>
class QCheckBox;
class QPushButton;
class QHBoxLayout;
class Task : public QWidget
{
Q_OBJECT
public:
explicit Task(const QString&, QWidget* parent = nullptr);
void setName(const QString&);
QString name() const;
bool isCompleted() const;
~Task();
public slots:
void rename();
signals:
void removed(Task*);
void statusChanged(Task*);
private slots:
void checked(bool);
private:
QCheckBox* checkbox;
QPushButton* editButton;
QPushButton* removeButton;
QHBoxLayout* hBox;
};
#endif // TASK_H
To copy to clipboard, switch view to plain text mode
Task.cpp:
#include "task.h"
#include "list.h"
#include <QInputDialog>
#include <QWidget>
#include <QHBoxLayout>
#include <QPushButton>
#include <QCheckBox>
{
hBox->addWidget(checkbox);
hBox->addStretch();
hBox->addWidget(editButton);
hBox->addWidget(removeButton);
setLayout(hBox);
setName(name);
connect(editButton, &QPushButton::clicked, this, &Task::rename);
connect(removeButton, &QPushButton::clicked, [this]()->void {
emit removed(this);
});
connect(checkbox, &QCheckBox::toggled, this, &Task::checked);
};
//*************************************
void Task::setName(const QString& name) {
checkbox->setText(name);
}
//********************************************
return checkbox->text();
}
//*****************************
bool Task::isCompleted() const {
return checkbox->isChecked();
}
//****************************************
void Task::rename() {
bool ok;
if(ok && !value.isEmpty())
setName(value);
}
//************************************
void Task::checked(bool checked) {
QFont font
(checkbox
->font
());
font.setStrikeOut(checked);
checkbox->setFont(font);
emit statusChanged(this);
}
//**************************************************
Task::~Task() {
delete hBox;
}
#include "task.h"
#include "list.h"
#include <QInputDialog>
#include <QWidget>
#include <QHBoxLayout>
#include <QPushButton>
#include <QCheckBox>
Task::Task(const QString& name, QWidget* parent) : QWidget (parent)
{
checkbox = new QCheckBox;
editButton = new QPushButton(tr("Edit"));
removeButton = new QPushButton(tr("Remove"));
hBox = new QHBoxLayout;
hBox->addWidget(checkbox);
hBox->addStretch();
hBox->addWidget(editButton);
hBox->addWidget(removeButton);
setLayout(hBox);
setName(name);
connect(editButton, &QPushButton::clicked, this, &Task::rename);
connect(removeButton, &QPushButton::clicked, [this]()->void {
emit removed(this);
});
connect(checkbox, &QCheckBox::toggled, this, &Task::checked);
};
//*************************************
void Task::setName(const QString& name) {
checkbox->setText(name);
}
//********************************************
QString Task::name() const {
return checkbox->text();
}
//*****************************
bool Task::isCompleted() const {
return checkbox->isChecked();
}
//****************************************
void Task::rename() {
bool ok;
QString value = QInputDialog::getText(this, tr("Edit task"), tr("Task name"),
QLineEdit::Normal, this->name(), &ok);
if(ok && !value.isEmpty())
setName(value);
}
//************************************
void Task::checked(bool checked) {
QFont font(checkbox->font());
font.setStrikeOut(checked);
checkbox->setFont(font);
emit statusChanged(this);
}
//**************************************************
Task::~Task() {
delete hBox;
}
To copy to clipboard, switch view to plain text mode
When I add tasks "untitled task 1" to "untitled task 3", image number 1 below:
3.jpg
then remove "untitled task 2", the frame/window doesn't shrink and the space for that task is just vacant, image number 2. I like it to decrease and fit the remained tasks, like image number 3. When a task is added, the window increases in size, so when one is removed, it should decrease in size too.
I tested the followings one by one in void List::updateStatus() {, but none worked as expected!
What should be used instead, please?
this->sizeHint();
this->update();
this->resize(sizeHint());
this->adjustSize();
this->sizeHint();
this->update();
this->resize(sizeHint());
this->adjustSize();
To copy to clipboard, switch view to plain text mode
Bookmarks