qt酷炫导航栏使用

来源:微信公众号「编程学习基地」

项目地址

https://github.com/freeghter/QtDemo

效果演示

请添加图片描述

使用方法

slidenavigation.cppslidenavigation.h 添加到项目里面

在UI设计师界面拖动一个Widget控件

右键选择 提升为...,填写提升的类名称为 SlideNavigation,如下所示

请添加图片描述

右键 提升为 选择刚刚添加的类 SlideNavigation

控件使用

控件属性
//#define YELLOW    //黄色
//#define RED       //红色
//#define PURPLE    //紫色
#define GREEN       //绿色

修改属性

	ui->widget->addItem("星期一");
    ui->widget->addItem("星期二");
    ui->widget->addItem("星期三");
    ui->widget->addItem("星期四");
    ui->widget->addItem("星期五");
    ui->widget->addItem("星期六");
    ui->widget->addItem("星期日");
    ui->widget->addItem("星期八");

    ui->widget->setBarRadious(5);   //设置Widget的圆角
    ui->widget->setItemRadious(5);  //设置Item的圆角
    ui->widget->setFixed(true);		//设置固定

#if defined YELLOW  //黄色
    ui->widget->setItemStartColor(QColor(254, 176, 42));
    ui->widget->setItemEndColor((QColor(225, 156, 37)));
#elif defined RED   //红色
    ui->widget->setItemStartColor(QColor(255, 0, 0));
    ui->widget->setItemEndColor((QColor(225, 20, 10)));
#elif defined PURPLE    //紫色
    ui->widget->setItemStartColor(QColor(191, 65, 249));
    ui->widget->setItemEndColor((QColor(187, 83, 217)));
#elif defined GREEN //绿色
    ui->widget->setItemStartColor(QColor(62, 139, 6));
    ui->widget->setItemEndColor((QColor(40, 139, 28)));
#else
    //默认灰底蓝色
#endif
控件信号

点击的控件下标

signals:
    void itemClicked(int index, QString str);

关联信号与槽

connect(ui->widget,SIGNAL(itemClicked(int, QString)),this,SLOT(_SlotCheckedMenu(int, QString)));

槽函数打印

void Widget::_SlotCheckedMenu(int index, QString name)
{
    qDebug()<<"index:"<<index<<" ,name"<<name;
}

主要代码已经扣过来了,可以直接复制代码到项目中去。

slidenavigation.h

#ifndef SLIDENAVATION_H
#define SLIDENAVATION_H

#include <QWidget>
#include <QMap>

class SlideNavigation : public QWidget
{
    Q_OBJECT

public:
    enum ItemLineStyle
    {
        None,//不显示
        ItemTop,//上方
        ItemRight,//右方
        ItemBottom,//下方
        ItemLeft,//左方
        ItemRect,//矩形
    };

    SlideNavigation(QWidget *parent = 0);
    ~SlideNavigation();

    void addItem(QString str);
    void setBarStartColor(QColor color);
    void setBarEndColor(QColor color);
    void setItemStartColor(QColor color);
    void setItemEndColor(QColor color);
    void setItemTextColor(QColor color);
    void setItemLineColor(QColor color);
    void setBarRadious(int radious);
    void setItemRadious(int radious);
    void setSpace(int space);
    void setItemLineWidth(int width);
    void setItemLineStyle(ItemLineStyle style);
    void setOrientation(Qt::Orientation orientation);
    void setFixed(bool fixed);

signals:
    void itemClicked(int index, QString str);

public slots:
    void setEnableKeyMove(bool enable);
    void moveToFirst();
    void moveToLast();
    void moveToPrevious();
    void moveToNext();
    void moveTo(int index);
    void moveTo(QString str);
    void moveTo(QPointF point);


protected:
    void paintEvent(QPaintEvent *);
    void resizeEvent(QResizeEvent *);
    void mousePressEvent(QMouseEvent *event);
    void keyPressEvent(QKeyEvent *event);
    void drawBarBackground(QPainter* p);//背景
    void drawItemBackground(QPainter* p);//当前选中Item背景
    void drawItemLine(QPainter* p);//ItemLine
    void drawText(QPainter* p);//all item text

private:
    void adjuseItemSize();//调整Item大小

private slots:
    void doSlide();//滑动
    void doShake();//晃动

private:
    QColor m_barStartColor;//背景色
    QColor m_barEndColor;
    QColor m_itemStartColor;//Item颜色
    QColor m_itemEndColor;
    QColor m_itemTextColor;//Item文字颜色
    QColor m_itemLineColor;//Item线条指示器颜色
    int m_barRadious;//边框圆角
    int m_itemRadious;//Item圆角
    int m_space;//Item间隔
    int m_itemLineWidth;//Item线条宽度
    ItemLineStyle m_itemLineStyle;//Item线条类型
    Qt::Orientation m_orientation;//导航方向
    bool m_enableKeyMove;//按键移动
    bool m_fixed;//固定大小

