写在前面
最近在做三维gis数据显示相关的内容,开发使用的是OSG和OSGEarth,因为osg本身对界面显示这块比较弱,不能在三维场景中呈现较好的二维界面效果,所以我想的是将Qt下开发的界面集成到OSG中,王锐老师的《OpenSceneGraph3 cookbook》中有一节是专门讲这个的,算是个引子,有兴趣的同学可以先读一读这一节的内容。在实现的时候遇到了很多的问题,网上相关的资料又比较少,所以摸索了一周左右,记录一下解决过程。
正文
先写一个简单的界面类
//InfoWidget.h
#ifndef INFOWIDGET_H
#define INFOWIDGET_H
#include <QWidget>
#include <QGridLayout>
#include <QVBoxLayout>
#include <QLabel>
class InfoWidget: public QWidget
{
Q_OBJECT
public:
InfoWidget();
~InfoWidget();
void setNameValue(const QString& labelValue);
const QString getNameValue() const;
void setAttribute_1_Value(const QString& labelValue);
const QString getAttribute_1_Value() const;
protected:
void paintEvent(QPaintEvent * event);
private:
QLabel* nameLabel;
QLabel* nameValue;
QLabel* attribute_1_Label;
QLabel* attribute_1_Value;
QGridLayout* mGridLayout;
QVBoxLayout* mVBoxLayout;
};
#endif //INFOWIDGET_H
//InfoWidget.cpp
#include "InfoWidget.h"
#include <QPainter>
InfoWidget::InfoWidget()
{
//文字标签
nameLabel = new QLabel(QString::fromLocal8Bit ("姓名:"));
nameValue = new QLabel(QString::fromLocal8Bit ("张三"));
attribute_1_Label = new QLabel(QString::fromLocal8Bit ("性别:"));
attribute_1_Value = new QLabel(QString::fromLocal8Bit ("男"));
//设置标签字体,字号要尽量大一点,以便在缩放时看的清楚一点
QFont mFont ( QString::fromLocal8Bit ("楷体"),40,75);
nameLabel->setFont(mFont);
nameValue->setFont(mFont);
attribute_1_Label->setFont(mFont);
attribute_1_Value->setFont(mFont);
//设置标签颜色
QString textStyle = "color: rgb(255, 255, 51);"
nameLabel->setStyleSheet(textStyle);
nameValue->setStyleSheet(textStyle);
attribut