这里的地理信息不是广义上的地理信息,专门指Qt中的位置和宽高,但是Qt中的位置是左上角的位置,这本来也没什么,只是我们以后用view什么类的时候怎么知道我是否放对了位置呢?所以我就做了这个类,用来标记像素点的位置。包括中心点和四个角的位置。代码如下
#include "widget.h"
#include <QApplication>
#include<QFrame>
#include<QDebug>
#include<Qlabel>
#include<QWidget>
#include<QPalette>
#include"qlabelforgeography.h"
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
Widget w;
//栈版原型
int center_x=230;
int center_y=310;
int widthOfCenterBlock=80;//自定义的标记块的尺寸
int heightOfCenterBlock=30;//
int cornerOfLeftAndTop_x=center_x-widthOfCenterBlock/2;
int cornerOfLeftAndTop_y=center_y-heightOfCenterBlock/2;
//带框的标签,中部标数据
QLabel qframeForgeoGraphy("");
qframeForgeoGraphy.move(cornerOfLeftAndTop_x,cornerOfLeftAndTop_y);
qframeForgeoGraphy.setText("center:\n"+QString::number(center_x)+","+QString::number(center_y));
qframeForgeoGraphy.setParent(&w);
qframeForgeoGraphy.resize(widthOfCenterBlock,heightOfCenterBlock);
qframeForgeoGraphy.setFrameShape(QFrame::Box);
qframeForgeoGraphy.setAlignment(Qt::AlignHCenter|Qt::AlignVCenter);
qframeForgeoGraphy.setTextInteractionFlags(Qt::TextEditorInteraction);
//中心点
QFrame qframeForgeoGraphy_point;
QPalette palette = qframeForgeoGraphy_point.palette();
qframeForgeoGraphy_point.setParent(&w);
qframeForgeoGraphy_point.move(center_x-1,center_y-1);
qframeForgeoGraphy_point.resize(2,2);
qframeForgeoGraphy_point.setFrameShape(QFrame::Box);
palette.setColor(QPalette::WindowText, Qt::red);
qframeForgeoGraphy_point.setPalette(palette);
//中心点的框
QFrame qframeForgeoGraphy_point_frame;
qframeForgeoGraphy_point.setPalette(palette);
qframeForgeoGraphy_point_frame.setParent(&w);//用Qframe把center标出来得了,可别paintEvent重绘了
qframeForgeoGraphy_point_frame.move(center_x-4,center_y-4);
qframeForgeoGraphy_point_frame.resize(8,8);
qframeForgeoGraphy_point_frame.setFrameShape(QFrame::Box);
qframeForgeoGraphy_point_frame.setPalette(palette);
palette = qframeForgeoGraphy_point_frame.palette();
palette.setColor(QPalette::WindowText, Qt::green);
qframeForgeoGraphy_point_frame.setPalette(palette);
//标签,四个角的数据
QLabel qframeForgeoGraphy_LeftAndTop(QString::number(cornerOfLeftAndTop_x)+","+QString::number(cornerOfLeftAndTop_y),&w);
qframeForgeoGraphy_LeftAndTop.move(cornerOfLeftAndTop_x,cornerOfLeftAndTop_y-qframeForgeoGraphy_LeftAndTop.font().pointSizeF()*1.7);
QLabel qframeForgeoGraphy_LeftAndDown(QString::number(cornerOfLeftAndTop_x)+","+QString::number(cornerOfLeftAndTop_y+heightOfCenterBlock),&w);
qframeForgeoGraphy_LeftAndDown.move(cornerOfLeftAndTop_x,cornerOfLeftAndTop_y+
heightOfCenterBlock);
QLabel qframeForgeoGraphy_RightAndTop(QString::number(cornerOfLeftAndTop_x+widthOfCenterBlock)+","+QString::number(cornerOfLeftAndTop_y),&w);
qframeForgeoGraphy_RightAndTop.move(cornerOfLeftAndTop_x+widthOfCenterBlock,cornerOfLeftAndTop_y-qframeForgeoGraphy_RightAndTop.font().pointSizeF()*1.7);
QLabel qframeForgeoGraphy_RightAndDown(QString::number(cornerOfLeftAndTop_x+widthOfCenterBlock)+","+QString::number(cornerOfLeftAndTop_y+heightOfCenterBlock),&w);
qframeForgeoGraphy_RightAndDown.move(cornerOfLeftAndTop_x+widthOfCenterBlock,cornerOfLeftAndTop_y+ heightOfCenterBlock);
//原始的点
QLabel qframeForgeoGraphy_orign("");
qframeForgeoGraphy_orign.move(230,310);
qframeForgeoGraphy_orign.setText("center:\n"+QString::number(12)+","+QString::number(32)+"\n这是左上角\n在230,310\n的QLabel");
qframeForgeoGraphy_orign.setParent(&w);
qframeForgeoGraphy_orign.resize(80,160);
qframeForgeoGraphy_orign.setFrameShape(QFrame::Box);
qframeForgeoGraphy_orign.setAlignment(Qt::AlignHCenter|Qt::AlignVCenter);
qframeForgeoGraphy_orign.setTextInteractionFlags(Qt::TextEditorInteraction);//让鼠标键盘可以选
w.show();
return a.exec();
}
其中 Widget w中什么也没做,qlabel和qframe也不能重绘,效果如顶页的图,标记了中心用红点表示,和四个角的坐标。
但是还是繁琐,想把它单独封装成一个类,甚至一个控件,其实控件本身就是类。就新建一个QLabelForGeography类,头文件qlabelforgeography.h源文件qlabelforgeography.cpp
#ifndef LABELFORGEOGRAPHY_H
#define LABELFORGEOGRAPHY_H
#include <QWidget>
#include<QLabel>
#include<QDebug>
class QLabelForGeography : public QLabel//头文件
{
Q_OBJECT
public:
explicit QLabelForGeography(QWidget *parent = nullptr,Qt::WindowFlags f=Qt::WindowFlags());
// explicit QLabelForGeography(const QString &text, QWidget *parent=nullptr, Qt::WindowFlags f=Qt::WindowFlags());
explicit QLabelForGeography(const QString &text, QWidget *parent=nullptr,
const int ¢er_x=0,
const int ¢er_y=0,
const int &widthOfCenterBlock=0,
const int &heightOfCenterBlock=0,
Qt::WindowFlags f=Qt::WindowFlags());
signals:
};
#endif // LABELFORGEOGRAPHY_H
#include "qlabelforgeography.h"//源文件
QLabelForGeography::QLabelForGeography(QWidget *parent, Qt::WindowFlags f): QLabel{parent}//没用的
{
}
QLabelForGeography::QLabelForGeography(const QString &text, QWidget *parent, const int ¢er_x, const int ¢er_y, const int &widthOfCenterBlock, const int &heightOfCenterBlock, Qt::WindowFlags f): QLabel{text,parent}
{
qDebug()<<center_x<<center_y<<widthOfCenterBlock<<heightOfCenterBlock;
int cornerOfLeftAndTop_x=center_x-widthOfCenterBlock/2;
int cornerOfLeftAndTop_y=center_y-heightOfCenterBlock/2;
move(cornerOfLeftAndTop_x,cornerOfLeftAndTop_y);
setText("center:\n"+QString::number(center_x)+","+QString::number(center_y));
resize(widthOfCenterBlock,heightOfCenterBlock);
setFrameShape(QFrame::Box);
setAlignment(Qt::AlignHCenter|Qt::AlignVCenter);
setTextInteractionFlags(Qt::TextEditorInteraction);
//中心点
QFrame *qframeForgeoGraphy_point=new QFrame();//如是栈上创建的出了构造就销毁了哪怕父类是顶层的widget
QPalette palette = qframeForgeoGraphy_point->palette();//被销毁也无所谓,只要qframeForgeoGraphy_point没有被销毁就行
qframeForgeoGraphy_point->setParent(parent);
qframeForgeoGraphy_point->move(center_x-1,center_y-1);
qframeForgeoGraphy_point->resize(2,2);
qframeForgeoGraphy_point->setFrameShape(QFrame::Box);
palette.setColor(QPalette::WindowText, Qt::red);
qframeForgeoGraphy_point->setPalette(palette);
//中心点的框
QFrame *qframeForgeoGraphy_point_frame=new QFrame();
qframeForgeoGraphy_point_frame->setParent(parent);//用Qframe把center标出来得了,可别paintEvent重绘了
qframeForgeoGraphy_point_frame->move(center_x-4,center_y-4);
qframeForgeoGraphy_point_frame->resize(8,8);
qframeForgeoGraphy_point_frame->setFrameShape(QFrame::Box);
qframeForgeoGraphy_point_frame->setPalette(palette);
palette = qframeForgeoGraphy_point_frame->palette();
palette.setColor(QPalette::WindowText, Qt::green);
qframeForgeoGraphy_point_frame->setPalette(palette);
//标签,四个角的数据
QLabel *qframeForgeoGraphy_LeftAndTop = new QLabel(QString::number(cornerOfLeftAndTop_x)+","+QString::number(cornerOfLeftAndTop_y),parent);
qframeForgeoGraphy_LeftAndTop->move(cornerOfLeftAndTop_x,cornerOfLeftAndTop_y-qframeForgeoGraphy_LeftAndTop->font().pointSizeF()*1.7);
QLabel *qframeForgeoGraphy_LeftAndDown = new QLabel(QString::number(cornerOfLeftAndTop_x)+","+QString::number(cornerOfLeftAndTop_y+heightOfCenterBlock),parent);
qframeForgeoGraphy_LeftAndDown->move(cornerOfLeftAndTop_x,cornerOfLeftAndTop_y+heightOfCenterBlock);
QLabel *qframeForgeoGraphy_RightAndTop = new QLabel(QString::number(cornerOfLeftAndTop_x+widthOfCenterBlock)+","+QString::number(cornerOfLeftAndTop_y),parent);
qframeForgeoGraphy_RightAndTop->move(cornerOfLeftAndTop_x+widthOfCenterBlock,cornerOfLeftAndTop_y-qframeForgeoGraphy_RightAndTop->font().pointSizeF()*1.7);
QLabel *qframeForgeoGraphy_RightAndDown = new QLabel(QString::number(cornerOfLeftAndTop_x+widthOfCenterBlock)+","+QString::number(cornerOfLeftAndTop_y+heightOfCenterBlock),parent);
qframeForgeoGraphy_RightAndDown->move(cornerOfLeftAndTop_x+widthOfCenterBlock,cornerOfLeftAndTop_y+ heightOfCenterBlock);
}
这个类继承了QLabel,在原来的QLabel::QLabel(const QString &text, QWidget *parent = nullptr, Qt::WindowFlags f = Qt::WindowFlags())上进行了扩展新增了四个参数就是中心点的x、y,框的宽高,使用的方法,QLabelForGeography QLabelForGeography_instance("",&w,230,114,52,200)其中第一个参数""没有啥用,最后都会替换成中间的位置信息,第二参数&w是绘图设备在这里是QWidget的对象,第三个是中心点的x第四个是中心点的y第五个是宽,第六个是高。只有一个构造函数干活了没有其它接口。main.cpp的完整代码如下:
#include "widget.h"
#include <QApplication>
#include<QFrame>
#include<QDebug>
#include<Qlabel>
#include<QWidget>
#include<QPalette>
#include"qlabelforgeography.h"
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
Widget w;
//栈版原型
int center_x=230;
int center_y=310;
int widthOfCenterBlock=80;//自定义的标记块的尺寸
int heightOfCenterBlock=30;//
int cornerOfLeftAndTop_x=center_x-widthOfCenterBlock/2;
int cornerOfLeftAndTop_y=center_y-heightOfCenterBlock/2;
//带框的标签,中部标数据
QLabel qframeForgeoGraphy("");
qframeForgeoGraphy.move(cornerOfLeftAndTop_x,cornerOfLeftAndTop_y);
qframeForgeoGraphy.setText("center:\n"+QString::number(center_x)+","+QString::number(center_y));
qframeForgeoGraphy.setParent(&w);
qframeForgeoGraphy.resize(widthOfCenterBlock,heightOfCenterBlock);
qframeForgeoGraphy.setFrameShape(QFrame::Box);
qframeForgeoGraphy.setAlignment(Qt::AlignHCenter|Qt::AlignVCenter);
qframeForgeoGraphy.setTextInteractionFlags(Qt::TextEditorInteraction);
//中心点
QFrame qframeForgeoGraphy_point;
QPalette palette = qframeForgeoGraphy_point.palette();
qframeForgeoGraphy_point.setParent(&w);
qframeForgeoGraphy_point.move(center_x-1,center_y-1);
qframeForgeoGraphy_point.resize(2,2);
qframeForgeoGraphy_point.setFrameShape(QFrame::Box);
palette.setColor(QPalette::WindowText, Qt::red);
qframeForgeoGraphy_point.setPalette(palette);
//中心点的框
QFrame qframeForgeoGraphy_point_frame;
qframeForgeoGraphy_point.setPalette(palette);
qframeForgeoGraphy_point_frame.setParent(&w);//用Qframe把center标出来得了,可别paintEvent重绘了
qframeForgeoGraphy_point_frame.move(center_x-4,center_y-4);
qframeForgeoGraphy_point_frame.resize(8,8);
qframeForgeoGraphy_point_frame.setFrameShape(QFrame::Box);
qframeForgeoGraphy_point_frame.setPalette(palette);
palette = qframeForgeoGraphy_point_frame.palette();
palette.setColor(QPalette::WindowText, Qt::green);
qframeForgeoGraphy_point_frame.setPalette(palette);
//标签,四个角的数据
QLabel qframeForgeoGraphy_LeftAndTop(QString::number(cornerOfLeftAndTop_x)+","+QString::number(cornerOfLeftAndTop_y),&w);
qframeForgeoGraphy_LeftAndTop.move(cornerOfLeftAndTop_x,cornerOfLeftAndTop_y-qframeForgeoGraphy_LeftAndTop.font().pointSizeF()*1.7);
QLabel qframeForgeoGraphy_LeftAndDown(QString::number(cornerOfLeftAndTop_x)+","+QString::number(cornerOfLeftAndTop_y+heightOfCenterBlock),&w);
qframeForgeoGraphy_LeftAndDown.move(cornerOfLeftAndTop_x,cornerOfLeftAndTop_y+
heightOfCenterBlock);
QLabel qframeForgeoGraphy_RightAndTop(QString::number(cornerOfLeftAndTop_x+widthOfCenterBlock)+","+QString::number(cornerOfLeftAndTop_y),&w);
qframeForgeoGraphy_RightAndTop.move(cornerOfLeftAndTop_x+widthOfCenterBlock,cornerOfLeftAndTop_y-qframeForgeoGraphy_RightAndTop.font().pointSizeF()*1.7);
QLabel qframeForgeoGraphy_RightAndDown(QString::number(cornerOfLeftAndTop_x+widthOfCenterBlock)+","+QString::number(cornerOfLeftAndTop_y+heightOfCenterBlock),&w);
qframeForgeoGraphy_RightAndDown.move(cornerOfLeftAndTop_x+widthOfCenterBlock,cornerOfLeftAndTop_y+ heightOfCenterBlock);
//原始的点
QLabel qframeForgeoGraphy_orign("");
qframeForgeoGraphy_orign.move(230,310);
qframeForgeoGraphy_orign.setText("center:\n"+QString::number(12)+","+QString::number(32)+"\n这是左上角\n在230,310\n的QLabel");
qframeForgeoGraphy_orign.setParent(&w);
qframeForgeoGraphy_orign.resize(80,160);
qframeForgeoGraphy_orign.setFrameShape(QFrame::Box);
qframeForgeoGraphy_orign.setAlignment(Qt::AlignHCenter|Qt::AlignVCenter);
qframeForgeoGraphy_orign.setTextInteractionFlags(Qt::TextEditorInteraction);//让鼠标键盘可以选
qDebug()<<qframeForgeoGraphy_orign.textInteractionFlags();
qDebug()<<w.geometry();
//堆版的自定义QLabel为地理位置,也可以用栈的方式创建,但是内部不能用栈了
//todo:要有两个构造函数,一个是设置中心和宽度*高度,一个是左上角和宽度*高度,想要通过左上角去定位也没有什么意义,只要宽和高是原来的二倍就行了
//一共有几个参数?
QLabelForGeography QLabelForGeography_instance("",&w,230,114,52,200);
QLabelForGeography QLabelForGeography_instance_RightAndDown_varis1_5("",&w,256,214,156,300);
QLabelForGeography QLabelForGeography_instance_RightAndDown_varis2_0("",&w,256,214,208,100);
QLabelForGeography QLabelForGeography_instance_RightAndDown_off_set("",&w,510,214,208,100);
// QLabelForGeography QLabelForGeography_instance("im is QLabelForGeography_instance",&w,center_x,center_y,widthOfCenterBlock,heightOfCenterBlock);
w.show();
return a.exec();
}
最后图片:
本来也想做一个从ui界面拖动的控件,但是Qt6.5的自定义控件不好用,可能还是我理解的有问题吧。 但本质上也是实现了用控件去标记绘图设备的地理信息的目的。