    QMap<int, QPair<QString, QRectF>> m_itemList;
    int m_totalTextWidth;//文字总长度,resize时重新计算每个Item的RectF
    int m_totalTextHeight;
    int m_currentItemIndex;//当前选中item
    QRectF m_startRect;//移动开始
    QRectF m_stopRect;//移动结束
    QTimer* m_slideTimer;//移动定时器
    QTimer* m_shakeTimer;//晃动定时器
    bool m_forward;//前进

};

#endif // SLIDENAVATION_H

slidenavigation.cpp

#include "slidenavigation.h"
#include <QPainter>
#include <QMouseEvent>
#include <QTimer>
#include <QtMath>
#include <QDebug>

SlideNavigation::SlideNavigation(QWidget *parent)
    : QWidget(parent)
{
    m_barStartColor = QColor(121,121,121);
    m_barEndColor = QColor(78,78,78);
    m_itemStartColor = QColor(46,132,243);
    m_itemEndColor = QColor(39,110,203);
    m_itemTextColor = Qt::white;
    m_itemLineColor = QColor(255,107,107);
    m_barRadious = 0;
    m_itemRadious = 0;
    m_space = 25;
    m_itemLineWidth = 3;
    m_itemLineStyle = ItemLineStyle::None;
    m_orientation = Qt::Horizontal;
    m_enableKeyMove = false;
    m_totalTextWidth = 0;
    m_totalTextHeight = 0;
    m_currentItemIndex = -1;
    m_fixed = false;

    setAttribute(Qt::WA_TranslucentBackground);
    m_slideTimer = new QTimer(this);
    m_slideTimer->setInterval(10);
    connect(m_slideTimer, SIGNAL(timeout()), this, SLOT(doSlide()));
    m_shakeTimer = new QTimer(this);
    m_shakeTimer->setInterval(10);
    connect(m_shakeTimer, SIGNAL(timeout()), this, SLOT(doShake()));
    setFocusPolicy(Qt::ClickFocus);
}

SlideNavigation::~SlideNavigation()
{

}

void SlideNavigation::addItem(QString str)
{
    if(str.isEmpty())
        return;
    QMap<int, QPair<QString,QRectF> >::iterator it = m_itemList.begin();
    while(it != m_itemList.end())
    {
        QPair<QString, QRectF>& itemData = it.value();
        if(str == itemData.first)
            return;
        ++it;
    }
    QFont f = font();
    QFontMetrics fm(f);
    int textWidth = fm.width(str);
    int textHeight = fm.height();
    int itemCount = m_itemList.size();
    if(itemCount > 0)
    {
        QPointF topLeft, bottomRight;
        if(m_orientation == Qt::Horizontal)
        {
            topLeft = QPointF(m_totalTextWidth,0);
            m_totalTextWidth += textWidth+m_space;
            bottomRight = QPointF(m_totalTextWidth, m_totalTextHeight);
        }
        else
        {
            topLeft = QPointF(0, m_totalTextHeight);
            m_totalTextHeight += textHeight+m_space;
            bottomRight = QPointF(m_totalTextWidth, m_totalTextHeight);
        }
        QRectF textRect(topLeft, bottomRight);
        m_itemList.insert(itemCount, qMakePair(str, textRect));
    }
    else
    {
        if(m_orientation == Qt::Horizontal)
        {
            m_totalTextWidth = textWidth+m_space;
            m_totalTextHeight = textHeight+m_space;//水平方向,水平占1个m_space,竖直占1个m_space
        }
        else
        {
            m_totalTextWidth = textWidth+2*m_space;//竖直方向,水平占2个m_space,竖直占1个m_space
            m_totalTextHeight = textHeight+m_space;
        }
        QPointF topLeft(0.0, 0.0);
        QPointF bottomRight(m_totalTextWidth, m_totalTextHeight);
        QRectF textRect(topLeft, bottomRight);
        m_itemList.insert(itemCount, qMakePair(str, textRect));
    }
    setMinimumSize(m_totalTextWidth, m_totalTextHeight);
    if(m_fixed)
    {
        if(m_orientation == Qt::Horizontal)
            setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Fixed);//固定高度
        else
            setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Preferred);//固定宽度
    }
    update();
}

