PDA

View Full Version : Q3PopupMenu



raphaelf
6th March 2006, 11:05
Hello everybody,

QT:4.1.1

I am trying to popup a menu inside my QTableView. I get a error on compiling this code:


void MainWindow::contextMenuEvent( QContextMenuEvent * )
{
Q3PopupMenu* contextMenu = new Q3PopupMenu( this );
Q_CHECK_PTR( contextMenu );
QLabel *caption = new QLabel( "<font color=darkblue><u><b>"
"ListView options</b></u></font>", this );
caption->setAlignment( Qt::AlignCenter );
contextMenu->insertItem( caption );
contextMenu->insertItem( "&einfügen", this, SLOT(selectTable()) );
contextMenu->exec( QCursor::pos() );
delete contextMenu;
}




private:
void contextMenuEvent ( QContextMenuEvent * );


ERROR:


test.cpp:21:23: Q3PopupMenu: No such file or directory
test.cpp: In member function `virtual void MainWindow::contextMenuEvent(QContext
MenuEvent*)':
test.cpp:217: error: invalid use of undefined type `struct Q3PopupMenu'
D:/apps/Qt/4.1.1/include/QtGui/../../src/gui/widgets/qmenu.h:359: error: forward
declaration of `struct Q3PopupMenu'
test.cpp:222: error: invalid use of undefined type `struct Q3PopupMenu'
D:/apps/Qt/4.1.1/include/QtGui/../../src/gui/widgets/qmenu.h:359: error: forward
declaration of `struct Q3PopupMenu'
test.cpp:223: error: invalid use of undefined type `struct Q3PopupMenu'
D:/apps/Qt/4.1.1/include/QtGui/../../src/gui/widgets/qmenu.h:359: error: forward
declaration of `struct Q3PopupMenu'
test.cpp:224: error: invalid use of undefined type `struct Q3PopupMenu'
D:/apps/Qt/4.1.1/include/QtGui/../../src/gui/widgets/qmenu.h:359: error: forward
declaration of `struct Q3PopupMenu'
test.cpp:225: warning: possible problem detected in invocation of delete operato
r:
test.cpp:217: warning: `contextMenu' has incomplete type
D:/apps/Qt/4.1.1/include/QtGui/../../src/gui/widgets/qmenu.h:359: warning: forwa
rd declaration of `struct Q3PopupMenu'
test.cpp:225: note: neither the destructor nor the class-specific operator delet
e will be called, even if they are declared when the class is defined.
mingw32-make[1]: *** [release\test.o] Error 1
mingw32-make[1]: Leaving directory `D:/rapha/qtprojekte/qt4/mainwindowTableModel
/ver3'
mingw32-make: *** [release] Error 2


I used this code on QT3 in my app. I have not found a example for QT4. Could be that in QT4 is easier, but i found nothing.
If somebody have a example for QT4, why not :p

Sarma
6th March 2006, 11:16
hi,
Instead of writing Q3PopupMenu, just write QPopupMenu and try the code. That will work and also don't forget to write :
#include <qpopupmenu.h>

As far asthe examples in QT3 are concerned, no where it is mentioned as Q3PopupMenu. You can find the example in the following file:
<qt-directory>/examples/menu/menu.cpp

Please let me know the result.

zlatko
6th March 2006, 11:18
have you this line in your *.cpp ?


#include <Q3PopupMenu>

Sarma
6th March 2006, 11:24
No. It is mentioned as :
#include <qpopupmenu.h>

raphaelf
6th March 2006, 11:27
Hi everybody! :D

zlatko: yes i have

both: i read that: For most purposes, QPopupMenu has been replaced by QMenu in Qt 4.
I tyied now and it works :p


QMenu* contextMenu = new QMenu( this );
Q_CHECK_PTR( contextMenu );
contextMenu->addAction( "&einfügen", this, SLOT(selectTable()) );
contextMenu->exec( QCursor::pos() );
delete contextMenu;

