//响应鼠标右键点击事件 void MusicList::contextMenuEvent(QContextMenuEvent *event) { QMenu menu; menu.addAction(QIcon(":/images/shareMusic.png"), tr("分享"), this, SLOT(slot_ShareCurrentMusic())); menu.addAction(QIcon(":/images/locateButton.png"), tr("定位到当前播放歌曲"), this, SLOT(slot_LocateCurrentMusic())); menu.addSeparator(); menu.addAction(QIcon(":/images/delCurrentMusic.png"), tr("删除当前歌曲"), this, SLOT(slot_DelCurrentMusic())); menu.addAction(QIcon(":/images/clearMusicList.png"), tr("清空列表"), this, SLOT(slot_ClearAllMusicList())); menu.addAction(QIcon(":/images/saveList.png"), tr("保存列表"), this, SLOT(slot_SaveList())); menu.addSeparator(); menu.addAction(QIcon(":/images/quitAction.png"), tr("关闭音乐列表"), parent, SLOT(slot_OpenMusicList())); menu.exec(event->globalPos());//返回鼠标指针的全局位置 event->accept(); }
MusicList::MusicList(Phonon::MediaObject *mediaObject, QList<Phonon::MediaSource> *sources, QWidget *widget) : m_mediaObject(mediaObject), m_sources(sources), parent(widget) { //设置窗口基本属性 this ->resize(380, 360);//设置窗体大小 //this ->setWindowFlags(Qt::FramelessWindowHint);//去掉窗体边框 this ->setWindowFlags(Qt::Tool | Qt::FramelessWindowHint);//去掉窗体边框,不在任务栏显示图标 //this ->setAttribute(Qt::WA_TranslucentBackground);//设置背景透明 this ->setWindowIcon(QIcon(":/images/CZPlayer.png"));//设置logo this ->setWindowTitle(tr("音乐播放列表")); this ->setRowCount(0);//初始化的行数为0 this ->setColumnCount(4);//初始化的列数为3 QStringList headList; headList << tr("序号") << tr("标题") << tr("艺术家") << tr("长度"); this ->setHorizontalHeaderLabels(headList);//设置头信息 this ->horizontalHeader() ->setVisible(false); this ->verticalHeader() ->setVisible(false); //this ->horizontalHeader() ->setResizeMode(QHeaderView::Stretch);//设置内容自动横向填满可用区域 this ->horizontalHeader() ->resizeSection(0, 30); this ->horizontalHeader() ->resizeSection(1, 170); this ->horizontalHeader() ->resizeSection(2, 100); this ->horizontalHeader() ->resizeSection(3, 65); this ->horizontalHeader() ->setHighlightSections(false);//点击表时不对表头行光亮(获取焦点) this ->horizontalHeader() ->setClickable(false);//不响应鼠标单击 this ->setSelectionMode(QAbstractItemView::SingleSelection);//设置只能选中单行 this ->setSelectionBehavior(QAbstractItemView::SelectRows);//设置选择习惯为选择行 this ->setEditTriggers(QAbstractItemView::NoEditTriggers);//设置内容不可编辑 this ->setShowGrid(false);//设置不显示网格 this ->setItemDelegate(new NoFocusDelegate());//去掉选中单元格的虚线 // QTableWidgetItem *columnHeadItem = this ->horizontalHeaderItem(0); // columnHeadItem ->setFont(QFont("幼圆")); // columnHeadItem ->setBackground(Qt::gray); // columnHeadItem ->setTextColor(Qt::white); // QTableWidgetItem *columnHeadItem2 = this ->horizontalHeaderItem(1); // columnHeadItem2 ->setFont(QFont("幼圆")); // columnHeadItem2 ->setBackground(Qt::gray); // columnHeadItem2 ->setTextColor(Qt::white); // QTableWidgetItem *columnHeadItem3 = this ->horizontalHeaderItem(2); // columnHeadItem3 ->setFont(QFont("幼圆")); // columnHeadItem3 ->setBackground(Qt::gray); // columnHeadItem3 ->setTextColor(Qt::white); //设置header样式 this ->horizontalHeader() ->setStyleSheet("QHeaderView::section{background: gray; color:white; font-family:'微软雅黑'}"); this ->setStyleSheet("QTableCornerButton::section{background:gray};");//设置表格直角方格样式 this ->verticalHeader() ->setStyleSheet("QHeaderView::section{background: gray; color:white; font-family:'微软雅黑';}"); clearMusicList = new QAction(this); clearMusicList ->setIcon(QIcon(":/images/clearMusicList.png")); clearMusicList ->setText(tr("清空列表")); closeAction = new QAction(this); closeAction ->setIcon(QIcon(":/images/quitAction")); closeAction ->setText(tr("关闭音乐列表")); delCurrentMusicAction = new QAction(this); delCurrentMusicAction ->setIcon(QIcon(":/images/delCurrentMusic.png")); delCurrentMusicAction ->setText(tr("删除当前歌曲")); saveListAction = new QAction(this); saveListAction ->setIcon(QIcon(":/images/saveList.png")); saveListAction ->setText(tr("保存列表")); connect(clearMusicList, SIGNAL(triggered()), this, SLOT(slot_ClearAllMusicList())); connect(closeAction, SIGNAL(triggered()), parent, SLOT(slot_OpenMusicList())); connect(delCurrentMusicAction, SIGNAL(triggered()), this, SLOT(slot_DelCurrentMusic())); connect(saveListAction, SIGNAL(triggered()), this, SLOT(slot_SaveList())); connect(this, SIGNAL(cellClicked(int,int)), this, SLOT(slot_TableClicked(int))); }