void SlideNavigation::setBarStartColor(QColor color)
{
    if(color != m_barStartColor)
    {
        m_barStartColor = color;
        update();
    }
}

void SlideNavigation::setBarEndColor(QColor color)
{
    if(color != m_barEndColor)
    {
        m_barEndColor = color;
        update();
    }
}

void SlideNavigation::setItemStartColor(QColor color)
{
    if(color != m_itemStartColor)
    {
        m_itemStartColor = color;
        update();
    }
}

void SlideNavigation::setItemEndColor(QColor color)
{
    if(color != m_itemEndColor)
    {
        m_itemEndColor = color;
        update();
    }
}

void SlideNavigation::setItemTextColor(QColor color)
{
    if(color != m_itemTextColor)
    {
        m_itemTextColor = color;
        update();
    }
}

void SlideNavigation::setItemLineColor(QColor color)
{
    if(color != m_itemLineColor)
    {
        m_itemLineColor = color;
        update();
    }
}

void SlideNavigation::setBarRadious(int radious)
{
    if(radious>=0 && radious != m_barRadious)
    {
        m_barRadious = radious;
        update();
    }
}

void SlideNavigation::setItemRadious(int radious)
{
    if(radious>=0 && radious != m_itemRadious)
    {
        m_itemRadious = radious;
        update();
    }
}

void SlideNavigation::setSpace(int space)
{
    if(space>=0 && space != m_space)
    {
        m_space = space;
        update();
    }
}

void SlideNavigation::setItemLineWidth(int width)
{
    if(width>=0 && width != m_itemLineWidth)
    {
        m_itemLineWidth = width;
        update();
    }
}

void SlideNavigation::setItemLineStyle(SlideNavigation::ItemLineStyle style)
{
    if(style != m_itemLineStyle)
    {
        m_itemLineStyle = style;
        update();
    }
}

void SlideNavigation::setOrientation(Qt::Orientation orientation)
{
    if(orientation != m_orientation)
    {
        m_orientation = orientation;
        update();
    }
}

void SlideNavigation::setFixed(bool fixed)
{
    if(fixed != m_fixed)
    {
        m_fixed = fixed;
        update();
    }
}

void SlideNavigation::setEnableKeyMove(bool enable)
{
    if(enable != m_enableKeyMove)
    {
        m_enableKeyMove = enable;
    }
}

void SlideNavigation::moveToFirst()
{
    moveTo(0);
}

void SlideNavigation::moveToLast()
{
    moveTo(m_itemList.size()-1);
}

void SlideNavigation::moveToPrevious()
{
    moveTo(m_currentItemIndex-1);
}

void SlideNavigation::moveToNext()
{
    moveTo(m_currentItemIndex+1);
}

void SlideNavigation::moveTo(int index)
{
    if(index>=0 && index<m_itemList.size() && index!=m_currentItemIndex)
    {
        emit itemClicked(index, m_itemList[index].first);
        if(index == m_currentItemIndex)
            return;
        if(m_currentItemIndex == -1)
            m_startRect = m_itemList[index].second;
        m_forward = index > m_currentItemIndex;
        m_currentItemIndex = index;
        m_stopRect = m_itemList[index].second;
        m_slideTimer->start();
    }
}

void SlideNavigation::moveTo(QString str)
{
    QMap<int, QPair<QString, QRectF> >::iterator it = m_itemList.begin();
    for(; it!=m_itemList.end(); ++it)
    {
        if(it.value().first == str)
        {
            int targetIndex = it.key();
            if(targetIndex == m_currentItemIndex)
                return;
            moveTo(targetIndex);
            break;
        }
    }
}

void SlideNavigation::moveTo(QPointF point)
{
    QMap<int, QPair<QString, QRectF> >::iterator it = m_itemList.begin();
    for(; it!=m_itemList.end(); ++it)
    {
        if(it.value().second.contains(point))
        {
            int targetIndex = it.key();
            if(targetIndex == m_currentItemIndex)
                return;
            moveTo(targetIndex);
            break;
        }
    }
}

