opengl中在QWidget中添加工具栏和右键菜单——学习总结


/**********************************************************************
*
*       工具栏、右键菜单 的实现
*
**********************************************************************/
void GGLView::initToolBar()
{
    m_openglToolBar = new QToolBar(tr("opengl工具栏"),this);

    m_openglToolBar->setStyleSheet("background-color: rgb(255,197,49);color:rgb(0,0,0);");
    m_openglToolBar->setLayoutDirection(Qt::LeftToRight);
    m_openglToolBar->setIconSize(QSize(24,24));
    m_openglToolBar->setOrientation(Qt::Horizontal);
    m_openglToolBar->setAllowedAreas(Qt::AllToolBarAreas);
    m_openglToolBar->setFloatable(true);
    m_openglToolBar->setMovable(true);
    m_openglToolBar->setEnabled(true);
    m_openglToolBar->setSizePolicy(QSizePolicy::Preferred,QSizePolicy::Fixed);
    m_openglToolBar->setFocusPolicy(Qt::NoFocus);
    m_openglToolBar->setContextMenuPolicy(Qt::DefaultContextMenu);
    m_openglToolBar->setInputMethodHints(Qt::ImhNone);
    m_openglToolBar->setToolButtonStyle(Qt::ToolButtonIconOnly);
//    m_openglToolBar->setToolButtonStyle(Qt::ToolButtonTextBesideIcon);
//    m_openglToolBar->adjustSize();
//    m_openglToolBar->show();

    QAction *actionFrontView    = new QAction(QIcon("images\\3D\\front view.ico"),tr("前视图"), this);
    QAction *actionRearView     = new QAction(QIcon("images\\3D\\rear view.ico"),tr("后视图"), this);
    QAction *actionLeftView     = new QAction(QIcon("images\\3D\\left view.ico"),tr("左视图"), this);
    QAction *actionRightView    = new QAction(QIcon("images\\3D\\right view.ico"),tr("右视图"), this);
    QAction *actionTopView      = new QAction(QIcon("images\\3D\\top view.ico"),tr("俯视图"), this);
    QAction *actionBottomView   = new QAction(QIcon("images\\3D\\bottom view.ico"),tr("仰视图"), this);

    QAction *actionSWView       = new QAction(QIcon("images\\3D\\sw iso.ico"),tr("西南轴测图"), this);
    QAction *actionNWView       = new QAction(QIcon("images\\3D\\nw iso.ico"),tr("西北轴测图"), this);
    QAction *actionSEView       = new QAction(QIcon("images\\3D\\se iso.ico"),tr("东南轴测图"), this);
    QAction *actionNEView       = new QAction(QIcon("images\\3D\\ne iso.ico"),tr("东北轴测图"), this);

    QAction *actionZoomAll      = new QAction(QIcon("images\\3D\\zoomAll.png"),tr("全局缩放"), this);
    QAction *actionZoomIn       = new QAction(QIcon("images\\3D\\zoomIn.png"),tr("放大"), this);
    QAction *actionZoomOut      = new QAction(QIcon("images\\3D\\zoomOut.png"),tr("缩小"), this);
    QAction *actionZoomTwoPoint = new QAction(QIcon("images\\3D\\zoomTwoPoint.png"),tr("局部缩放"), this);
    QAction *actionMove         = new QAction(QIcon("images\\3D\\cursor_drag_arrow.png"),tr("移动"), this);
    QAction *actionPLTopen      = new QAction(QIcon("images\\3D\\plt.png"),tr("打开plt文件"), this);
//    QAction *actionNEView = new QAction(QIcon("images\\3D\\rotateCCW"),tr("旋转"), this);
    m_openglToolBar->addAction(actionFrontView);
    m_openglToolBar->addAction(actionRearView);
    m_openglToolBar->addAction(actionLeftView);
    m_openglToolBar->addAction(actionRightView);
    m_openglToolBar->addAction(actionTopView);
    m_openglToolBar->addAction(actionBottomView);
    m_openglToolBar->addSeparator();
    m_openglToolBar->addAction(actionSWView);
    m_openglToolBar->addAction(actionNWView);
    m_openglToolBar->addAction(actionSEView);
    m_openglToolBar->addAction(actionNEView);
    m_openglToolBar->addSeparator();
    m_openglToolBar->addAction(actionZoomAll);
    m_openglToolBar->addAction(actionZoomIn);
    m_openglToolBar->addAction(actionZoomOut);
    m_openglToolBar->addAction(actionZoomTwoPoint);
    m_openglToolBar->addAction(actionMove);
    m_openglToolBar->addAction(actionPLTopen);

    connect(actionFrontView,SIGNAL(triggered()),this, SLOT(on_action_FrontView_triggered()));
    connect(actionRearView,SIGNAL(triggered()),this, SLOT(on_action_RearView_triggered()));
    connect(actionLeftView,SIGNAL(triggered()),this, SLOT(on_action_LeftView_triggered()));
    connect(actionRightView,SIGNAL(triggered()),this, SLOT(on_action_RightView_triggered()));
    connect(actionTopView,SIGNAL(triggered()),this, SLOT(on_action_TopView_triggered()));
    connect(actionBottomView,SIGNAL(triggered()),this, SLOT(on_action_BottomView_triggered()));

    connect(actionSWView,SIGNAL(triggered()),this, SLOT(on_action_WS_triggered()));
    connect(actionNWView,SIGNAL(triggered()),this, SLOT(on_action_WN_triggered()));
    connect(actionSEView,SIGNAL(triggered()),this, SLOT(on_action_ES_triggered()));
    connect(actionNEView,SIGNAL(triggered()),this, SLOT(on_action_EN_triggered()));

    connect(actionZoomAll,SIGNAL(triggered()),this, SLOT(on_action_zoomAll_triggered()));
    connect(actionZoomIn,SIGNAL(triggered()),this, SLOT(on_action_zoomIn_triggered()));
    connect(actionZoomOut,SIGNAL(triggered()),this, SLOT(on_action_zoomOut_triggered()));
    connect(actionZoomTwoPoint,SIGNAL(triggered()),this, SLOT(on_action_zoomTwoPoint_triggered()));
    connect(actionMove,SIGNAL(triggered()),this, SLOT(on_action_move_triggered()));
    connect(actionPLTopen,SIGNAL(triggered()),this, SLOT(on_actionPLT_triggered()));

    m_btnOpengl = new QPushButton(tr("click"),this);
    m_btnOpengl->setGeometry(40,40,50,50);

//    qDebug() << "move :" << m_openglToolBar->isMovable();
//    qDebug() << "float:" << m_openglToolBar->isFloatable()
//                << m_openglToolBar->isFloating();
}


