Results 1 to 5 of 5

Thread: how to show popup on mouse right click on a cell of table widget

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Oct 2006
    Location
    New Delhi, India
    Posts
    2,467
    Thanks
    8
    Thanked 334 Times in 317 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: how to show popup on mouse right click on a cell of table widget

    Can we see the code ?
    You seem to be using a menu pointer from various places. Without code its hard to say what wrong are u doing.

  2. #2
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: how to show popup on mouse right click on a cell of table widget

    It's called "context menu". See this post. Same approach works for table widget.
    J-P Nurmi

  3. #3
    Join Date
    Jan 2008
    Posts
    2
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: how to show popup on mouse right click on a cell of table widget

    Qt Code:
    1. #include<QtGui>
    2. #include<QPushButton>
    3. #include"newform.h"
    4. #include<iostream>
    5. #include<QString>
    6. #include<QTableWidget>
    7. #include<QWidget>
    8. #include<QAction>
    9. #include<QMenu>
    10. #include<Qt>
    11.  
    12. using namespace std;
    13.  
    14.  
    15. Form::Form(QWidget *parent):QWidget(parent)
    16. {
    17.  
    18. setupUi(this);
    19. //connect(this,SIGNAL(rightClick()),this,SLOT(itemClicked()));
    20.  
    21. //connect(this,SIGNAL(rc(QMouseEvent *)),tableWidget,SLOT(mousePressEvent(QMouseEvent *)));
    22. //connect(this,SIGNAL(cellClicked(int,int)),this,SLOT(mousePressEvent(QMouseEvent *)));
    23. cout<<"\nconstructor called\n";
    24. // if(count==1)
    25. //{
    26. // Form::createActions();
    27. // Form::setupContextMenu();
    28. //}
    29. //setCentralWidget(table);
    30. //mousePressEvent(e);
    31. }
    32. /*Form::Form(int n)
    33. {
    34.   setupUi(this);
    35.   count=n;
    36.  // connect(tableWidget,SIGNAL(cellClicked(int,int)),this,SLOT(mouseClick()));
    37.   if(count==1)
    38.   {
    39.   Form::createActions();
    40.   Form::setupContextMenu();
    41.   }
    42. }*/
    43. void Form::mouseClick()
    44. {
    45. // count=1;
    46. // Form fm;
    47. Form::createActions();
    48. Form::setupContextMenu();
    49. }
    50.  
    51. void Form::display()
    52. {
    53.  
    54. int no_of_row;
    55. int no_of_column;
    56. cout<<"\n"<<"test56767"<<endl;
    57. no_of_column = tableWidget->columnCount ();
    58. no_of_row = tableWidget->rowCount () ;
    59. cout<<"\n ROW "<<no_of_row<<endl;
    60. cout<<"\n Column "<<no_of_column<<endl;
    61.  
    62. //tableWidget->clear ();
    63. //tableWidget->removeColumn(no_of_column);
    64. for(int k=no_of_row;k>=0;k--)
    65. {
    66. cout<<"\t " <<k;
    67. tableWidget->removeRow (k);
    68.  
    69. }
    70. cout<<"\n";
    71. for(int j=0;j<100;j++)
    72. {
    73. tableWidget->insertRow(j);
    74. for(int i=0;i<20;i++)
    75. {
    76. if(i==0)
    77. {
    78. newItem->setText("0");
    79. tableWidget->setItem(j,i,newItem);
    80. }
    81. if(i==1)
    82. {
    83. newItem->setText("1");
    84. tableWidget->setItem(j,i,newItem);
    85. }
    86. if(i==2)
    87. {
    88. newItem->setText("2");
    89. tableWidget->setItem(j,i,newItem);
    90. }
    91. if(i==3)
    92. {
    93. newItem->setText("3");
    94. tableWidget->setItem(j,i,newItem);
    95. }
    96. if(i==4)
    97. {
    98. newItem->setText("4");
    99. tableWidget->setItem(j,i,newItem);
    100. }
    101. if(i==5)
    102. {
    103. newItem->setText("5");
    104. tableWidget->setItem(j,i,newItem);
    105. }
    106. if(i==6)
    107. {
    108. newItem->setText("6");
    109. tableWidget->setItem(j,i,newItem);
    110. }
    111. if(i==7)
    112. {
    113. newItem->setText("7");
    114. tableWidget->setItem(j,i,newItem);
    115. }
    116. if(i==8)
    117. {
    118. newItem->setText("8");
    119. tableWidget->setItem(j,i,newItem);
    120. }
    121. if(i==9)
    122. {
    123. newItem->setText("9");
    124. tableWidget->setItem(j,i,newItem);
    125. }
    126. if(i==10)
    127. {
    128. newItem->setText("10");
    129. tableWidget->setItem(j,i,newItem);
    130. }
    131. if(i==11)
    132. {
    133. newItem->setText("11");
    134. tableWidget->setItem(j,i,newItem);
    135. }
    136. if(i==12)
    137. {
    138. newItem->setText("12");
    139. tableWidget->setItem(j,i,newItem);
    140. }
    141. if(i==13)
    142. {
    143. newItem->setText("13");
    144. tableWidget->setItem(j,i,newItem);
    145. }
    146. if(i==14)
    147. {
    148. newItem->setText("14");
    149. tableWidget->setItem(j,i,newItem);
    150. }
    151. if(i==15)
    152. {
    153. newItem->setText("15");
    154. tableWidget->setItem(j,i,newItem);
    155. }
    156. if(i==16)
    157. {
    158. newItem->setText("16");
    159. tableWidget->setItem(j,i,newItem);
    160. }
    161. if(i==17)
    162. {
    163. newItem->setText("17");
    164. tableWidget->setItem(j,i,newItem);
    165. }
    166. if(i==18)
    167. {
    168. newItem->setText("18");
    169. tableWidget->setItem(j,i,newItem);
    170. }
    171. if(i==19)
    172. {
    173. newItem->setText("19");
    174. tableWidget->setItem(j,i,newItem);
    175. }
    176. }
    177.  
    178. }
    179. //emit rightClick(tableWidget->grabMouse());
    180. }
    181. void Form::mclick()
    182. {
    183. QMenu *contextMenu = new QMenu ( this );
    184. QMenu *subMenu = new QMenu ( this );
    185. Q_CHECK_PTR (contextMenu);
    186. Q_CHECK_PTR (subMenu);
    187.  
    188. subMenu=contextMenu->addMenu ( "Math Opr.");
    189.  
    190. subMenu->addAction(cell_addAction);
    191. subMenu->addAction(cell_subAction);
    192. subMenu->addAction(cell_mulAction);
    193. subMenu->addAction(cell_divAction);
    194. subMenu->addAction(cell_sumAction);
    195. contextMenu->addAction(firstSeparator);
    196. contextMenu->addAction(colorAction);
    197. contextMenu->addAction(fontAction);
    198. contextMenu->addAction(secondSeparator);
    199. contextMenu->addAction(clearAction);
    200.  
    201. pb3->setMenu(contextMenu);
    202.  
    203. //contextMenu->popup(QCursor::pos());
    204. //contextMenu->exec ();
    205.  
    206. //setContextMenuPolicy(Qt::ActionsContextMenu);
    207.  
    208. }
    209.  
    210. void Form::del()
    211. {
    212. { delete cell_addAction;
    213. delete cell_subAction;
    214. delete cell_mulAction;
    215. delete cell_divAction;
    216. delete cell_sumAction;
    217. delete firstSeparator;
    218. delete colorAction;
    219. delete fontAction;
    220. delete secondSeparator;
    221. delete clearAction;
    222. }
    223.  
    224. }
    225.  
    226. void Form::mousePressEvent(QMouseEvent *event)
    227. {
    228. if(count==1)
    229. {
    230. del();
    231. count=0;
    232. }
    233. // cout<<"\n"<<"testmousepressevent455"<<endl;
    234. if(event->button() == Qt::LeftButton)
    235. {
    236. //QString test = QString(event->x()) + ", " + QString(event->y());
    237. QMessageBox::warning( this, tr("Position"), tr( "Left Button Pressed" ) );
    238. }
    239.  
    240. else if(event->button() == Qt::RightButton)
    241. {
    242. //emit rightClick();
    243. //setupContextMenu();
    244. // Form frm;
    245. QMessageBox::warning( this, tr("Position"), tr( "Right Button Pressed" ) );
    246. }
    247.  
    248. }
    249.  
    250.  
    251.  
    252.  
    253. void Form::createActions()
    254. {
    255. count =1;
    256. cell_sumAction = new QAction(tr("Sum"), this);
    257. connect(cell_sumAction, SIGNAL(triggered()), this, SLOT(actionSum()));
    258.  
    259. cell_addAction = new QAction(tr("&Add"), this);
    260. cell_addAction->setShortcut(Qt::CTRL | Qt::Key_Plus);
    261. connect(cell_addAction, SIGNAL(triggered()), this, SLOT(actionAdd()));
    262.  
    263. cell_subAction = new QAction(tr("&Subtract"), this);
    264. cell_subAction->setShortcut(Qt::CTRL | Qt::Key_Minus);
    265. connect(cell_subAction, SIGNAL(triggered()), this, SLOT(actionSubtract()));
    266.  
    267. cell_mulAction = new QAction(tr("&Multiply"), this);
    268. cell_mulAction->setShortcut(Qt::CTRL | Qt::Key_multiply);
    269. connect(cell_mulAction, SIGNAL(triggered()), this, SLOT(actionMultiply()));
    270.  
    271. cell_divAction = new QAction(tr("&Divide"), this);
    272. cell_divAction->setShortcut(Qt::CTRL | Qt::Key_division);
    273. connect(cell_divAction, SIGNAL(triggered()), this, SLOT(actionDivide()));
    274.  
    275. fontAction = new QAction(tr("Font..."), this);
    276. fontAction->setShortcut(Qt::CTRL|Qt::Key_F);
    277. connect(fontAction, SIGNAL(triggered()), this, SLOT(selectFont()));
    278.  
    279. colorAction = new QAction(QPixmap(16, 16), tr("Background &Color..."), this);
    280. connect(colorAction, SIGNAL(triggered()), this, SLOT(selectColor()));
    281.  
    282. clearAction = new QAction(tr("Clear"), this);
    283. clearAction->setShortcut(Qt::Key_Delete);
    284. connect(clearAction, SIGNAL(triggered()), this, SLOT(clear()));
    285.  
    286.  
    287. exitAction = new QAction(tr("E&xit"), this);
    288. connect(exitAction, SIGNAL(triggered()), qApp, SLOT(quit()));
    289.  
    290. firstSeparator = new QAction(this);
    291. firstSeparator->setSeparator(true);
    292.  
    293. secondSeparator = new QAction(this);
    294. secondSeparator->setSeparator(true);
    295.  
    296.  
    297. }
    298.  
    299. void Form::actionSum()
    300. {
    301.  
    302. int row_first = 0;
    303. int row_last = 0;
    304. int row_cur = 0;
    305.  
    306. int col_first = 0;
    307. int col_last = 0;
    308. int col_cur = 0;
    309.  
    310.  
    311. }
    312.  
    313.  
    314.  
    315. void Form::actionMath_helper(const QString &title, const QString &op)
    316. {
    317.  
    318. QString cell1 = "C1";
    319. QString cell2 = "C2";
    320. QString out = "C3";
    321. //QTableWidgetItem *current = tableWidget->currentItem();
    322.  
    323. }
    324.  
    325.  
    326. void Form::actionAdd()
    327. {
    328. actionMath_helper(tr("Addition"), "+");
    329. }
    330.  
    331. void Form::actionSubtract()
    332. {
    333. actionMath_helper(tr("Subtraction"), "-");
    334. }
    335.  
    336. void Form::actionMultiply()
    337. {
    338. actionMath_helper(tr("Multiplication"), "*");
    339. }
    340.  
    341. void Form::actionDivide()
    342. {
    343. actionMath_helper(tr("Division"), "/");
    344. }
    345.  
    346.  
    347. void Form::clear()
    348. {
    349. foreach (QTableWidgetItem *i, tableWidget->selectedItems())
    350. i->setText("");
    351. }
    352.  
    353.  
    354. void Form::setupContextMenu()
    355. {
    356.  
    357.  
    358. addAction(cell_addAction);
    359. addAction(cell_subAction);
    360. addAction(cell_mulAction);
    361. addAction(cell_divAction);
    362. addAction(cell_sumAction);
    363. addAction(firstSeparator);
    364. addAction(colorAction);
    365. addAction(fontAction);
    366. addAction(secondSeparator);
    367. addAction(clearAction);
    368. setContextMenuPolicy(Qt::ActionsContextMenu);
    369. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by jpn; 4th February 2008 at 06:05. Reason: missing [code] tags

  4. #4
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: how to show popup on mouse right click on a cell of table widget

    You didn't check out the example, did you?
    • Please use [code]-tags for small code blocks
    • Please attach large code blocks as files
    • Please post only parts of code that are relevant to the problem
    J-P Nurmi

Similar Threads

  1. How to show custom widget in TreeView's cell :-/
    By WolfMM in forum Qt Programming
    Replies: 2
    Last Post: 7th July 2007, 11:16
  2. Replies: 11
    Last Post: 7th September 2006, 23:15

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.