void SlideNavigation::paintEvent(QPaintEvent *)
{
    QPainter painter(this);
    painter.setRenderHints(QPainter::Antialiasing);

    drawBarBackground(&painter);
    drawItemBackground(&painter);
    drawItemLine(&painter);
    drawText(&painter);
}

void SlideNavigation::resizeEvent(QResizeEvent *)
{
    adjuseItemSize();
}

void SlideNavigation::mousePressEvent(QMouseEvent *event)
{
    QMap<int, QPair<QString, QRectF> >::iterator it = m_itemList.begin();
    for(; it!=m_itemList.end(); ++it)
    {
        if(it.value().second.contains(event->pos()))
        {
            int targetIndex = it.key();
            emit itemClicked(targetIndex, it.value().first);
            if(targetIndex == m_currentItemIndex)
                return;
            if(m_currentItemIndex == -1)
            {
                m_startRect = it.value().second;
            }
            m_forward = targetIndex > m_currentItemIndex;
            m_currentItemIndex = targetIndex;
            m_stopRect = it.value().second;
            m_slideTimer->start();
            break;
        }
    }
}

void SlideNavigation::keyPressEvent(QKeyEvent *event)
{
    if(!m_enableKeyMove)
    {
        QWidget::keyPressEvent(event);
        return;
    }
    switch (event->key()) {
    case Qt::Key_Home:
        moveToFirst();
        break;
    case Qt::Key_End:
        moveToLast();
        break;
    case Qt::Key_Up:
    case Qt::Key_Left:
        moveToPrevious();
        break;
    case Qt::Key_Down:
    case Qt::Key_Right:
        moveToNext();
        break;
    default:
        QWidget::keyPressEvent(event);
        break;
    }
}

void SlideNavigation::drawBarBackground(QPainter *p)
{
    p->save();
    p->setPen(Qt::NoPen);
    QLinearGradient linerGradient(QPointF(0,0), QPointF(0,height()));
    linerGradient.setColorAt(0.0, m_barStartColor);
    linerGradient.setColorAt(1.0, m_barEndColor);
    p->setBrush(linerGradient);
    p->drawRoundedRect(rect(), m_barRadious, m_barRadious);
    p->restore();
}

void SlideNavigation::drawItemBackground(QPainter *p)
{
    if(m_startRect.isNull())
        return;
    p->save();
    QLinearGradient linerGradient(m_startRect.topLeft(), m_startRect.bottomRight());
    linerGradient.setColorAt(0.0, m_itemStartColor);
    linerGradient.setColorAt(1.0, m_itemEndColor);
    p->setPen(Qt::NoPen);
    p->setBrush(linerGradient);
    p->drawRoundedRect(m_startRect, m_itemRadious, m_itemRadious);
    p->restore();
}

void SlideNavigation::drawItemLine(QPainter *p)
{
    if(m_startRect.isNull())
        return;
    QPointF p1,p2;
    switch(m_itemLineStyle)
    {
    case None:
        return;
        break;
    case ItemTop:
        p1 = m_startRect.topLeft();
        p2 = m_startRect.topRight();
        break;
    case ItemRight:
        p1 = m_startRect.topRight();
        p2 = m_startRect.bottomRight();
        break;
    case ItemBottom:
        p1 = m_startRect.bottomLeft();
        p2 = m_startRect.bottomRight();
        break;
    case ItemLeft:
        p1 = m_startRect.topLeft();
        p2 = m_startRect.bottomLeft();
        break;
    case ItemRect:
        p1 = m_startRect.topLeft();
        p2 = m_startRect.bottomRight();
        break;
    default:
        return;
        break;
    }
    p->save();
    QPen linePen;
    linePen.setColor(m_itemLineColor);
    linePen.setWidth(m_itemLineWidth);
    p->setPen(linePen);
    if(m_itemLineStyle == ItemRect)
    {
        p->drawRoundedRect(QRectF(p1, p2), m_itemRadious, m_itemRadious);
    }
    else
    {
        p->drawLine(p1, p2);
    }
    p->restore();
}

void SlideNavigation::drawText(QPainter *p)
{
    p->save();
    p->setPen(m_itemTextColor);
    QMap<int, QPair<QString,QRectF> >::iterator it = m_itemList.begin();
    while(it != m_itemList.end())
    {
        QPair<QString, QRectF>& itemData = it.value();
        p->drawText(itemData.second, Qt::AlignCenter, itemData.first);
        ++it;
    }
    p->restore();
}

