-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathlistview.cpp
100 lines (84 loc) · 3.12 KB
/
listview.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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
/*
* Copyright (C) 2017 ~ 2017 Deepin Technology Co., Ltd.
*
* Author: rekols <[email protected]>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "listview.h"
#include <QHeaderView>
#include <QStandardItem>
#include <QAction>
#include <QDebug>
ListView::ListView(QWidget *parent)
: QTableView(parent),
m_itemModel(new QStandardItemModel)
{
init();
}
ListView::~ListView()
{
}
void ListView::init()
{
QStringList titles;
titles << "" << "音乐标题" << "歌手" << "时长";
m_itemModel->setHorizontalHeaderLabels(titles);
setModel(m_itemModel);
setWordWrap(true);
setShowGrid(false);
setAlternatingRowColors(true);
setEditTriggers(QAbstractItemView::NoEditTriggers);
setSelectionMode(QAbstractItemView::SingleSelection);
setSelectionBehavior(QAbstractItemView::SelectRows);
setHorizontalScrollMode(QAbstractItemView::ScrollPerPixel);
setContextMenuPolicy(Qt::CustomContextMenu);
QHeaderView *hHeader = horizontalHeader();
hHeader->setSectionResizeMode(0, QHeaderView::Fixed);
hHeader->setSectionResizeMode(1, QHeaderView::Stretch);
hHeader->setSectionResizeMode(2, QHeaderView::Stretch);
hHeader->setSectionResizeMode(3, QHeaderView::Fixed);
hHeader->setHighlightSections(false);
setColumnWidth(0, 50);
setColumnWidth(1, 300);
setColumnWidth(2, 100);
setColumnWidth(3, 100);
connect(this, &QTableView::customContextMenuRequested, this,
[=] (QPoint point) {
QModelIndex index = indexAt(point);
QMenu menu(this);
QAction *playAction = menu.addAction("播放");
QAction *downAction = menu.addAction("下载");
QAction *a = menu.exec(QCursor::pos());
if (a == downAction) {
emit downloadActionPress(index.row());
} else if (a == playAction) {
emit playActionPress(index.row());
}
});
}
void ListView::appendItem(MusicData *data)
{
int row = m_itemModel->rowCount();
m_itemModel->insertRow(row);
setRowHeight(row, 45);
QStandardItem *rowItem = new QStandardItem(QString::number(row));
m_itemModel->setItem(row, 0, rowItem);
QStandardItem *nameItem = new QStandardItem(data->songName);
m_itemModel->setItem(row, 1, nameItem);
QStandardItem *singerItem = new QStandardItem(data->singerName);
m_itemModel->setItem(row, 2, singerItem);
QStandardItem *timeItem = new QStandardItem(data->timeLength);
m_itemModel->setItem(row, 3, timeItem);
}