qt 自定义工具栏

发布时间 2023-06-07 10:18:27作者: 阳光下的小土豆

自定义工具栏

这个类中杂质较多,主要是是单击标签改变经纬度显示类型。

调用方法,在析构函数中删除

if(_statusBar)
delete _statusBar;

void MainWindow::createStatusBar()
{
    if (!_statusBar)
        _statusBar = new TDStatusBar(this);
    _statusBar->setFixedHeight(STATUSBAR_HEIGHT);
    this->setStatusBar(_statusBar);
}

 

.h

#ifndef TDSTATUSBAR_H
#define TDSTATUSBAR_H

#include <QStatusBar>
class QFrame;
class QLabel;

class TDStatusBar : public QStatusBar
{
    Q_OBJECT
public:
    explicit TDStatusBar(QWidget *parent = 0);

public slots:
    void slot_updateMouseCoords(double x, double y, double z);
    void slot_updateViewpointRange(double range);
    void slot_updateCurStatus(const QString &text);
    
    void slotLogMessageNotify(const QString &msg);    

protected:
    virtual bool eventFilter(QObject *watched, QEvent *event);

private:
    void updateCoord();

private:
    QLabel*        _labelCurOperator;
    QLabel*        _labelLon;
    QLabel*        _labelLat;
    QLabel*        _labelAlt;
    QLabel*        _labelViewpoint;
    bool        _angelFormat;

    double        _dlon;
    double        _dLat;
};

#endif // TDSTATUSBAR_H

 

.cpp

  

#include "statusbar.h"
#include "tdhandler/tdhandler.h"
#include <QStatusBar>
#include <QHBoxLayout>
#include <QFrame>
#include <QLabel>
#include <QTimer>
#include <QMouseEvent>

#include "tdscene/tdscene.h"
#include "tdmap/tdscenewidget.h"

#include "config.h"
#include "AppHelper.h"
#include "AppConfig.h"


TDStatusBar::TDStatusBar(QWidget *parent) 
    : QStatusBar(parent)
    , _angelFormat(true)
    , _dlon(0.0)
    , _dLat(0.0)
{
    this->setObjectName("TDStatusBar");
    
    _labelCurOperator = new QLabel(this);
    _labelCurOperator->setObjectName("labelCurOperator");
    _labelCurOperator->setFrameStyle(QFrame::NoFrame);
    _labelCurOperator->setIndent(3);
    this->addWidget(_labelCurOperator);

    _labelLon = new QLabel(this);
    _labelLon->setObjectName("labelLon");
    _labelLon->setFrameStyle(QFrame::NoFrame);
    _labelLon->setIndent(3);
    this->addPermanentWidget(_labelLon);

    _labelLat = new QLabel(this);
    _labelLat->setObjectName("labelLat");
    _labelLat->setFrameStyle(QFrame::NoFrame);
    _labelLat->setIndent(3);
    this->addPermanentWidget(_labelLat);

    _labelAlt = new QLabel(this);
    _labelAlt->setObjectName("labelAlt");
    _labelAlt->setFrameStyle(QFrame::NoFrame);
    _labelAlt->setIndent(3);
    this->addPermanentWidget(_labelAlt);

    _labelViewpoint = new QLabel(this);
    _labelViewpoint->setObjectName("labelViewpoint");
    _labelViewpoint->setFrameStyle(QFrame::NoFrame);
    _labelViewpoint->setIndent(3);
    this->addPermanentWidget(_labelViewpoint);

    connect(HandlerManage::getInstall(), SIGNAL(mouseCoordsSignal(double, double, double)),
        this, SLOT(slot_updateMouseCoords(double, double, double)));
    connect(HandlerManage::getInstall(), SIGNAL(viewpointRangeSignal(double)),
        this, SLOT(slot_updateViewpointRange(double)));

    _labelLon->installEventFilter(this);
    _labelLat->installEventFilter(this);

#if MGIAP_feature_gzearth == 1
    connect(gz::GZEarth::instance(), SIGNAL(sigLogMessageNotify(const QString &)),
        this, SLOT(slotLogMessageNotify(const QString &)));
#endif

    _angelFormat = AppConfig::instance().GetPositionShowType();
}

void TDStatusBar::slot_updateMouseCoords(double x, double y, double z)
{
    _dlon = x;
    _dLat = y;
    updateCoord();
    _labelAlt->setText(QString(tr("Alt: %1 m")).arg(z, 0, 'f', 3));
}

void TDStatusBar::slot_updateViewpointRange(double range)
{
    AppHelper::instance().SetVpRange(range);
    QString msg = QString(tr("Range: %1 m")).arg(range, 0, 'f', 3);
    _labelViewpoint->setText(msg);
}

void TDStatusBar::slot_updateCurStatus(const QString & text)
{
    _labelCurOperator->setText(text);
}

void TDStatusBar::slotLogMessageNotify(const QString &msg) {
    _labelCurOperator->setText(msg);
}

bool TDStatusBar::eventFilter(QObject * watched, QEvent * event)
{
    if (watched == _labelLon || watched == _labelLat)
    {
        if (event->type() == QEvent::MouseButtonPress) 
        {
            QMouseEvent* mouseEvent = static_cast<QMouseEvent*>(event);
            _angelFormat = !_angelFormat;
            AppConfig::instance().SetPositionShowType(_angelFormat);
            updateCoord();
            return true;
        }
        else {
            return false;
        }
    }
    else {
        // pass the event on to the parent class
        return QStatusBar::eventFilter(watched, event);
    }
}

void TDStatusBar::updateCoord()
{
    if (_angelFormat)
    {
        _labelLon->setText(QString(tr("Lon: %1%2")).arg(_dlon, 0, 'f', 6).arg('\xb0'));
        _labelLat->setText(QString(tr("Lat: %1%2")).arg(_dLat, 0, 'f', 6).arg('\xb0'));
    }
    else
    {
        double lon = _dlon;
        if (lon < 0.0)
            lon = -_dlon;
        int degree = (int)lon;
        double min = (lon - degree) * 60.0;
        double sec = (min - (int)min) * 60.0;

        if (_dlon < 0.0)
            degree = -degree;
        QString textLon = QString(tr("Lon: %1%4 %2' %3\"")).arg(degree).arg(min, 2, 'f', 0, QLatin1Char('0')).arg(sec, 0, 'f', 2).arg('\xb0');
        _labelLon->setText(textLon);

        double lat = _dLat;
        if (_dLat < 0.0)
            lat = -_dLat;

        degree = (int)lat;
        min = (lat - degree) * 60.0;
        sec = (min - (int)min) * 60.0;
        if (_dLat < 0.0)
            degree = -degree;
        QString textLat = QString(tr("Lat: %1%4 %2' %3\"")).arg(degree).arg(min, 2, 'f', 0, QLatin1Char('0')).arg(sec, 0, 'f', 2).arg('\xb0');
        _labelLat->setText(textLat);
    }
}