void SlideNavigation::adjuseItemSize()
{
    if(m_fixed)
    {//针对固定大小的不对Item的位置进行调整
        return;
    }
    qreal addWidth, addHeight;
    if(m_orientation == Qt::Horizontal)
    {
        addWidth = 1.0*(width()-m_totalTextWidth)/m_itemList.size();
        addHeight = 1.0*(height()-m_totalTextHeight);
    }
    else
    {
        addWidth = 1.0*(width()-m_totalTextWidth);
        addHeight = 1.0*(height()-m_totalTextHeight)/m_itemList.size();
    }
    int itemCount = m_itemList.size();
    qreal dx = 0;
    qreal dy = 0;
    QPointF topLeft, bottomRight;
    for(int i=0; i<itemCount; ++i)
    {
        QPair<QString, QRectF>& itemData = m_itemList[i];
        QFont f = font();
        QFontMetrics fm(f);
        int textWidth = fm.width(itemData.first);
        int textHeight = fm.height();
        if(m_orientation == Qt::Horizontal)
        {
            topLeft = QPointF(dx, 0);
            dx += textWidth+m_space+addWidth;
            dy = m_totalTextHeight+addHeight;
        }
        else
        {
            topLeft = QPointF(0, dy);
            dx = m_totalTextWidth+addWidth;
            dy += textHeight+m_space+addHeight;
        }
        bottomRight = QPointF(dx, dy);
        QRectF textRect(topLeft, bottomRight);
        itemData.second = textRect;
        if(i == m_currentItemIndex)
        {
            m_startRect = textRect;
            m_stopRect = textRect;
        }
    }
    update();
}