void GGLView::on_action_FrontView_triggered()
{
    OnViewType(VIEW_FRONT);
}

void GGLView::on_action_RearView_triggered()
{
    OnViewType(VIEW_BACK);
}

void GGLView::on_action_LeftView_triggered()
{
    OnViewType(VIEW_LEFT);
}

void GGLView::on_action_RightView_triggered()
{
    OnViewType(VIEW_RIGHT);
}

void GGLView::on_action_TopView_triggered()
{
    OnViewType(VIEW_TOP);
}

void GGLView::on_action_BottomView_triggered()
{
    OnViewType(VIEW_BOTTOM);
}

void GGLView::on_action_WS_triggered()
{
    OnViewType(VIEW_SW_ISOMETRIC);
}

void GGLView::on_action_WN_triggered()
{
    OnViewType(VIEW_NW_ISOMETRIC);
}

void GGLView::on_action_ES_triggered()
{
    OnViewType(VIEW_SE_ISOMETRIC);
}

void GGLView::on_action_EN_triggered()
{
    OnViewType(VIEW_NE_ISOMETRIC);
}

void GGLView::on_action_zoomIn_triggered()
{
    Zoom(0.9);
}

void GGLView::on_action_zoomOut_triggered()
{
    Zoom(1.1);
}

void GGLView::on_action_zoomAll_triggered()
{
    qDebug() << "zoom all";
    ZoomAll();
}

void GGLView::on_action_zoomTwoPoint_triggered()
{
    m_bZoomTwoPoint = true;
}

void GGLView::on_action_shade_triggered()
{
    m_pGLSet->Shading(!m_pGLSet->IsShading());
    updateGL();
}

void GGLView::on_action_Rotate_triggered()
{

}

void GGLView::on_action_move_triggered()
{
    /// 两种皆可,后面一种较稳妥
//    m_bDragMove = true;
    setDragMove(true);
}

void GGLView::on_actionPLT_triggered()
{
//    qDebug() << "start plt file load";
//    QString pltPath = QDir::currentPath() + "//hpgl";

//    QString fileName = QFileDialog::getOpenFileName(this,
//                                                    tr("打开plt文件"),
//                                                    pltPath,tr("Text files (*.txt *.plt)"));
//    if("" == fileName)
//    {
//        qDebug() << "do not open any plt file!!!";
//        return;
//    }

//    qDebug() << "file name:" << fileName.toStdString().c_str() << '\n';
//    HpglRead(HPGLCodeBuffer,fileName.toStdString().c_str());//解析HPGL代码

//    cleanPltList();
//    initPltGL(HPGLCodeBuffer,HPGLReadInfo.RealWriteCount);
    STLfileLoad(fileName);
    updateGL();
    showPartTreeWidget(ui->treeWidget);
//    qDebug() << "click PLT Open button success!!!";
}

二、右键菜单实现功能:显示和隐藏工具栏

void GGLView::contextMenuEvent(QContextMenuEvent *event)
{
    Q_UNUSED(event);
    QCursor cur=this->cursor();
    QMenu *menu=new QMenu(this);
    if(m_openglToolBar->isEnabled())
    {
        QAction *isShowToolBarAction = new QAction(tr("隐藏工具栏"),this);
        connect(isShowToolBarAction,SIGNAL(triggered()),this, SLOT(ShowToolBar()));
        menu->addAction(isShowToolBarAction); //添加菜单项1
    }
    else
    {
        QAction *isShowToolBarAction = new QAction(tr("显示工具栏"),this);
        connect(isShowToolBarAction,SIGNAL(triggered()),this, SLOT(ShowToolBar()));
        menu->addAction(isShowToolBarAction); //添加菜单项1
    }

    menu->exec(cur.pos()); //关联到光标
}

void GGLView::ShowToolBar()
{
    if(m_openglToolBar->isEnabled())
    {
        m_openglToolBar->setEnabled(false);
        m_openglToolBar->hide();
    }
    else
    {
        m_openglToolBar->setEnabled(true);
        m_openglToolBar->show();
    }
}

注意:

1. 在QWidget中添加的工具栏,无法移动和悬浮,只能在左上角,一行或一列显示

坐等其他同仁的解答














  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值