PDA

View Full Version : QMenu in QHeaderView



munna
13th July 2006, 14:54
Hi,

How can I get the rect of a section of headerView so that I can make a menu appear in a section of a headerView.

I wrote the following code but it is not working.




DataTable::DataTable(QWidget *parent) : QTableWidget(parent)
{
setAlternatingRowColors(true);

verticalHeader()->hide();

tableHeader = new TableHeader(this);
connect(tableHeader,SIGNAL(clicked(const QModelIndex &)),this,SLOT(headerClicked(const QModelIndex &)));
setHorizontalHeader(tableHeader);

headerMenu = new QMenu(tr("Undefined"),tableHeader);
connect(headerMenu,SIGNAL(triggered(QAction *)),this,SLOT(headerMenuTriggered(QAction *)));

populateHeaderMenu();//Add the actions to the menu
}

//The following slot is not called itself
void DataTable::headerClicked(const QModelIndex &index)
{
//I wonder if this is correct.
QRect rect = visualRect(index);
headerMenu->exec(tableHeader->mapToGlobal(QPoint(rect.x(),rect.y())));
}



The same menu needs to appear for all the columns and therefore I used this way.
Is there a better way of doing it.

Can someone please help me with this?

Thanks a lot.

munna
14th July 2006, 12:45
Any ideas ??

jacek
14th July 2006, 13:18
Shouldn't it be:
void DataTable::headerClicked(const QModelIndex &index)
{
QRect rect = tableHeader->visualRect(index);
headerMenu->exec(tableHeader->mapToGlobal(QPoint(rect.x(),rect.y())));
}?

munna
15th July 2006, 06:01
Thanks for the reply. But the problem is that this slot itself is not called.

Am I doing anything wrong.

Thanks a lot.

jpn
15th July 2006, 06:43
The reason is that QHeaderView doesn't emit clicked() signal. Use QHeaderView::sectionClicked(int section) instead.

munna
15th July 2006, 06:58
Use QHeaderView::sectionClicked(int section) instead

But then how can I get the bound of that section ?

Thanks

jpn
15th July 2006, 07:39
void headerClicked(int section)
{
QModelIndex idx = tableHeader->model()->index(0, section);
// about jacek's solution: AbstractItemView::visualRect()
// is protected and this seems to work..
QRect rect = visualRect(idx);
headerMenu->exec(tableHeader->mapToGlobal(rect.topLeft()));
}

munna
15th July 2006, 12:20
The slot is now called but the rect is(0,0,-1,-1) which is wrong. I am not getting the right co-ordiantes.

Any ideas?

Here is my code




void DataTable::headerClicked(int logicalIndex)
{
QModelIndex index = tableHeader->model()->index(0,logicalIndex);

QRect rect = tableHeader->visualRect(index);//QHeaderView::visualRect(index)
//rect is wrong
headerMenu->exec(tableHeader->mapToGlobal(QPoint(rect.x(),rect.y())));
}



Thaks a lot

munna
15th July 2006, 14:15
I have finally solve this. The following code will make the horizontal header of a QTableWidget behave like a menu bar.



//slot connected to the signal of sectionClicked(int) of QHeaderView.
void DataTable::headerClicked(int logicalIndex)
{
QModelIndex index = model()->index(0,logicalIndex);
QRect rect = visualRect(index);
headerMenu->exec(mapToGlobal(QPoint(rect.x(),rect.y()+tableHea der->height())));
}

jpn
15th July 2006, 14:58
Notice the difference? :)


headerMenu->exec(mapToGlobal(QPoint(rect.x(),rect.y()+tableHea der->height())));

and


headerMenu->exec(tableHeader->mapToGlobal(rect.topLeft()));

It matters from which widget's point of view the coordinates are mapped to global..