void SlideNavigation::doSlide()
{
    if(m_space <= 0 || m_startRect == m_stopRect)
        return;
    qreal dx,dy;
    if(m_orientation == Qt::Horizontal)
    {
        dx = m_space/2.0;
        dy = 0;
    }
    else
    {
        dx = 0;
        dy = m_space/2.0;
    }
    if(m_forward)
    {
        m_startRect.adjust(dx, dy, dx, dy);
        if((m_orientation == Qt::Horizontal && m_startRect.topLeft().x() >= m_stopRect.topLeft().x()) ||
                (m_orientation == Qt::Vertical && m_startRect.topLeft().y() >= m_stopRect.topLeft().y()))
        {
            m_slideTimer->stop();
            if(m_startRect != m_stopRect)
                m_shakeTimer->start();
        }
    }
    else
    {
        m_startRect.adjust(-dx, -dy, -dx, -dy);
        if((m_orientation == Qt::Horizontal && m_startRect.topLeft().x() <= m_stopRect.topLeft().x()) ||
                (m_orientation == Qt::Vertical && m_startRect.topLeft().y() <= m_stopRect.topLeft().y()))
        {
            m_slideTimer->stop();
            if(m_startRect != m_stopRect)
                m_shakeTimer->start();
        }
    }
    update();

//    static qreal stepDx = m_space/2.0;//步进平移
//    static qreal stepDy = m_space/2.0;
//    static qreal adjustDx = m_space/2.0;//调整平移
//    static qreal adjustDy = m_space/2.0;
//    static int state = 1; //1普通平移,2偏移,3回弹
//    if(m_orientation == Qt::Horizontal)
//    {
//        stepDy = 0;
//        adjustDy = 0;
//    }
//    else
//    {
//        stepDx = 0;
//        adjustDx = 0;
//    }
//    if(m_forward)
//    {
//        m_startRect.adjust(stepDx, stepDy, stepDx, stepDy);
//        if(state == 1 &&
//                ((m_orientation == Qt::Horizontal && m_startRect.topLeft().x() >= m_stopRect.topLeft().x()) ||
//                (m_orientation == Qt::Vertical && m_startRect.topLeft().y() >= m_stopRect.topLeft().y())) )
//        {//偏移
//            if(m_orientation == Qt::Horizontal)
//            {
//                stepDx = 1;
//            }
//            else
//            {
//                stepDy = 1;
//            }
//            m_startRect = m_stopRect;
//            m_stopRect.adjust(adjustDx, adjustDy, adjustDx, adjustDy);
//            state = 2;
//            qDebug() << "开始右偏移" << m_startRect << m_stopRect;
//        }
//        if(state == 2 &&
//                ((m_orientation == Qt::Horizontal && m_startRect.topLeft().x() >= m_stopRect.topLeft().x()) ||
//                (m_orientation == Qt::Vertical && m_startRect.topLeft().y() >= m_stopRect.topLeft().y())) )
//        {//回弹
//            if(m_orientation == Qt::Horizontal)
//            {
//                stepDx = -1;
//            }
//            else
//            {
//                stepDy = -1;
//            }
//            m_startRect = m_stopRect;
//            m_stopRect.adjust(-adjustDx, adjustDy, -adjustDx, adjustDy);
//            state = 3;
//            qDebug() << "开始右回弹" << m_startRect << m_stopRect;
//        }
//        if(state == 3 &&
//                ((m_orientation == Qt::Horizontal && m_startRect.topLeft().x() <= m_stopRect.topLeft().x()) ||
//                (m_orientation == Qt::Vertical && m_startRect.topLeft().y() <= m_stopRect.topLeft().y())) )
//        {//重置变量
//            stepDx = m_space/2.0;
//            stepDy = m_space/2.0;
//            adjustDx = m_space/2.0;
//            adjustDy = m_space/2.0;
//            state = 1;
//            m_slideTimer->stop();
//            qDebug() << "开始右重置变量";
//        }
//    }
//    else
//    {
//        m_startRect.adjust(-stepDx, stepDy, -stepDx, stepDy);
//        if(state == 1 &&
//                ((m_orientation == Qt::Horizontal && m_startRect.topLeft().x() <= m_stopRect.topLeft().x()) ||
//                (m_orientation == Qt::Vertical && m_startRect.topLeft().y() <= m_stopRect.topLeft().y())) )
//        {//偏移
//            if(m_orientation == Qt::Horizontal)
//            {
//                stepDx = 1;
//            }
//            else
//            {
//                stepDy = 1;
//            }
//            m_startRect = m_stopRect;
//            m_stopRect.adjust(-adjustDx, adjustDy, -adjustDx, adjustDy);
//            state = 2;
//            qDebug() << "开始左偏移" << m_startRect << m_stopRect;
//        }
//        if(state == 2 &&
//                ((m_orientation == Qt::Horizontal && m_startRect.topLeft().x() <= m_stopRect.topLeft().x()) ||
//                (m_orientation == Qt::Vertical && m_startRect.topLeft().y() <= m_stopRect.topLeft().y())) )
//        {//回弹
//            if(m_orientation == Qt::Horizontal)
//            {
//                stepDx = -1;
//            }
//            else
//            {
//                stepDy = -1;
//            }
//            m_startRect = m_stopRect;
//            m_stopRect.adjust(adjustDx, adjustDy, adjustDx, adjustDy);
//            state = 3;
//            qDebug() << "开始左回弹" << m_startRect << m_stopRect;
//        }
//        if(state == 3 &&
//                ((m_orientation == Qt::Horizontal && m_startRect.topLeft().x() >= m_stopRect.topLeft().x()) ||
//                (m_orientation == Qt::Vertical && m_startRect.topLeft().y() >= m_stopRect.topLeft().y())) )
//        {//重置变量
//            stepDx = m_space/2.0;
//            stepDy = m_space/2.0;
//            adjustDx = m_space/2.0;
//            adjustDy = m_space/2.0;
//            state = 1;
//            m_slideTimer->stop();
//            qDebug() << "开始左重置变量";
//        }
//    }
//    update();
}

