PDA

View Full Version : Context Menu on QTableWidget



ankurjain
8th April 2006, 06:15
hi all,

i've made a dll to provide some table functions ( excel like ) to my main project. It works fine with linkage and all. The code to make context menu and calling it is also in the dll part, in the constructor.

My problem is, in my table in the main project, i get context menu at all places, except at the headers. Like in excel on right click on the headers select all rows/columns and giv a context menu there also. My context menu is not coming on the header.

what can be the problem.
below is my dll code:



//QGrid.cpp

#include <QtGui>
#include "QGrid.h"
#include <QMessageBox>


QGrid::QGrid(int r, int c,QWidget *parent) : QTableWidget(r,c,parent)
{
createActions();
setUpContextMenu();
}


QGrid::QGrid(QWidget *parent) : QTableWidget(parent)
{
createActions();
setUpContextMenu();
}

void QGrid::setUpContextMenu()
{
addAction(copyAct);
addAction(pasteAct);
...
setContextMenuPolicy(Qt::ActionsContextMenu);
}

void QGrid::createActions()
{
copyAct = new QAction(QIcon(":/images/copy.png"), "&Copy", this);
copyAct->setShortcut(tr("Ctrl+C"));
copyAct->setStatusTip(tr("Copy the current selection's contents to the "
"clipboard"));
connect(copyAct, SIGNAL(triggered()), this, SLOT(copy()));

pasteAct = new QAction(QIcon(":/images/paste.png"), "&Paste", this);
pasteAct->setShortcut(tr("Ctrl+V"));
pasteAct->setStatusTip(tr("Paste the clipboard's contents into the current "
"selection"));
connect(pasteAct, SIGNAL(triggered()), this, SLOT(paste()));

....
}



//QGrid.h

#ifndef QGRID_H
#define QGRID_H
#include <QtCore/qglobal.h>
#include <QtGui/QDialog>
#include <QAction>
#include <QTableWidget>

#ifdef BUILD_QGRID
# ifdef Q_CC_MSVC
# define QGRID_EXPORT Q_DECL_EXPORT
# else
# define QGRID_EXPORT Q_DECL_IMPORT
# endif
#else
# define QGRID_EXPORT
#endif

class QGRID_EXPORT QGrid : public QTableWidget
{
Q_OBJECT
public:
QGrid(int c,int r,QWidget *parent = 0);
QGrid(QWidget *parent = 0);
QClipboard *clip;
QTableWidget *table;

private:
QAction *copyAct;
QAction *pasteAct;
....
public slots:

void copy();
void paste();
....
public:
void setUpContextMenu();
void createActions();
};
#endif

please help ....

jpn
8th April 2006, 09:58
Put the context menu also for the headers:

QHeaderView* QTableView::horizontalHeader() const
QHeaderView* QTableView::verticalHeader() const

ankurjain
8th April 2006, 10:16
hi jpn,
thanx 4 replying but can u explain a bit more ... u c i m a novice ... i m also already trying to solve this the same way u say. but how shall i call contextmenu functions on the header . please also hav a look at the code in 1st post.
i hav defined 2 functions there, how shall i call those functions....

jpn
8th April 2006, 10:20
Ok, sorry for not being clear and understandable enough.
I meant something like this:


void QGrid::setUpContextMenu()
{
...
horizontalHeader()->addAction(someAct);
horizontalHeader()->setContextMenuPolicy(Qt::ActionsContextMenu);
// and the same goes for verticalHeader..
}

ankurjain
8th April 2006, 10:35
thank you jpn,

my problem is solved ( except that my program crashes on clicking any items of context menu, i will solve that ).

I am also having one more problem, i had assigned shortcutkeys to these actions via



cutAct->setShortcut(tr("Ctrl+X"));


But this doesn't seem to work. is there some mistake in that ?

jpn
8th April 2006, 14:01
Hmm, I don't see anything wrong with that..
Have you tried to change the shortcut context (http://doc.trolltech.com/4.1/qaction.html#shortcutContext-prop) for that action? Try setting it to Qt::ApplicationShortcut.

ankurjain
11th April 2006, 05:05
hi jpn,

i tried what u told, but still no use. FYI i am also using some delegates in the table. can it cause some problems ?

jpn
11th April 2006, 14:53
Hmm, if you are using the actions context menu policy I'm not really sure what's the problem.
As the action is created somewhere in the constructor or so, that it exists during the lifetime of the widget, the action should be triggered as well.
Maybe the shortcut event goes to a wrong widget or something.

Anyway, how about this way, does it work?


QShortcut* copyShortcut = new QShortcut(QKeySequence("Ctrl+C"), this);
connect(copyShortcut, SIGNAL(activated()), copyAct, SLOT(trigger()));

ankurjain
12th April 2006, 12:37
Still not working :(

nikhilqt
17th December 2009, 09:52
Hi ankur,

Did your concept worked? Could you please let me know some coding part regarding the copying into clipboard and pasting back from clipboard. Even my requirement is same. It would be great if yo can share your dll/code.

Thanks in advance,
Nikhil