But the menu should just be called inside my QTableView :( and i am crashing my mind :confused:
Where could i find more informations or examples for that

zlatko
6th March 2006, 11:38
try reinplement in QTableWidget contextMenuEvent

raphaelf
6th March 2006, 11:55
Hi zlatko,
Can you give me more information what you mean? :p

jpn
6th March 2006, 12:19
But the menu should just be called inside my QTableView and i am crashing my mind
Where could i find more informations or examples for that


Override table view's contextMenuEvent instead of MainWindow's.. (or use an event filter).

zlatko
6th March 2006, 13:11
Hi zlatko,
Can you give me more information what you mean? :p

Create class inherited from QTableView, alike QTableViewMy..and reinplement contextMenuEvent( QContextMenuEvent * )..ant then call menu exec from it.


p.s. also look in jpn post, dont forget that you can use eventFilter instead of sublassing

raphaelf
6th March 2006, 14:12
Hi!
eventfilter looks to be easier like subclass it :o

I have tried to use it, but i dont have understand 100% how to use it,
please help me to understand :rolleyes:

test.h:


.
.
protected:
bool eventFilter( QObject *o, QEvent *e );


test.cpp:


MainWindow::MainWindow()

{
ui.setupUi(this);

ui.tree->installEventFilter(this);


// connect(ui.tabellen_cb, SIGNAL(currentIndexChanged (int)), this, SLOT(selectTable()));
connect(ui.actionverbinden, SIGNAL(triggered()), this, SLOT(openLoginDialog()));
connect(ui.actionNeu, SIGNAL(triggered()), this, SLOT(insertNewRow()));
connect(ui.tree, SIGNAL(itemDoubleClicked( QTreeWidgetItem*, int)), this, SLOT(selectTable()));

init();

}




void MainWindow::contextMenuEvent( QContextMenuEvent * )
{
QMenu* contextMenu = new QMenu( this );
Q_CHECK_PTR( contextMenu );
contextMenu->addAction( "&datensatz einfügen", this, SLOT(insertNewRow()) );
contextMenu->addAction( "&datensatz löschen", this, SLOT(deleteRow()) );
contextMenu->exec( QCursor::pos() );
delete contextMenu;
}




bool MainWindow::eventFilter( QObject *o, QEvent *e )
{

if (event->type() == QEvent::ContextMenu)
{

return true;

}
else
{

return false;
}

}


What is missing. Or what is false?:cool:

jpn
6th March 2006, 14:25
You can use the same way for showing a context menu, as you used in MainWindow::contextMenuEvent


if (event->type() == QEvent::ContextMenu)
{
// you may cast the event to appropriate type:
QContextMenuEvent* menuevent = static_cast<QContextMenuEvent*>(event);

// (construct and) show your context menu here:
contextMenu->exec(menuevent->globalPos());
}

raphaelf
6th March 2006, 14:48
Hi! :D
It works!



bool MainWindow::eventFilter( QObject *o, QEvent *event )
{

if (event->type() == QEvent::ContextMenu)
{

QContextMenuEvent* menuevent = static_cast<QContextMenuEvent*>(event);

QMenu* contextMenu = new QMenu( this );
Q_CHECK_PTR( contextMenu );
contextMenu->addAction( "&datensatz einfügen", this, SLOT(insertNewRow()) );
contextMenu->addAction( "&datensatz löschen", this, SLOT(deleteRow()) );
contextMenu->exec(menuevent->globalPos());
delete contextMenu;
}

}


If i have understand with "xxx->installEventFilter(this);" i enable a event for Object xxx ;)

raphaelf
6th March 2006, 15:37
Hi :(
I found a bug!!!

If i change my column width the values stay (see picutre).

If i comment this line, it works :confused:


ui.tableView->installEventFilter(this);


Oh, sheet, what should i do now?I dont think thats a qt bug

jpn
6th March 2006, 15:50
The event filter function should return a value:
bool MainWindow::eventFilter( QObject *o, QEvent *event )

From docs:

In your reimplementation of this function, if you want to filter the event out, i.e. stop it being handled further, return true; otherwise return false.
..so make it return false

zlatko
6th March 2006, 16:07
in other words


if (event->type() == QEvent::ContextMenu)
{
QContextMenuEvent* menuevent = static_cast<QContextMenuEvent*>(event);

QMenu* contextMenu = new QMenu( this );
Q_CHECK_PTR( contextMenu );
contextMenu->addAction( "&datensatz einfügen", this, SLOT(insertNewRow()) );
contextMenu->addAction( "&datensatz löschen", this, SLOT(deleteRow()) );
contextMenu->exec(menuevent->globalPos());
delete contextMenu;
return true;
}
else
return false;


I think your compilator give you something alike that warning : not of all control block retun value ;)

raphaelf
6th March 2006, 17:57
Hi Guys!
ufff :p

Thanks it looks to work perfect now :D

zlatko: yes there was a warning