直接上代码
#ifndef QNAVIGATIONWIDGET_H
#define QNAVIGATIONWIDGET_H
#include <QWidget>
#include <QMouseEvent>
class QNavigationWidget : public QWidget
{
Q_OBJECT
public:
QNavigationWidget(QWidget *parent=0);
~QNavigationWidget();
void addItem(const QString &title);
void setWidth(const int &width);
void setBackgroundColor(const QString &color);
void setSelectColor(const QString &color);
void setRowHeight(const int &height);
protected:
void paintEvent(QPaintEvent *);
void mouseMoveEvent(QMouseEvent *);
void mousePressEvent(QMouseEvent *);
void mouseReleaseEvent(QMouseEvent *);
private:
QList<QString> listItems;
QString backgroundColor;
QString selectedColor;
int rowHeight;
int currentIndex;
signals:
void currentItemChanged(const int &index);
};
#endif
#include "qnavigationwidget.h"
#include <QPainter>
#include <QDebug>
QNavigationWidget::QNavigationWidget(QWidget *parent) : QWidget(parent)
{
backgroundColor = "#E4E4E4";
selectedColor = "#2CA7F8";
rowHeight = 40;
currentIndex = 0;
setMouseTracking(true);
setFixedWidth(150);
}
QNavigationWidget::~QNavigationWidget()
{
}
void QNavigationWidget::addItem(const QString &title)
{
listItems << title;
update();
}
void QNavigationWidget::setWidth(const int &width)
{
setFixedWidth(width);
}
void QNavigationWidget::setBackgroundColor(const QString &color)
{
backgroundColor = color;
update();
}
void QNavigationWidget::setSelectColor(const QString &color)
{
selectedColor = color;
update();
}
void QNavigationWidget::setRowHeight(const int &height)
{
rowHeight = height;
update();
}
void QNavigationWidget::paintEvent(QPaintEvent *)
{
QPainter painter(this);
painter.setRenderHint(QPainter::Antialiasing, true);
// Draw background color.
painter.setPen(Qt::NoPen);
painter.setBrush(QColor(backgroundColor));
painter.drawRect(rect());
// Draw Items
int count = 0;
for (const QString &str : listItems) {
QPainterPath itemPath;
itemPath.addRect(QRect(0, count * rowHeight, width(), rowHeight));
if (currentIndex == count) {
painter.setPen("#FFFFFF");
painter.fillPath(itemPath, QColor(selectedColor));
}else {
painter.setPen("#202020");
painter.fillPath(itemPath, QColor(backgroundColor));
}
painter.drawText(QRect(0, count * rowHeight, width(), rowHeight), Qt::AlignVCenter | Qt::AlignHCenter, str);
++count;
}
}
void QNavigationWidget::mouseMoveEvent(QMouseEvent *e)
{
if (e->y() / rowHeight < listItems.count()) {
// qDebug() << e->y() / rowHeight;
}
}
void QNavigationWidget::mousePressEvent(QMouseEvent *e)
{
if (e->y() / rowHeight < listItems.count()) {
currentIndex = e->y() / rowHeight;
emit currentItemChanged(currentIndex);
update();
}
}
void QNavigationWidget::mouseReleaseEvent(QMouseEvent *e)
{
Q_UNUSED(e);
}
测试示例
#include "mainwindow.h"
#include <QDebug>
MainWindow::MainWindow(QMainWindow *parent) : QMainWindow(parent)
{
mainWidget = new QWidget;
rightWidget = new QWidget;
rightLayout = new QVBoxLayout(rightWidget);
mainLayout = new QHBoxLayout(mainWidget);
navigationWidget = new QNavigationWidget;
tipsLabel = new QLabel("Item: 0");
rightWidget->setFixedWidth(600 - navigationWidget->width());
navigationWidget->setRowHeight(50);
navigationWidget->addItem("常规");
navigationWidget->addItem("画面");
navigationWidget->addItem("字幕");
navigationWidget->addItem("下载");
navigationWidget->addItem("其他");
navigationWidget->addItem("关于");
rightLayout->addWidget(tipsLabel, 0, Qt::AlignCenter);
mainLayout->setContentsMargins(0, 0, 0, 0);
mainLayout->addWidget(navigationWidget);
mainLayout->addWidget(rightWidget);
setCentralWidget(mainWidget);
connect(navigationWidget, &QNavigationWidget::currentItemChanged, this, [=](const int ¤t){
tipsLabel->setText("Item: " + QString::number(current));
});
}
MainWindow::~MainWindow()
{
}