右键菜单怎么弹出?

yanj20 2010-04-16 08:55:47
首先看头文件:
//mainwindows.h
class MainWindow : public QMainWindow
{
Q_OBJECT

public:
MainWindow(QWidget *parent = 0);
~MainWindow();

void initTableWidget(QString title,QStringList Headers);
void ctxMenu(const QPoint &pos);

private:
Ui::MainWindow *ui;
TableWidget *m_Table;
};

下面看源文件:
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent), ui(new Ui::MainWindow)
{
ui->setupUi(this);
QStringList Headers;
Headers << "name" << "FileID";
initTableWidget("Catalog Detail",Headers);

this->setCentralWidget(m_Table);

for(int i = 0;i < 10;i++){
this->m_Table->insertRowAtLast();

for(int j = 0;j < 5;j++){

this->m_Table->insertItemWithContent(j,"TestTable");

}//end of for(j)
}//end of for(i)

m_Table->setContextMenuPolicy(Qt::CustomContextMenu);
connect(m_Table, SIGNAL(customContextMenuRequested(const QPoint &)), this, SLOT(ctxMenu(const QPoint &)));

}//end of method QMainWindow

void MainWindow::initTableWidget(QString title,QStringList Headers){

m_Table = new TableWidget();
m_Table->initTableWidget(Headers);
//setTableWidgetStype(m_CatalogTable);


}//end of method initTableWidget

QMenu *cmenu = NULL;
void MainWindow::ctxMenu(const QPoint &pos) {

if(cmenu){//保证同时只存在一个menu,及时释放内存

delete cmenu;
cmenu = NULL;
}//end of if

QMenu *cmenu = new QMenu(m_Table);
cmenu->addAction(tr("Test Item"), this, SLOT(test_slot()));
QAction *ascendSortAction = cmenu->addAction("Test1");
QAction *descendSortAction = cmenu->addAction("Test2");
QAction *filterAction = cmenu->addAction("Test3");
QAction *reshowAction = cmenu->addAction("Test4");

connect(ascendSortAction, SIGNAL(triggered(bool)), this, SLOT(sort_ascend()));
connect(descendSortAction, SIGNAL(triggered(bool)), this, SLOT(sort_descend()));
connect(filterAction, SIGNAL(triggered(bool)), this, SLOT(show_filter_dlg()));
connect(reshowAction, SIGNAL(triggered(bool)), this, SLOT(reshow_data()));
//cmenu->exec(m_Table->mapToGlobal(pos));
cmenu->exec(QCursor::pos());

}//end of method ctxMenu

看完以上代码,大家知道,我的需求就是在TableWiget中弹出右键菜单cmenu,但是经过查询晚上好多帖子,他们都是这么干的,请各位热心达人帮忙一下,看看到底怎么回事右键菜单无法弹出。

测试demo在http://download.csdn.net/source/2248724
...全文
375 9 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
huazhao33 2012-06-15
  • 打赏
  • 举报
回复
请问 楼上的问题解决没?我的在桌面程序里起作用 在移动平台就不行了
huazhao33 2012-06-15
  • 打赏
  • 举报
回复
请问上面的问题解决没?我在桌面程序里可以 , 但是在移动平台就是不行
dellon05 2011-07-07
  • 打赏
  • 举报
回复
请问楼主void ctxMenu(const QPoint &pos);宣告要改到哪?
  • 打赏
  • 举报
回复
这里是个是我以前整理了,可以参考下

http://blog.csdn.net/killua_hzl/archive/2010/02/04/5288782.aspx
MicroSky2813 2010-04-17
  • 打赏
  • 举报
回复
Qt\qt\examples\widgets\shapedclock\release

右键菜单貌似可以很简单的做出来,如这个小例子
yanj20 2010-04-17
  • 打赏
  • 举报
回复
哎,头脑发热,槽函数声明void ctxMenu(const QPoint &pos);放错位置
yanj20 2010-04-17
  • 打赏
  • 举报
回复
谢谢楼上的,有没有见过基于信号customContextMenuRequested(const QPoint &))的右键弹出菜单的demo呢?
yanj20 2010-04-16
  • 打赏
  • 举报
回复
This signal is emitted when the widget's contextMenuPolicy is Qt::CustomContextMenu, and the user has requested a context menu on the widget. The position pos is the position of the context menu event that the widget receives. Normally this is in widget coordinates. The exception to this rule is QAbstractScrollArea and its subclasses that map the context menu event to coordinates of the viewport() .还是不会用啊
shiweiqiang365 2010-04-16
  • 打赏
  • 举报
回复
不知 帮顶。。。。。。

16,815

社区成员

发帖
与我相关
我的任务
社区描述
Qt 是一个跨平台应用程序框架。通过使用 Qt,您可以一次性开发应用程序和用户界面,然后将其部署到多个桌面和嵌入式操作系统,而无需重复编写源代码。
社区管理员
  • Qt
  • 亭台六七座
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