首页 > 其他分享 >qt 自定义工具栏

qt 自定义工具栏

时间:2023-06-07 10:22:05浏览次数:38  
标签:TDStatusBar 工具栏 qt 自定义 double void QString arg include

自定义工具栏

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

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

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);
    }
}

 

标签:TDStatusBar,工具栏,qt,自定义,double,void,QString,arg,include
From: https://www.cnblogs.com/warmlight/p/17462598.html

相关文章

  • 如何在Flutter中构建自定义Widget
    Flutter最近越来越流行。您可以使用它来构建可在MacOS、Windows和Linux上顺畅运行的复杂应用程序。但是构建这些应用程序并不总是一个简单的过程。您经常需要重构代码以保持应用程序的性能。一种这样的重构技术是提取重复的代码和组件并在多个地方重用它们。在本教程中,您......
  • 自定义上传图片,动态拼接html元素,node插入/替换指定位置旧元素
    <!DOCTYPEhtml><head>  <metaname="viewport"    content="width=device-width,initial-scale=0.5,maximum-scale=1.0,minimum-scale=0.5,user-scalable=yes">  <title>上传图片</title></head><bo......
  • Docker(二)自定义镜像
    Dockerfile是生成镜像脚本的应答文件,Dockerfile是比commit更强大的镜像编排方式Dockerfile语法格式:FROM:基础镜像MAINTAINER:镜像创建者信息EXPOSE:开放的端口ENV:设置变量ADD:复制文件到镜像RUN:制作镜像时执行的命令,可以有多个WORKDIR:定义容器默认工作目录CMD:容器启......
  • EasyExcel实战 自定义动态化导出excel
    1.Java操作excel表格,除了运用POI技术,阿里开发一个工具简易操作EasyExcel,接下来我们来实战操作下自定义动态化导出excel,自定义动态化为自定义标题,合并单元格引入pom<dependency><groupId>com.alibaba</groupId><artifactId>easyexcel</artifactId>......
  • CKEditor (Toolbar Definition)工具栏自定义配置
    CKEditor中的工具栏默认显示所有功能按钮。出于安全的原因、或者是简化的原因,需要对这个工具栏进行自定义设置。CKEditor工具栏自定义配置非常简单。编辑ckeditor目录下的的config.js文件,默认的是下面的工具栏代码,注意工具按钮是分组的,一个name内的大括号就是一个工具按钮......
  • qt安装
            ......
  • 统信UOS系统开发笔记(三):从Qt源码编译安装之编译安装Qt5.12.8
    前言  上一篇,是使用Qt提供的安装包安装的,有些场景需要使用到自己编译的Qt,所以本篇如何在统信UOS系统上编译Qt5.12.8源码。<br>统信UOS系统版本  系统版本:  Qt源码下载  参考博文《获取下载Qt安装包,Qt源码全国网址备忘录(不用注册Qt账户,即可下载各版本Qt安装包和Qt源......
  • Odoo集成MQTT
    文件结构MQTT客户端importpaho.mqtt.clientasmqttclient_dict={}importlogging_logger=logging.getLogger(__name__)classMQTTClient(object):def__init__(self,client_id,protocol,broker,port,username,password):self.client_id=......
  • 饶派杯 XCTF 车联网挑战赛 mqttsvr 复现
    前言IDA和Ghidra对mips64架构的识别貌似不是很友好,赛场上由于反编译实在难看,所以很难静下心来去逆,于是赛后在期末考试前稍微花点时间做了一下复现。准备checksec一下,发现是mips64大端,没开CanaryRELRO,可以溢出,可以覆写got表,这里开了NX和PIE,NX对mips64这种异架构来说好像并不会......
  • Qt迭代器(Java类型和STL类型)详解
    迭代器为访问容器类里的数据项提供了统一的方法,Qt有两种迭代器类:Java类型的迭代器和STL类型的迭代器。两者比较,Java类型的迭代器更易于使用,且提供一些高级功能,而STL类型的迭代器效率更高。 Java类型迭代器对于每个容器类,有两个Java类型迭代器:一个用于只读操作,一个用于......