• 7153阅读
  • 10回复

谁有右键弹出菜单的代码呢? 谢谢了。。 [复制链接]

上一主题 下一主题
离线robertkun
 

只看楼主 正序阅读 楼主  发表于: 2009-06-04
谁有右键弹出菜单的代码呢? 谢谢了。。

这个方法试过了,在我这咋不好使呢。。

void LineEdit::contextMenuEvent(QContextMenuEvent *event)
{
  QMenu *menu = createStandardContextMenu();
     QList<QAction *> actList = menu->actions();
    
     actList[UndoAct]->setText(QString::fromLocal8Bit("撤销(&U)"));
     actList[RedoAct]->setText(QString::fromLocal8Bit("重复(&R)"));
     actList[CutAct]->setText(QString::fromLocal8Bit("剪切(&T)"));
     actList[CopyAct]->setText(QString::fromLocal8Bit("复制(&C)"));
     actList[PasteAct]->setText(QString::fromLocal8Bit("粘帖(&P)"));
     actList[ClearAct]->setText(QString::fromLocal8Bit("删除"));
     actList[SelectAllAct]->setText(QString::fromLocal8Bit("全选"));
    

  //...
  menu->exec(event->globalPos());
  delete menu;
}
                                              简单的生活使人快乐!
离线wuleeemail

只看该作者 10楼 发表于: 2009-06-12
我倒是觉得楼主最早先的方法好,能想着用容器的方式来建,只是当中出了一些小差错没有实现。后面实现的方式只要看例子都知道的,不稀奇了!
离线robertkun

只看该作者 9楼 发表于: 2009-06-12
哈哈,谢谢了,就它了,偶的标谁,能用就行。。
                                              简单的生活使人快乐!
离线duduqq

只看该作者 8楼 发表于: 2009-06-05
在QT DEMO里有个例子mainwindows/menus/
离线robertkun

只看该作者 7楼 发表于: 2009-06-05
噢,感谢版主。

我换了一种方法:

void pixmapScene::contextMenuEvent(QGraphicsSceneContextMenuEvent *event)
{
    mMenu = new QMenu();
    mMenu->setObjectName("menu");
    mMenu->setWindowOpacity(0.8);


    actDel = new QAction(this);
    actDel->setText("Delete");

    actUp = new QAction(this);
    actUp->setText("UP");

    actDown = new QAction(this);
    actDown->setText("Down");

    mMenu->addAction(actDel);
    mMenu->addAction(actUp);
    mMenu->addAction(actDown);

    mMenu->exec(event->screenPos());
}

这个是加到Scene 中的快捷菜单,不容易啊。。呜呜。。
                                              简单的生活使人快乐!
离线shiroki

只看该作者 6楼 发表于: 2009-06-05
sigh, 文档没好好看阿。 createStandardContextMenu这个函数只有qtextedit, qlineedit这几个类有, 普通的窗体类是没有的。
普通的窗体类里你得自己new一个菜单
--
shiro is White
ki is tree
http://www.cuteqt.com
论坛 http://www.cuteqt.com/bbs
博客 http://www.cuteqt.com/blog
博客镜像: http://sites.cuteqt.com/cuteqt
Linux/Qt/嵌入式讨论群 http://qun.qq.com/air/5699823
离线robertkun

只看该作者 5楼 发表于: 2009-06-05
引用第4楼shiroki于2009-06-05 11:40发表的  :
pixeltest从哪个类派生的阿?



这是主程序的。。
                                              简单的生活使人快乐!
离线shiroki

只看该作者 4楼 发表于: 2009-06-05
pixeltest从哪个类派生的阿?
--
shiro is White
ki is tree
http://www.cuteqt.com
论坛 http://www.cuteqt.com/bbs
博客 http://www.cuteqt.com/blog
博客镜像: http://sites.cuteqt.com/cuteqt
Linux/Qt/嵌入式讨论群 http://qun.qq.com/air/5699823
离线robertkun

只看该作者 3楼 发表于: 2009-06-05
引用第1楼duduqq于2009-06-04 17:47发表的  :
你让菜单一显示出来又Delete了


dodoqq 帮忙看看这是什么错误??

void PixmapTest::contextMenuEvent(QContextMenuEvent *event)
{
    QMenu *menu = createStandardContextMenu();
    QList<QAction *> actList = menu->actions();

    actList[UndoAct]->setText(QString::fromLocal8Bit("撤销(&U)"));
    actList[RedoAct]->setText(QString::fromLocal8Bit("重复(&R)"));
    actList[CutAct]->setText(QString::fromLocal8Bit("剪切(&T)"));
    actList[CopyAct]->setText(QString::fromLocal8Bit("复制(&C)"));
    actList[PasteAct]->setText(QString::fromLocal8Bit("粘帖(&P)"));
    actList[ClearAct]->setText(QString::fromLocal8Bit("删除"));
    actList[SelectAllAct]->setText(QString::fromLocal8Bit("全选"));


    //...
    menu->exec(event->globalPos());
    delete menu;
}

这段代码报错如下:
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
pixmaptest.cpp
.\pixmaptest.cpp(356) : error C3861: 'createStandardContextMenu': identifier not found
.\pixmaptest.cpp(359) : error C2065: 'UndoAct' : undeclared identifier
.\pixmaptest.cpp(359) : error C2227: left of '->setText' must point to class/struct/union/generic type
.\pixmaptest.cpp(360) : error C2065: 'RedoAct' : undeclared identifier
.\pixmaptest.cpp(360) : error C2227: left of '->setText' must point to class/struct/union/generic type
.\pixmaptest.cpp(361) : error C2065: 'CutAct' : undeclared identifier
.\pixmaptest.cpp(361) : error C2227: left of '->setText' must point to class/struct/union/generic type
.\pixmaptest.cpp(362) : error C2065: 'CopyAct' : undeclared identifier
.\pixmaptest.cpp(362) : error C2227: left of '->setText' must point to class/struct/union/generic type
.\pixmaptest.cpp(363) : error C2065: 'PasteAct' : undeclared identifier
.\pixmaptest.cpp(363) : error C2227: left of '->setText' must point to class/struct/union/generic type
.\pixmaptest.cpp(364) : error C2065: 'ClearAct' : undeclared identifier
.\pixmaptest.cpp(364) : error C2227: left of '->setText' must point to class/struct/union/generic type
.\pixmaptest.cpp(365) : error C2065: 'SelectAllAct' : undeclared identifier
.\pixmaptest.cpp(365) : error C2227: left of '->setText' must point to class/struct/union/generic type
                                              简单的生活使人快乐!
离线hys97

只看该作者 2楼 发表于: 2009-06-05
我也想做这个功能,怎么没有前辈执教呢?
离线duduqq

只看该作者 1楼 发表于: 2009-06-04
你让菜单一显示出来又Delete了
快速回复
限100 字节
 
上一个 下一个