void SlideNavigation::doShake()
{
    qreal delta = 2.0;
    qreal dx1,dy1,dx2,dy2;
    dx1=dy1=dx2=dy2=0.0;
    if(m_startRect.topLeft().x()>m_stopRect.topLeft().x())
    {
        dx1 = -delta;
    }
    else if(m_startRect.topLeft().x()<m_stopRect.topLeft().x())
    {
        dx1 = delta;
    }
    if(m_startRect.topLeft().y()>m_stopRect.topLeft().y())
    {
        dy1 = -delta;
    }
    else if(m_startRect.topLeft().y()<m_stopRect.topLeft().y())
    {
        dy1 = delta;
    }
    if(m_startRect.bottomRight().x()>m_stopRect.bottomRight().x())
    {
        dx2 = -delta;
    }
    else if(m_startRect.bottomRight().x()<m_stopRect.bottomRight().x())
    {
        dx2 = delta;
    }
    if(m_startRect.bottomRight().y()>m_stopRect.bottomRight().y())
    {
        dy2 = -delta;
    }
    else if(m_startRect.bottomRight().y()<m_stopRect.bottomRight().y())
    {
        dy2 = delta;
    }
    m_startRect.adjust(dx1,dy1,dx2,dy2);
    if(qAbs(m_startRect.topLeft().x()-m_stopRect.topLeft().x()) <= delta)
    {
        m_startRect.setLeft(m_stopRect.topLeft().x());
    }
    if(qAbs(m_startRect.topLeft().y()-m_stopRect.topLeft().y()) <= delta)
    {
        m_startRect.setTop(m_stopRect.topLeft().y());
    }
    if(qAbs(m_startRect.bottomRight().x()-m_stopRect.bottomRight().x()) <= delta)
    {
        m_startRect.setRight(m_stopRect.bottomRight().x());
    }
    if(qAbs(m_startRect.bottomRight().y()-m_stopRect.bottomRight().y()) <= delta)
    {
        m_startRect.setBottom(m_stopRect.bottomRight().y());
    }
    if(m_startRect == m_stopRect)
        m_shakeTimer->stop();
    update();
}
  • 5
    点赞
  • 18
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
### 回答1: Qt Creator导航栏是软件中的一个重要工具栏,它能够帮助用户快速浏览和编辑项目文件。该导航栏中的按钮可以分为两种类型,分别是项目路径按钮和编辑按钮。 项目路径按钮的作用是展示项目目录结构,用户可以通过单击不同的路径按钮来浏览或编辑所选的文件。同时,该按钮也提供了一些快捷键来帮助用户进行常见的文件操作,例如新建文件、复制文件、移动文件以及重命名文件等。 而编辑按钮则提供了一些常用的编辑工具,例如搜索、替换和自动补全等,使用户能够更方便地进行代码编辑。此外,在导航栏的底部,还有一个输出窗口,可以查看软件运行时的报错信息。 总的来说,Qt Creator导航栏为用户提供了一个直观、便捷的方式来管理和编辑代码,让用户能够更加高效地完成自己的项目。 ### 回答2: Qt Creator 是一种集成开发环境(IDE),它为用户提供了一个创建 Qt 应用程序的快速而高效的方式。在 Qt Creator 中,导航栏是一个非常重要的组件,它提供了对编程项目的有效控制。 Qt Creator 的导航栏包括几个子项,如文件、项目、类别、搜索等。在导航栏中,文件子项可以让用户快速浏览并定位他们在项目中的文件。项目子项显示了当前打开的项目的重要关键信息。而在类别子项下,则列出了项目中的所有类别,包括类、资源、UI 窗体等。最后,搜索子项可以让用户快速找到他们在项目中的代码。 作为一款先进的 IDE,Qt Creator 的导航栏为用户提供了出色的导航和管理代码的功能。用户可以使用导航栏快速访问项目、文件和类别,以便快速找到他们正在寻找的内容。此外,搜索子项还支持在项目中快速查找关键字和代码片段,以便更好地了解项目的内部细节。综合来看,导航栏Qt Creator 的一个非常重要的组成部分,它使用户可以更加轻松地管理和控制他们的编程项目。 ### 回答3: Qt Creator 是一个跨平台的集成开发环境(IDE),专为QT程序员打造。在Qt Creator中,导航栏是一个重要的组成部分,在开发期间,它可以帮助我们快速访问常用的功能和资源。 导航栏位于Qt Creator的顶部,其中包含了许多常用的工具和功能,包括项目浏览器、文件浏览器、搜索框和编译器的输出窗口等。 项目浏览器是导航栏的核心,它可以让开发者方便地浏览、编辑和管理项目中的文件和目录。在项目浏览器中,我们可以进行文件的创建、删除、复制、黏贴和重命名等操作;同时,我们也可以通过双击某个文件来快速打开它,以便进行相应的编辑操作。 文件浏览器是导航栏中另一个重要的组成部分,它可以帮助开发者浏览系统中的文件和目录,方便地进行文件的拖拽、复制和粘贴等操作。 搜索框是文件浏览器的重要辅助工具,我们可以在其中输入关键字来搜索系统中的文件和目录。 编译器的输出窗口位于导航栏的最右侧,他可以实时显示编译器的消息、警告和错误信息,为开发者提供及时的反馈和帮助。 以上就是Qt Creator导航栏的基本功能和作用,希望能够帮助大家更好地进行Qt程序开发。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

DeRoy

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值