PDA

View Full Version : QItemDelegate Context Menu



aekilic
27th November 2008, 22:29
Dear All

We have a QTableWidget and we make a delegate for this. For one of the coloumn we need a context menu. We could bring a context menu but the problem is context menu is not standart. Is there a solution for this, or can you make a sugestion for this?

Please help

wysota
27th November 2008, 23:08
Delegate has nothing to do with context menus. You have to do that in the view like for any other widget.

aekilic
28th November 2008, 08:00
What I mean is like this

In my delegate fuction I have createActions to create action which changes in every row.


void ProformaDelegate::createActions()
{

QSqlQuery fiyat_tipleri;
fiyat_tipleri.exec("SELECT id, stokid from table where if = '" + xxx +"';");
int i = 0;
while(fiyat_tipleri.next()){

recentFileActs[i] = new QAction(fiyat_tipleri.value(3).toString(), this);

++i;
sayi = i;
}
}

And in createEditor part I ask for the delegate.


else if(index.column() == 5 || index.column() == 8)
{

MyDoubleSpinBox2 *editor = new MyDoubleSpinBox2(parent);
editor->setMinimum(0);
editor->setMaximum(4000000000);
editor->setDecimals(4);
editor->setSuffix(" " + totalParaBirimi);
editor->setAlignment(Qt::AlignRight);

for (int i = 0; i < sayi; ++i)
editor->addAction(recentFileActs[i]);
editor->setContextMenuPolicy(Qt::ActionsContextMenu);

return editor;
}

The problem is I could not start createActions() functions from createEditor() part, and also send information (xxx) for the SQL query

wysota
28th November 2008, 08:05
What do you mean you couldn't start it? It doesn't compile or you don't see the result or...? BTW. I think your SQL statement is invalid - 'if' is probably a function name which may cause a clash with a database field with the same name if you have one.

aekilic
28th November 2008, 08:19
I dont have a problem in compiling.

But what I want to do is everytime I come to column 5 of column 8 I want the delegate start fuction createActions(), but when I write like this



if(index.column() == 5 || index.column() == 8)

{
MyDoubleSpinBox2 *editor = new MyDoubleSpinBox2(parent);
editor->setMinimum(0);
editor->setMaximum(4000000000);
editor->setDecimals(4);
editor->setSuffix(" " + totalParaBirimi);
editor->setAlignment(Qt::AlignRight);

createActions();

for (int i = 0; i < sayi; ++i)
editor->addAction(recentFileActs[i]);
editor->setContextMenuPolicy(Qt::ActionsContextMenu);

return editor;

}

I could not compile it. I get a error like

error: passing `const ProformaDelegate'
as `this' argument of `void ProformaDelegate::createActions()' discards qualifie
rs

aekilic
1st December 2008, 08:21
any idea? How can I do it?

wysota
1st December 2008, 10:00
In general you shouldn't do it this way or you should make your createActions() method const.

aekilic
1st December 2008, 11:05
I understand,

But can you help me how can I do it?

What I would like to do is,

Think about a qtablewidget,

for every row there are different data. For everydata (for every row) I would like to biring a information from SQL, when context menu requested, I need to run sql query with a data on the row and bring information for the row with context.

Can you help?

wysota
1st December 2008, 11:13
Reimplement contextMenuEvent() from QTableWidget.

aekilic
1st December 2008, 11:19
any example you could help me?

jpn
2nd December 2008, 19:25
any example you could help me?
http://www.qtcentre.org/forum/p-drag-drop-using-qtablewidget-post30148/postcount2.html

aekilic
2nd December 2008, 23:03
the problem I have now is,

I could use

connect(tableProformaindex, SIGNAL(customContextMenuRequested (QPoint)), this, SLOT(createActions(QPoint)));

but after the window open it works one time, but after that it never works.

aekilic
3rd December 2008, 07:51
any thing you could help me?

jpn
3rd December 2008, 08:17
the problem I have now is,

I could use

connect(tableProformaindex, SIGNAL(customContextMenuRequested (QPoint)), this, SLOT(createActions(QPoint)));

but after the window open it works one time, but after that it never works.

There is no way we can help you unless you show the relevant code.

aekilic
3rd December 2008, 08:21
Dear Jpn

What we are trying to is, for every row of a QTableWidget, we need a new menu...

Is there a way that we could do it?

wysota
3rd December 2008, 09:28
Yes, create a regular context menu :) In the event handler create an instance of QMenu, assign it the actions you want to show and call QMenu::exec(). And please read the docs.

jpn
3rd December 2008, 09:29
What we are trying to is, for every row of a QTableWidget, we need a new menu...

Is there a way that we could do it?
I have already posted a link which shows an example how to do it. Use QTableWidget::itemAt() to get the item below requested point. Here's one more example with signals and slots: http://www.qtcentre.org/forum/p-qlistwidget-right-mouse-click-post32647/postcount5.html