ToDoList project (Gui Programming)
1. ToDoList.pro
QT += core gui
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
CONFIG += c++11
# You can make your code fail to compile if it uses deprecated APIs.
# In order to do so, uncomment the following line.
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000
# disables all the APIs deprecated before Qt 6.0.0
SOURCES += \
main.cpp \
todolist.cpp
HEADERS += \
todolist.h
# Default rules for deployment.
qnx: target.path = /tmp/$${TARGET}/bin
else: unix:!android: target.path = /opt/$${TARGET}/bin
!isEmpty(target.path): INSTALLS += target
RESOURCES += \
images.qrc
2. Header - todolist.h
#ifndef CTODOLIST_H
#define CTODOLIST_H
#include <QMainWindow>
#include <QListView>
#include <QStringListModel>
#include <QPushButton>
class CToDoList : public QMainWindow
{
Q_OBJECT
public:
CToDoList(QWidget *parent = 0);
private slots:
void onAddPressed();
void onRemovePressed();
void onDreamButtonPressed();
private:
QListView * leftList = 0;
QListView * rightList = 0;
QAction * add = 0;
QAction * remove = 0;
QPushButton * dreamButton =0;
};
#endif // CTODOLIST_H
3. Sources - main.cpp
#include "todolist.h"
#include <QApplication>
#include<QSplashScreen>
#include<QTimer>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
CToDoList w;
w.show();
w.setWindowTitle("My To Do List");
QSplashScreen * splash = new QSplashScreen;
splash ->setPixmap(QPixmap(":/icon/listpic.jpg"));
splash ->show();
QTimer::singleShot(2000, splash, SLOT(close()));
QTimer::singleShot(2000, &w, SLOT(show()));
return a.exec();
}
4. Sources - todolist.cpp
#include "todolist.h"
#include <QLabel>
#include <QToolBar>
#include <QBoxLayout>
#include <QPushButton>
#include <QStatusBar>
CToDoList::CToDoList(QWidget *parent)
: QMainWindow(parent)
{
move(411, 200);
QWidget * allWindow = new QWidget(this);
allWindow->setStyleSheet("background-color: AntiqueWhite");
setCentralWidget(allWindow);
QLabel * lblTitle = new QLabel("To Do List", this);
lblTitle->setStyleSheet("font-size: 37px;");
lblTitle->setAlignment(Qt::AlignCenter);
QLabel * lPending = new QLabel("pending", this);
lPending->setStyleSheet("font-size: 20px; font: bold;");
QLabel * lCompleted = new QLabel("Completed", this);
lCompleted->setStyleSheet("font-size: 20px; font: bold;");
//**********************************Button*****************************//
dreamButton = new QPushButton("Motivation button");
connect(dreamButton, SIGNAL(clicked()),
this, SLOT(onDreamButtonPressed()));
//**********************************QListView**************************//
leftList = new QListView(this);
leftList-> setDragEnabled(true); //take a line
leftList-> setAcceptDrops(true); // line from right to left
leftList->setDefaultDropAction(Qt::MoveAction);
rightList = new QListView(this);
rightList-> setDragEnabled(true);
rightList-> setAcceptDrops(true);
rightList->setDefaultDropAction(Qt::MoveAction);
//************************(leftList,rightList) ListViews styles********//
leftList->setStyleSheet(
"QListView {font-size: 25px; font: italic;}"
"QListView::item {background-color: tomato;"
"border: 1px solid black;}"
"QListView::item::hover {background-color: grey}"
);
rightList->setStyleSheet(
"QListView {font-size: 25px; font: italic;}"
"QListView::item {background-color: #2ECC71;"
"border: 1px solid black;}"
"QListView::item::hover {background-color: grey}"
);
leftList->setModel(new QStringListModel());
rightList->setModel(new QStringListModel());
//**********************************Layouts****************************//
QVBoxLayout * lyt = new QVBoxLayout();
QHBoxLayout * hlyt = new QHBoxLayout();
QHBoxLayout * listLayout = new QHBoxLayout();
lyt->addWidget(lblTitle);
hlyt->addWidget(lPending);
hlyt->addWidget(lCompleted);
lyt->addWidget(dreamButton);
listLayout->addWidget(leftList);
listLayout->addWidget(rightList);
allWindow->setLayout(lyt);
lyt->addLayout(hlyt);
lyt->addLayout(listLayout);
//**********************************QAction (icons)********************//
QToolBar * pToolBar = new QToolBar(this);
addToolBar(pToolBar);
add = new QAction(this);
remove = new QAction(this);
add->setIcon(QIcon(":/icon/add.png"));
remove->setIcon(QIcon(":/icon/delete.png"));
connect(add, SIGNAL(triggered()), this, SLOT(onAddPressed()));
connect(remove, &QAction::triggered,
this, &CToDoList::onRemovePressed); //right
pToolBar->addAction(add);
pToolBar->addAction(remove);
}
//**********************************Functions**************************//
void CToDoList::onAddPressed()
{
leftList->model()->insertRow(leftList->model()->rowCount());
QModelIndex Getindex = leftList->model()->index(leftList->
model()->rowCount() -1,0);
leftList->edit(Getindex);
}
void CToDoList::onRemovePressed()
{
QModelIndex yntacikTox = leftList->currentIndex();
leftList->model()->removeRow(yntacikTox.row());
}
void CToDoList::onDreamButtonPressed()
{
statusBar()->showMessage(" Believe in yourself!", 2000);
}
5. Resources - images.qrc - /icon
Result: