-
Notifications
You must be signed in to change notification settings - Fork 3
/
mainwindow.cpp
40 lines (33 loc) · 1010 Bytes
/
mainwindow.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
#include "mainwindow.h"
#include "./ui_mainwindow.h"
#include "customwidget.h"
MainWindow::MainWindow(QWidget* parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
{
ui->setupUi(this);
// Creates a list of custom widgets
for (int i = 0; i < 5; ++i) {
auto item = new QListWidgetItem();
auto widget = new CustomWidget(this);
widget->setText(QString("text %1").arg(i));
item->setSizeHint(widget->sizeHint());
ui->listWidget->addItem(item);
ui->listWidget->setItemWidget(item, widget);
}
}
MainWindow::~MainWindow() { }
/*
* Removes the item from the list
*/
void MainWindow::removeItem(const QString& text)
{
for (int i = 0; i < ui->listWidget->count(); ++i) {
auto item = ui->listWidget->item(i);
auto itemWidget = qobject_cast<CustomWidget*>(ui->listWidget->itemWidget(item));
if (itemWidget->getText() == text) {
delete ui->listWidget->takeItem(i);
break;
}
}
}